article table

This commit is contained in:
Rama Priyanto 2025-02-14 23:53:14 +07:00
parent d5c09548fa
commit 6a7ff45817
1 changed files with 84 additions and 71 deletions

View File

@ -43,23 +43,28 @@ import withReactContent from "sweetalert2-react-content";
const columns = [ const columns = [
{ name: "No", uid: "no" }, { name: "No", uid: "no" },
{ name: "Judul", uid: "title" }, { name: "Judul", uid: "title" },
{ name: "Kategori", uid: "categoryName" }, { name: "Kategori", uid: "category" },
{ name: "Tanggal Unggah", uid: "createdAt" }, { name: "Tanggal Unggah", uid: "createdAt" },
{ name: "Kreator", uid: "createdByName" }, { name: "Kreator", uid: "createdByName" },
{ name: "Aksi", uid: "actions" }, { name: "Aksi", uid: "actions" },
]; ];
interface Category {
id: number;
title: string;
}
type ArticleData = Article & { type ArticleData = Article & {
no: number; no: number;
createdAt: string; createdAt: string;
categories: Category[];
}; };
export default function ArticleTable() { export default function ArticleTable() {
const MySwal = withReactContent(Swal); const MySwal = withReactContent(Swal);
const [page, setPage] = useState(1); const [page, setPage] = useState(1);
const [totalPage, setTotalPage] = useState(1); const [totalPage, setTotalPage] = useState(1);
const [article, setArticle] = useState<ArticleData[]>([]); const [article, setArticle] = useState<any[]>([]);
const [showData, setShowData] = useState("10"); const [showData, setShowData] = useState("10");
const [search, setSearch] = useState(""); const [search, setSearch] = useState("");
const [categories, setCategoies] = useState<any>([]); const [categories, setCategoies] = useState<any>([]);
@ -96,7 +101,6 @@ export default function ArticleTable() {
}; };
const res = await getListArticle(req); const res = await getListArticle(req);
getTableNumber(parseInt(showData), res.data?.data); getTableNumber(parseInt(showData), res.data?.data);
console.log("res.data?.data", res.data);
setTotalPage(res?.data?.meta?.totalPage); setTotalPage(res?.data?.meta?.totalPage);
} }
@ -109,7 +113,6 @@ export default function ArticleTable() {
value.no = startIndex + iterate; value.no = startIndex + iterate;
return value; return value;
}); });
console.log("daata", data);
setArticle(newData); setArticle(newData);
} }
}; };
@ -142,8 +145,9 @@ export default function ArticleTable() {
}); });
}; };
const renderCell = useCallback((article: ArticleData, columnKey: Key) => { const renderCell = useCallback(
const cellValue = article[columnKey as keyof ArticleData]; (article: any, columnKey: Key) => {
const cellValue = article[columnKey as keyof any];
const statusColorMap: Record<string, ChipProps["color"]> = { const statusColorMap: Record<string, ChipProps["color"]> = {
active: "primary", active: "primary",
cancel: "danger", cancel: "danger",
@ -160,12 +164,19 @@ export default function ArticleTable() {
variant="flat" variant="flat"
> >
<div className="flex flex-row items-center gap-2 justify-center"> <div className="flex flex-row items-center gap-2 justify-center">
{cellValue} {article.status}
</div> </div>
</Chip> </Chip>
); );
case "createdAt": case "createdAt":
return <p>{convertDateFormat(article.createdAt)}</p>; return <p>{convertDateFormat(article.createdAt)}</p>;
case "category":
return (
<p>
{article?.categories?.map((list: any) => list.title).join(", ") +
" "}
</p>
);
case "actions": case "actions":
return ( return (
@ -209,7 +220,9 @@ export default function ArticleTable() {
default: default:
return cellValue; return cellValue;
} }
}, []); },
[article]
);
let typingTimer: NodeJS.Timeout; let typingTimer: NodeJS.Timeout;
const doneTypingInterval = 1500; const doneTypingInterval = 1500;
@ -333,7 +346,7 @@ export default function ArticleTable() {
emptyContent={"No data to display."} emptyContent={"No data to display."}
loadingContent={<Spinner label="Loading..." />} loadingContent={<Spinner label="Loading..." />}
> >
{(item) => ( {(item: any) => (
<TableRow key={item.id}> <TableRow key={item.id}>
{(columnKey) => ( {(columnKey) => (
<TableCell>{renderCell(item, columnKey)}</TableCell> <TableCell>{renderCell(item, columnKey)}</TableCell>