608 lines
19 KiB
TypeScript
608 lines
19 KiB
TypeScript
"use client";
|
|
import {
|
|
BannerIcon,
|
|
CloudUploadIcon,
|
|
CreateIconIon,
|
|
DeleteIcon,
|
|
DotsYIcon,
|
|
EyeIconMdi,
|
|
SearchIcon,
|
|
TimesIcon,
|
|
} from "@/components/icons";
|
|
import { close, error, loading, success } from "@/config/swal";
|
|
import {
|
|
deleteArticle,
|
|
getArticleByCategory,
|
|
getListArticle,
|
|
} from "@/service/article";
|
|
import { Article } from "@/types/globals";
|
|
import { convertDateFormat, convertDateFormatNoTimeV2 } from "@/utils/global";
|
|
import { Button } from "@heroui/button";
|
|
import {
|
|
Calendar,
|
|
Chip,
|
|
ChipProps,
|
|
Dropdown,
|
|
DropdownItem,
|
|
DropdownMenu,
|
|
DropdownTrigger,
|
|
Input,
|
|
Modal,
|
|
ModalBody,
|
|
ModalContent,
|
|
ModalFooter,
|
|
ModalHeader,
|
|
Pagination,
|
|
Popover,
|
|
PopoverContent,
|
|
PopoverTrigger,
|
|
Select,
|
|
SelectItem,
|
|
Spinner,
|
|
Switch,
|
|
Table,
|
|
TableBody,
|
|
TableCell,
|
|
TableColumn,
|
|
TableHeader,
|
|
TableRow,
|
|
Textarea,
|
|
useDisclosure,
|
|
} from "@heroui/react";
|
|
import Link from "next/link";
|
|
import { Fragment, Key, useCallback, useEffect, useState } from "react";
|
|
import Cookies from "js-cookie";
|
|
import * as z from "zod";
|
|
import { zodResolver } from "@hookform/resolvers/zod";
|
|
import { Controller, useForm } from "react-hook-form";
|
|
import Swal from "sweetalert2";
|
|
import withReactContent from "sweetalert2-react-content";
|
|
import { useDropzone } from "react-dropzone";
|
|
import Image from "next/image";
|
|
import SuggestionsChart from "@/components/main/dashboard/chart/suggestions-line-chart";
|
|
import { parseDate } from "@internationalized/date";
|
|
import { getFeedbacks, getFeedbacksById } from "@/service/feedbacks";
|
|
|
|
const columns = [
|
|
{ name: "No", uid: "no" },
|
|
{ name: "Nama", uid: "name" },
|
|
{ name: "Email", uid: "email" },
|
|
{ name: "Kritik & Saran", uid: "suggestions" },
|
|
{ name: "Aksi", uid: "actions" },
|
|
];
|
|
|
|
const createArticleSchema = z.object({
|
|
id: z.string().optional(),
|
|
commentFromName: z.string().min(2, {
|
|
message: "Nama harus diisi",
|
|
}),
|
|
commentFromEmail: z.string().min(2, {
|
|
message: "Email harus diisi",
|
|
}),
|
|
description: z.string().min(2, {
|
|
message: "Pesan harus diisi",
|
|
}),
|
|
file: z.string().optional(),
|
|
});
|
|
|
|
export default function SuggestionsTable() {
|
|
const MySwal = withReactContent(Swal);
|
|
const { isOpen, onOpen, onOpenChange, onClose } = useDisclosure();
|
|
|
|
const [page, setPage] = useState(1);
|
|
const [totalPage, setTotalPage] = useState(1);
|
|
const [article, setArticle] = useState<any[]>([]);
|
|
const [showData, setShowData] = useState("10");
|
|
const [search, setSearch] = useState("");
|
|
const [isReply, setIsReply] = useState(false);
|
|
const [replyValue, setReplyValue] = useState("");
|
|
|
|
const formOptions = {
|
|
resolver: zodResolver(createArticleSchema),
|
|
defaultValues: {
|
|
commentFromName: "",
|
|
description: "",
|
|
commentFromEmail: "",
|
|
file: "",
|
|
},
|
|
};
|
|
|
|
type UserSettingSchema = z.infer<typeof createArticleSchema>;
|
|
const {
|
|
control,
|
|
handleSubmit,
|
|
setValue,
|
|
formState: { errors },
|
|
} = useForm<UserSettingSchema>(formOptions);
|
|
|
|
useEffect(() => {
|
|
initState();
|
|
}, [page, showData]);
|
|
|
|
async function initState() {
|
|
const res = await getFeedbacks({ limit: showData, search: search });
|
|
getTableNumber(parseInt(showData), res.data?.data);
|
|
setTotalPage(res?.data?.meta?.totalPage);
|
|
}
|
|
|
|
const getTableNumber = (limit: number, data: Article[]) => {
|
|
if (data) {
|
|
const startIndex = limit * (page - 1);
|
|
let iterate = 0;
|
|
const newData = data.map((value: any) => {
|
|
iterate++;
|
|
value.no = startIndex + iterate;
|
|
return value;
|
|
});
|
|
setArticle(newData);
|
|
}
|
|
};
|
|
|
|
async function doDelete(id: any) {
|
|
// loading();
|
|
const resDelete = await deleteArticle(id);
|
|
|
|
if (resDelete?.error) {
|
|
error(resDelete.message);
|
|
return false;
|
|
}
|
|
close();
|
|
success("Berhasil Hapus");
|
|
initState();
|
|
}
|
|
|
|
const handleDelete = (id: any) => {
|
|
MySwal.fire({
|
|
title: "Hapus Data",
|
|
icon: "warning",
|
|
showCancelButton: true,
|
|
cancelButtonColor: "#3085d6",
|
|
confirmButtonColor: "#d33",
|
|
confirmButtonText: "Hapus",
|
|
}).then((result) => {
|
|
if (result.isConfirmed) {
|
|
doDelete(id);
|
|
}
|
|
});
|
|
};
|
|
|
|
const onSubmit = async (values: z.infer<typeof createArticleSchema>) => {
|
|
loading();
|
|
const formData = {
|
|
commentFromName: values.commentFromName,
|
|
message: values.description,
|
|
commentFromEmail: values.commentFromEmail,
|
|
};
|
|
console.log("dataas", formData);
|
|
close();
|
|
// setRefresh(!refresh);
|
|
// MySwal.fire({
|
|
// title: "Sukses",
|
|
// icon: "success",
|
|
// confirmButtonColor: "#3085d6",
|
|
// confirmButtonText: "OK",
|
|
// }).then((result) => {
|
|
// if (result.isConfirmed) {
|
|
// }
|
|
// });
|
|
};
|
|
|
|
const openModal = async (id: number) => {
|
|
const res = await getFeedbacksById(id);
|
|
const data = res?.data?.data;
|
|
setValue("id", String(data?.id));
|
|
setValue("commentFromName", data?.commentFromName);
|
|
setValue("commentFromEmail", data?.commentFromEmail);
|
|
setValue("description", data?.message);
|
|
|
|
onOpen();
|
|
};
|
|
|
|
const getMonthYearName = (date: any) => {
|
|
const newDate = new Date(date);
|
|
|
|
const months = [
|
|
"Januari",
|
|
"Februari",
|
|
"Maret",
|
|
"April",
|
|
"Mei",
|
|
"Juni",
|
|
"Juli",
|
|
"Agustus",
|
|
"September",
|
|
"Oktober",
|
|
"November",
|
|
"Desember",
|
|
];
|
|
const year = newDate.getFullYear();
|
|
|
|
const month = months[newDate.getMonth()];
|
|
return month + " " + year;
|
|
};
|
|
|
|
const renderCell = useCallback(
|
|
(suggestion: any, columnKey: Key) => {
|
|
const cellValue = suggestion[columnKey as keyof any];
|
|
|
|
switch (columnKey) {
|
|
case "commentFromEmail":
|
|
return (
|
|
<Link
|
|
href={`https://www.google.com/`}
|
|
target="_blank"
|
|
className="text-primary hover:underline"
|
|
>
|
|
https://www.google.com/
|
|
</Link>
|
|
);
|
|
|
|
case "actions":
|
|
return (
|
|
<div className="relative flex justify-star items-center gap-2">
|
|
<Dropdown className="lg:min-w-[150px] bg-black text-white shadow border ">
|
|
<DropdownTrigger>
|
|
<Button isIconOnly size="lg" variant="light">
|
|
<DotsYIcon className="text-default-300" />
|
|
</Button>
|
|
</DropdownTrigger>
|
|
<DropdownMenu>
|
|
{/* <DropdownItem key="detail">
|
|
<Link href={`/admin/suggestion/detail/${article.id}`}>
|
|
<EyeIconMdi className="inline mr-2 mb-1" />
|
|
Detail
|
|
</Link>
|
|
</DropdownItem> */}
|
|
<DropdownItem
|
|
key="edit"
|
|
onPress={() => openModal(suggestion.id)}
|
|
>
|
|
<CreateIconIon className="inline mr-2 mb-1" />
|
|
Edit
|
|
</DropdownItem>
|
|
|
|
<DropdownItem
|
|
key="delete"
|
|
// onPress={() => handleDelete(article.id)}
|
|
>
|
|
<DeleteIcon
|
|
color="red"
|
|
size={18}
|
|
className="inline ml-1 mr-2 mb-1"
|
|
/>
|
|
Delete
|
|
</DropdownItem>
|
|
</DropdownMenu>
|
|
</Dropdown>
|
|
</div>
|
|
);
|
|
|
|
default:
|
|
return cellValue;
|
|
}
|
|
},
|
|
[article]
|
|
);
|
|
|
|
let typingTimer: NodeJS.Timeout;
|
|
const doneTypingInterval = 1500;
|
|
|
|
const handleKeyUp = () => {
|
|
clearTimeout(typingTimer);
|
|
typingTimer = setTimeout(doneTyping, doneTypingInterval);
|
|
};
|
|
|
|
const handleKeyDown = () => {
|
|
clearTimeout(typingTimer);
|
|
};
|
|
|
|
async function doneTyping() {
|
|
initState();
|
|
}
|
|
|
|
const [startDateValue, setStartDateValue] = useState(
|
|
parseDate(convertDateFormatNoTimeV2(new Date()))
|
|
);
|
|
const [typeDate, setTypeDate] = useState("monthly");
|
|
|
|
return (
|
|
<>
|
|
<div className="flex flex-row gap-2">
|
|
<Button
|
|
color="primary"
|
|
variant={typeDate === "monthly" ? "solid" : "bordered"}
|
|
onPress={() => setTypeDate("monthly")}
|
|
className="w-[140px] text-xs lg:text-sm h-[30px] lg:h-[40px] rounded-md lg:rounded-lg"
|
|
>
|
|
Bulanan
|
|
</Button>
|
|
<Button
|
|
color="primary"
|
|
onPress={() => setTypeDate("weekly")}
|
|
variant={typeDate === "weekly" ? "solid" : "bordered"}
|
|
className="w-[140px] text-xs lg:text-sm h-[30px] lg:h-[40px] rounded-md lg:rounded-lg"
|
|
>
|
|
Mingguan
|
|
</Button>
|
|
<Button
|
|
color="primary"
|
|
onPress={() => setTypeDate("daily")}
|
|
variant={typeDate === "daily" ? "solid" : "bordered"}
|
|
className="w-[140px] text-xs lg:text-sm h-[30px] lg:h-[40px] rounded-md lg:rounded-lg"
|
|
>
|
|
Harian
|
|
</Button>
|
|
<div className="w-[140px]">
|
|
<Popover
|
|
placement="bottom"
|
|
classNames={{ content: ["!bg-transparent", "p-0"] }}
|
|
>
|
|
<PopoverTrigger>
|
|
<Button className="w-[140px] text-xs lg:text-sm h-[30px] lg:h-[40px] rounded-md lg:rounded-lg">
|
|
{getMonthYearName(startDateValue)}
|
|
</Button>
|
|
</PopoverTrigger>
|
|
<PopoverContent className="bg-transparent">
|
|
<Calendar value={startDateValue} onChange={setStartDateValue} />
|
|
</PopoverContent>
|
|
</Popover>
|
|
</div>
|
|
</div>
|
|
<div className="flex flex-row w-full h-full">
|
|
<div className="w-full h-[30vh] lg:h-[300px] text-black">
|
|
<SuggestionsChart
|
|
type={typeDate}
|
|
date={`${startDateValue.month} ${startDateValue.year}`}
|
|
/>
|
|
</div>
|
|
</div>
|
|
<div className="py-3">
|
|
<div className="flex flex-col items-start rounded-2xl gap-3">
|
|
<div className="flex flex-col md:flex-row gap-3 w-full">
|
|
<div className="flex flex-col gap-1 w-full lg:w-1/3">
|
|
<p className="font-semibold text-sm">Pencarian</p>
|
|
<Input
|
|
aria-label="Search"
|
|
classNames={{
|
|
inputWrapper: "bg-default-100",
|
|
input: "text-sm",
|
|
}}
|
|
labelPlacement="outside"
|
|
startContent={
|
|
<SearchIcon className="text-base text-default-400 pointer-events-none flex-shrink-0" />
|
|
}
|
|
type="text"
|
|
onChange={(e) => setSearch(e.target.value)}
|
|
onKeyUp={handleKeyUp}
|
|
onKeyDown={handleKeyDown}
|
|
/>
|
|
</div>
|
|
<div className="flex flex-col gap-1 w-full lg:w-[72px]">
|
|
<p className="font-semibold text-sm">Data</p>
|
|
<Select
|
|
label=""
|
|
variant="bordered"
|
|
labelPlacement="outside"
|
|
placeholder="Select"
|
|
selectedKeys={[showData]}
|
|
className="w-full"
|
|
classNames={{ trigger: "border-1" }}
|
|
onChange={(e) =>
|
|
e.target.value === "" ? "" : setShowData(e.target.value)
|
|
}
|
|
>
|
|
<SelectItem key="5" value="5">
|
|
5
|
|
</SelectItem>
|
|
<SelectItem key="10" value="10">
|
|
10
|
|
</SelectItem>
|
|
</Select>
|
|
</div>
|
|
<Button onPress={() => openModal(4)}>test</Button>
|
|
</div>
|
|
<Table
|
|
aria-label="micro issue table"
|
|
className="rounded-3xl"
|
|
classNames={{
|
|
th: "bg-white dark:bg-black text-black dark:text-white border-b-1 text-md",
|
|
base: "bg-white dark:bg-black border",
|
|
wrapper:
|
|
"min-h-[50px] bg-transpararent text-black dark:text-white ",
|
|
}}
|
|
>
|
|
<TableHeader columns={columns}>
|
|
{(column) => (
|
|
<TableColumn key={column.uid}>{column.name}</TableColumn>
|
|
)}
|
|
</TableHeader>
|
|
<TableBody
|
|
items={article}
|
|
emptyContent={"No data to display."}
|
|
loadingContent={<Spinner label="Loading..." />}
|
|
>
|
|
{(item: any) => (
|
|
<TableRow key={item.id}>
|
|
{(columnKey) => (
|
|
<TableCell>{renderCell(item, columnKey)}</TableCell>
|
|
)}
|
|
</TableRow>
|
|
)}
|
|
</TableBody>
|
|
</Table>
|
|
<div className="my-2 w-full flex justify-center">
|
|
<Pagination
|
|
isCompact
|
|
showControls
|
|
showShadow
|
|
color="primary"
|
|
classNames={{
|
|
base: "bg-transparent",
|
|
wrapper: "bg-transparent",
|
|
}}
|
|
page={page}
|
|
total={totalPage}
|
|
onChange={(page) => setPage(page)}
|
|
/>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<Modal isOpen={isOpen} onOpenChange={onOpenChange} size="3xl">
|
|
<ModalContent>
|
|
{() => (
|
|
<>
|
|
<ModalHeader className="flex flex-col gap-1">
|
|
Kritik Saran
|
|
</ModalHeader>
|
|
<ModalBody>
|
|
<form
|
|
onSubmit={handleSubmit(onSubmit)}
|
|
className="flex flex-col gap-3"
|
|
>
|
|
<div className="flex flex-col gap-1">
|
|
<p className="text-sm">Nama</p>
|
|
<Controller
|
|
control={control}
|
|
name="commentFromName"
|
|
render={({ field: { onChange, value } }) => (
|
|
<Input
|
|
type="text"
|
|
id="commentFromName"
|
|
placeholder=""
|
|
label=""
|
|
value={value}
|
|
onChange={onChange}
|
|
labelPlacement="outside"
|
|
className="w-full "
|
|
classNames={{
|
|
inputWrapper: [
|
|
"border-1 rounded-lg",
|
|
"dark:group-data-[focused=false]:bg-transparent !border-1 dark:!border-gray-400",
|
|
],
|
|
}}
|
|
variant="bordered"
|
|
/>
|
|
)}
|
|
/>
|
|
{errors?.commentFromName && (
|
|
<p className="text-red-400 text-sm">
|
|
{errors.commentFromName?.message}
|
|
</p>
|
|
)}
|
|
</div>
|
|
<div className="flex flex-col gap-1">
|
|
<p className="text-sm">Email</p>
|
|
<Controller
|
|
control={control}
|
|
name="commentFromEmail"
|
|
render={({ field: { onChange, value } }) => (
|
|
<Input
|
|
type="text"
|
|
id="commentFromEmail"
|
|
placeholder=""
|
|
label=""
|
|
value={value}
|
|
onChange={onChange}
|
|
labelPlacement="outside"
|
|
className="w-full "
|
|
classNames={{
|
|
inputWrapper: [
|
|
"border-1 rounded-lg",
|
|
"dark:group-data-[focused=false]:bg-transparent !border-1 dark:!border-gray-400",
|
|
],
|
|
}}
|
|
variant="bordered"
|
|
/>
|
|
)}
|
|
/>
|
|
{errors?.commentFromEmail && (
|
|
<p className="text-red-400 text-sm">
|
|
{errors.commentFromEmail?.message}
|
|
</p>
|
|
)}
|
|
</div>
|
|
<div className="flex flex-col gap-1">
|
|
<p className="text-sm">Deskripsi</p>
|
|
<Controller
|
|
control={control}
|
|
name="description"
|
|
render={({ field: { onChange, value } }) => (
|
|
<Textarea
|
|
type="text"
|
|
id="description"
|
|
placeholder=""
|
|
label=""
|
|
value={value}
|
|
onChange={onChange}
|
|
labelPlacement="outside"
|
|
className="w-full "
|
|
classNames={{
|
|
inputWrapper: [
|
|
"border-1 rounded-lg",
|
|
"dark:group-data-[focused=false]:bg-transparent !border-1 dark:!border-gray-400",
|
|
],
|
|
}}
|
|
variant="bordered"
|
|
/>
|
|
)}
|
|
/>
|
|
{errors?.description && (
|
|
<p className="text-red-400 text-sm">
|
|
{errors.description?.message}
|
|
</p>
|
|
)}
|
|
</div>
|
|
|
|
<div className="flex flex-col gap-1">
|
|
<a
|
|
className="text-sm cursor-pointer hover:underline text-primary"
|
|
onClick={() => setIsReply(!isReply)}
|
|
>
|
|
Balas
|
|
</a>
|
|
{isReply && (
|
|
<Textarea
|
|
type="text"
|
|
id="description"
|
|
placeholder=""
|
|
label=""
|
|
value={replyValue}
|
|
onValueChange={setReplyValue}
|
|
labelPlacement="outside"
|
|
className="w-full "
|
|
classNames={{
|
|
inputWrapper: [
|
|
"border-1 rounded-lg",
|
|
"dark:group-data-[focused=false]:bg-transparent !border-1 dark:!border-gray-400",
|
|
],
|
|
}}
|
|
variant="bordered"
|
|
/>
|
|
)}
|
|
</div>
|
|
|
|
<ModalFooter className="self-end grow items-end">
|
|
{isReply && (
|
|
<Button
|
|
color="primary"
|
|
type="submit"
|
|
isDisabled={replyValue == ""}
|
|
>
|
|
Kirim
|
|
</Button>
|
|
)}
|
|
|
|
<Button color="danger" variant="light" onPress={onClose}>
|
|
Tutup
|
|
</Button>
|
|
</ModalFooter>
|
|
</form>
|
|
</ModalBody>
|
|
</>
|
|
)}
|
|
</ModalContent>
|
|
</Modal>
|
|
</>
|
|
);
|
|
}
|