article table
This commit is contained in:
parent
d5c09548fa
commit
6a7ff45817
|
|
@ -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,74 +145,84 @@ 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 statusColorMap: Record<string, ChipProps["color"]> = {
|
const cellValue = article[columnKey as keyof any];
|
||||||
active: "primary",
|
const statusColorMap: Record<string, ChipProps["color"]> = {
|
||||||
cancel: "danger",
|
active: "primary",
|
||||||
pending: "success",
|
cancel: "danger",
|
||||||
};
|
pending: "success",
|
||||||
|
};
|
||||||
|
|
||||||
switch (columnKey) {
|
switch (columnKey) {
|
||||||
case "status":
|
case "status":
|
||||||
return (
|
return (
|
||||||
<Chip
|
<Chip
|
||||||
className="capitalize "
|
className="capitalize "
|
||||||
color={statusColorMap[article.status]}
|
color={statusColorMap[article.status]}
|
||||||
size="lg"
|
size="lg"
|
||||||
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>
|
||||||
|
</Chip>
|
||||||
|
);
|
||||||
|
case "createdAt":
|
||||||
|
return <p>{convertDateFormat(article.createdAt)}</p>;
|
||||||
|
case "category":
|
||||||
|
return (
|
||||||
|
<p>
|
||||||
|
{article?.categories?.map((list: any) => list.title).join(", ") +
|
||||||
|
" "}
|
||||||
|
</p>
|
||||||
|
);
|
||||||
|
|
||||||
|
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/article/detail/${article.id}`}>
|
||||||
|
<EyeIconMdi className="inline mr-2 mb-1" />
|
||||||
|
Detail
|
||||||
|
</Link>
|
||||||
|
</DropdownItem>
|
||||||
|
<DropdownItem key="edit">
|
||||||
|
<Link href={`/admin/article/edit/${article.id}`}>
|
||||||
|
<CreateIconIon className="inline mr-2 mb-1" />
|
||||||
|
Edit
|
||||||
|
</Link>
|
||||||
|
</DropdownItem>
|
||||||
|
<DropdownItem
|
||||||
|
key="delete"
|
||||||
|
onClick={() => handleDelete(article.id)}
|
||||||
|
>
|
||||||
|
<DeleteIcon
|
||||||
|
color="red"
|
||||||
|
width={20}
|
||||||
|
height={16}
|
||||||
|
className="inline mr-2 mb-1"
|
||||||
|
/>
|
||||||
|
Delete
|
||||||
|
</DropdownItem>
|
||||||
|
</DropdownMenu>
|
||||||
|
</Dropdown>
|
||||||
</div>
|
</div>
|
||||||
</Chip>
|
);
|
||||||
);
|
|
||||||
case "createdAt":
|
|
||||||
return <p>{convertDateFormat(article.createdAt)}</p>;
|
|
||||||
|
|
||||||
case "actions":
|
default:
|
||||||
return (
|
return cellValue;
|
||||||
<div className="relative flex justify-star items-center gap-2">
|
}
|
||||||
<Dropdown className="lg:min-w-[150px] bg-black text-white shadow border ">
|
},
|
||||||
<DropdownTrigger>
|
[article]
|
||||||
<Button isIconOnly size="lg" variant="light">
|
);
|
||||||
<DotsYIcon className="text-default-300" />
|
|
||||||
</Button>
|
|
||||||
</DropdownTrigger>
|
|
||||||
<DropdownMenu>
|
|
||||||
<DropdownItem key="detail">
|
|
||||||
<Link href={`/admin/article/detail/${article.id}`}>
|
|
||||||
<EyeIconMdi className="inline mr-2 mb-1" />
|
|
||||||
Detail
|
|
||||||
</Link>
|
|
||||||
</DropdownItem>
|
|
||||||
<DropdownItem key="edit">
|
|
||||||
<Link href={`/admin/article/edit/${article.id}`}>
|
|
||||||
<CreateIconIon className="inline mr-2 mb-1" />
|
|
||||||
Edit
|
|
||||||
</Link>
|
|
||||||
</DropdownItem>
|
|
||||||
<DropdownItem
|
|
||||||
key="delete"
|
|
||||||
onClick={() => handleDelete(article.id)}
|
|
||||||
>
|
|
||||||
<DeleteIcon
|
|
||||||
color="red"
|
|
||||||
width={20}
|
|
||||||
height={16}
|
|
||||||
className="inline mr-2 mb-1"
|
|
||||||
/>
|
|
||||||
Delete
|
|
||||||
</DropdownItem>
|
|
||||||
</DropdownMenu>
|
|
||||||
</Dropdown>
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
|
|
||||||
default:
|
|
||||||
return cellValue;
|
|
||||||
}
|
|
||||||
}, []);
|
|
||||||
|
|
||||||
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>
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue