"use client"; import { BannerIcon, CopyIcon, CreateIconIon, DeleteIcon, DotsYIcon, EyeIconMdi, SearchIcon, TimesIcon, } from "@/components/icons"; import { close, error, loading, success, successToast } from "@/config/swal"; import { deleteArticle, getArticleByCategory, getListArticle, getListArticleAdminPage, updateIsBannerArticle, } from "@/services/article"; import { Article } from "@/types/globals"; import { convertDateFormat, convertDateFormatNoTime, convertDateFormatNoTimeV2, } from "@/utils/global"; import { Button } from "@heroui/button"; import { Autocomplete, AutocompleteItem, Calendar, Chip, ChipProps, Dropdown, DropdownItem, DropdownMenu, DropdownTrigger, Input, Pagination, Popover, PopoverContent, PopoverTrigger, Select, SelectItem, Spinner, Table, TableBody, TableCell, TableColumn, TableHeader, TableRow, } from "@heroui/react"; import Link from "next/link"; import { Key, useCallback, useEffect, useState } from "react"; import Datepicker from "react-tailwindcss-datepicker"; import Swal from "sweetalert2"; import withReactContent from "sweetalert2-react-content"; import Cookies from "js-cookie"; import { parseDate } from "@internationalized/date"; import { listMasterUsers } from "@/services/master-user"; import ReactSelect from "react-select"; import makeAnimated from "react-select/animated"; const columns = [ { name: "No", uid: "no" }, { name: "Judul", uid: "title" }, { name: "Banner", uid: "isBanner" }, { name: "Kategori", uid: "category" }, { name: "Tanggal Unggah", uid: "createdAt" }, { name: "Kreator", uid: "createdByName" }, { name: "Status", uid: "isPublish" }, { name: "Aksi", uid: "actions" }, ]; const columnsOtherRole = [ { name: "No", uid: "no" }, { name: "Judul", uid: "title" }, { name: "Kategori", uid: "category" }, { name: "Tanggal Unggah", uid: "createdAt" }, { name: "Kreator", uid: "createdByName" }, { name: "Status", uid: "isPublish" }, { name: "Aksi", uid: "actions" }, ]; interface Category { id: number; title: string; } type ArticleData = Article & { no: number; createdAt: string; categories: Category[]; }; export default function ArticleTable() { const MySwal = withReactContent(Swal); const username = Cookies.get("username"); const userId = Cookies.get("uie"); const animatedComponents = makeAnimated(); const [page, setPage] = useState(1); const [totalPage, setTotalPage] = useState(1); const [article, setArticle] = useState([]); const [showData, setShowData] = useState("10"); const [search, setSearch] = useState(""); const [categories, setCategories] = useState([]); const [users, setUsers] = useState([]); const [selectedCategories, setSelectedCategories] = useState([]); const [selectedUsers, setSelectedUsers] = useState([]); const [startDateValue, setStartDateValue] = useState({ startDate: null, endDate: null, }); const [articleDate, setArticleDate] = useState<{ startDate: any; endDate: any; }>({ startDate: parseDate( convertDateFormatNoTimeV2( new Date(new Date().setDate(new Date().getDate() - 7)) ) ), endDate: parseDate(convertDateFormatNoTimeV2(new Date())), }); useEffect(() => { initState(); }, [ page, showData, startDateValue, selectedCategories, articleDate, selectedUsers, ]); useEffect(() => { getCategories(); getUsers(); }, []); const setupList = (data: any, type: string) => { const temp = []; if (data) { for (const element of data) { temp.push({ id: element.id, label: element.title || element.fullname, value: element.id, }); } if (type === "users") { setUsers(temp); } if (type === "category") { setCategories(temp); } } }; async function getUsers() { const res = await listMasterUsers({ page: page, limit: -1 }); setupList(res?.data?.data, "users"); } async function getCategories() { const res = await getArticleByCategory(); setupList(res?.data?.data, "category"); } const onSelectionChange = (id: Key | null) => { setSelectedCategories(String(id)); }; const getDate = (data: any) => { if (data === null) { return ""; } else { return `${data.year}-${data.month < 10 ? `0${data.month}` : data.month}-${ data.day < 10 ? `0${data.day}` : data.day }`; } }; const getIds = (data: { id: number; label: string; value: number }[]) => { let temp = ""; if (data) { for (let i = 0; i < data.length; i++) { if (i == 0) { temp = String(data[i].id); } else { temp = temp + `,${data[i].id}`; } } } return temp; }; async function initState() { loading(); console.log("test", getIds(selectedCategories)); const req = { limit: showData, page: page, search: search, startDate: getDate(articleDate.startDate), endDate: getDate(articleDate.endDate), categoryIds: getIds(selectedCategories), createdByIds: getIds(selectedUsers), sort: "desc", sortBy: "created_at", }; const res = await getListArticleAdminPage(req); await getTableNumber(parseInt(showData), res.data?.data); setTotalPage(res?.data?.meta?.totalPage); close(); } const getTableNumber = async (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); } else { setArticle([]); } }; 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 handleBanner = async (id: number, status: boolean) => { const res = await updateIsBannerArticle(id, status); if (res?.error) { error(res?.message); return false; } initState(); }; const copyUrlArticle = async (id: number, slug: string) => { const url = `${window.location.protocol}//${window.location.host}` + "/news/detail/" + `${id}-${slug}`; try { await navigator.clipboard.writeText(url); successToast("Success", "Article Copy to Clipboard"); setTimeout(() => {}, 1500); } catch (err) { ("Failed to copy!"); } }; const renderCell = useCallback( (article: any, columnKey: Key) => { const cellValue = article[columnKey as keyof any]; switch (columnKey) { case "isPublish": return ( // //
// {article.status} //
//

{article.isPublish ? "Publish" : "Draft"}

); case "isBanner": return

{article.isBanner ? "Ya" : "Tidak"}

; case "createdAt": return

{convertDateFormat(article.createdAt)}

; case "category": return (

{article?.categories?.map((list: any) => list.title).join(", ") + " "}

); case "actions": return (
copyUrlArticle(article.id, article.slug)} > Copy Url Article Detail {(username?.includes("mabes") || Number(userId) === article.createdById) && ( Edit )} handleBanner(article?.id, !article?.isBanner) } className={username?.includes("mabes") ? "" : "hidden"} > {username?.includes("mabes") && ( <> {article?.isBanner ? "Hapus dari Banner" : "Jadikan Banner"} )} handleDelete(article.id)} className={ username?.includes("mabes") || Number(userId) === article.createdById ? "" : "hidden" } > {(username?.includes("mabes") || Number(userId) === article.createdById) && ( <> {" "} Delete )}
); default: return cellValue; } }, [article, page] ); let typingTimer: NodeJS.Timeout; const doneTypingInterval = 1500; const handleKeyUp = () => { clearTimeout(typingTimer); typingTimer = setTimeout(doneTyping, doneTypingInterval); }; const handleKeyDown = () => { clearTimeout(typingTimer); }; async function doneTyping() { setPage(1); initState(); } const [hasMounted, setHasMounted] = useState(false); useEffect(() => { setHasMounted(true); }, []); // Render if (!hasMounted) return null; return ( <>

