|
1 | 1 | "use client"; |
2 | 2 |
|
3 | | -import type { Category, CategoryForm } from "@/types"; |
4 | | - |
5 | 3 | import { useState } from "react"; |
6 | 4 | import { useRouter } from "next/navigation"; |
7 | 5 | import { useDisclosure, addToast } from "@heroui/react"; |
8 | 6 | import { useForm } from "react-hook-form"; |
9 | 7 | import { zodResolver } from "@hookform/resolvers/zod"; |
| 8 | +import { SquarePenIcon, Trash2 } from "lucide-react"; |
| 9 | + |
10 | 10 | import { |
11 | 11 | createCategory, |
12 | 12 | deleteCategory, |
13 | 13 | updateCategory, |
14 | 14 | } from "@/actions/categories"; |
15 | | -import { CategoryDrawer, CategoryTable } from "@/components/categories"; |
16 | | -import { DeleteModal, HeadingDashboard } from "@/components/ui"; |
| 15 | +import { CategoryDrawer } from "@/components/categories"; |
| 16 | +import { DeleteModal, HeadingDashboard, Table } from "@/components/ui"; |
17 | 17 | import { categorySchema } from "@/lib/zod"; |
18 | 18 |
|
| 19 | +import type { Category, CategoryForm, TableAction, TableColumn } from "@/types"; |
| 20 | + |
19 | 21 | type Props = { |
20 | 22 | categories: Category[]; |
21 | 23 | }; |
@@ -98,20 +100,54 @@ export default function CategoriesPageClient({ categories }: Props) { |
98 | 100 | } |
99 | 101 | }; |
100 | 102 |
|
| 103 | + const columns: TableColumn[] = [ |
| 104 | + { name: "Name", uid: "name", sortable: true }, |
| 105 | + { name: "Slug", uid: "slug", sortable: true }, |
| 106 | + { name: "Created At", uid: "created_at", sortable: true }, |
| 107 | + ]; |
| 108 | + |
| 109 | + const actions: TableAction[] = [ |
| 110 | + { |
| 111 | + key: "edit", |
| 112 | + label: "Edit Category", |
| 113 | + icon: <SquarePenIcon size={18} />, |
| 114 | + shortcut: "⌘E", |
| 115 | + onAction: (category) => openEditModal(category), |
| 116 | + }, |
| 117 | + { |
| 118 | + key: "delete", |
| 119 | + label: "Delete Category", |
| 120 | + icon: <Trash2 size={18} />, |
| 121 | + color: "danger", |
| 122 | + shortcut: "⌘D", |
| 123 | + onAction: (category) => handleDelete(category.id), |
| 124 | + }, |
| 125 | + ]; |
| 126 | + |
101 | 127 | return ( |
102 | 128 | <div className="flex flex-col gap-4"> |
103 | 129 | {/* Heading */} |
104 | | - <HeadingDashboard |
105 | | - title="Categories" |
106 | | - count={categories.length} |
107 | | - openAddModal={openAddModal} |
108 | | - /> |
| 130 | + <HeadingDashboard title="Categories" count={categories.length} /> |
109 | 131 |
|
110 | 132 | {/* Table */} |
111 | | - <CategoryTable |
112 | | - categories={categories} |
113 | | - openEditModal={openEditModal} |
114 | | - handleDelete={handleDelete} |
| 133 | + <Table |
| 134 | + title="Category table" |
| 135 | + data={categories} |
| 136 | + columns={columns} |
| 137 | + actions={actions} |
| 138 | + searchPlaceholder="Search by name..." |
| 139 | + searchKeys={["name"]} |
| 140 | + defaultRowsPerPage={10} |
| 141 | + rowsPerPageOptions={[10, 25, 50, 100]} |
| 142 | + addButton={{ label: "Add Category", onAdd: () => openAddModal() }} |
| 143 | + cellRenderer={({ item, columnKey, value }) => { |
| 144 | + switch (columnKey) { |
| 145 | + case "created_at": |
| 146 | + return new Date(value).toLocaleDateString(); |
| 147 | + default: |
| 148 | + return value; |
| 149 | + } |
| 150 | + }} |
115 | 151 | /> |
116 | 152 |
|
117 | 153 | {/* Drawer */} |
|
0 commit comments