Pencarian

} type="text" onChange={(e) => setSearch(e.target.value)} onKeyUp={handleKeyUp} onKeyDown={handleKeyDown} />

Data

Kategori

"!rounded-lg bg-white !border-1 !border-gray-200 dark:!border-stone-500", }} classNamePrefix="select" onChange={setSelectedCategories} closeMenuOnSelect={false} components={animatedComponents} isClearable={true} isSearchable={true} isMulti={true} placeholder="Kategori..." name="sub-module" options={categories} // styles={{ // control: (base) => ({ // ...base, // width: "100%", // overflowX: "auto", // }), // valueContainer: (base) => ({ // ...base, // display: "flex", // flexWrap: "nowrap", // overflowX: "auto", // whiteSpace: "nowrap", // gap: "4px", // }), // multiValue: (base) => ({ // ...base, // whiteSpace: "nowrap", // flexShrink: 0, // }), // multiValueLabel: (base) => ({ // ...base, // whiteSpace: "nowrap", // }), // }} />
{username?.includes("mabes") && (

Author

"!rounded-lg bg-white !border-1 !border-gray-200 dark:!border-stone-500", }} classNamePrefix="select" onChange={setSelectedUsers} closeMenuOnSelect={false} components={animatedComponents} isClearable={true} isSearchable={true} isMulti={true} placeholder="Kategori..." name="sub-module" options={users} />
)}

Start Date

setArticleDate({ startDate: e, endDate: articleDate.endDate, }) } maxValue={articleDate.endDate} />

End Date

setArticleDate({ startDate: articleDate.startDate, endDate: e, }) } minValue={articleDate.startDate} />
{(column) => ( {column.name} )} } > {(item: any) => ( {(columnKey) => ( {renderCell(item, columnKey)} )} )}
setPage(page)} />
); }