2025-03-14 13:28:15 +08:00
|
|
|
import {
|
|
|
|
|
PencilSquareIcon,
|
|
|
|
|
PlusIcon,
|
|
|
|
|
TrashIcon,
|
2025-03-14 23:50:27 +08:00
|
|
|
} from '@heroicons/react/24/solid'
|
2025-03-09 15:31:10 +08:00
|
|
|
import DT, { type Config, type ConfigColumns } from 'datatables.net-dt'
|
|
|
|
|
import DataTable, { type DataTableSlots } from 'datatables.net-react'
|
2025-03-13 11:01:28 +07:00
|
|
|
import { useState } from 'react'
|
2025-03-07 12:42:08 +07:00
|
|
|
import { Link, useRouteLoaderData } from 'react-router'
|
|
|
|
|
|
2025-03-08 00:45:05 +08:00
|
|
|
import type { TCategoryResponse } from '~/apis/common/get-categories'
|
2025-03-13 11:01:28 +07:00
|
|
|
import { DialogDelete } from '~/components/dialog/delete'
|
2025-03-07 12:42:08 +07:00
|
|
|
import { Button } from '~/components/ui/button'
|
|
|
|
|
import { UiTable } from '~/components/ui/table'
|
|
|
|
|
import { TitleDashboard } from '~/components/ui/title-dashboard'
|
2025-03-09 09:57:26 +08:00
|
|
|
import type { loader } from '~/routes/_admin.lg-admin._dashboard'
|
2025-03-12 21:30:30 +08:00
|
|
|
|
2025-03-07 12:42:08 +07:00
|
|
|
export const CategoriesPage = () => {
|
2025-03-09 09:57:26 +08:00
|
|
|
const loaderData = useRouteLoaderData<typeof loader>(
|
|
|
|
|
'routes/_admin.lg-admin._dashboard',
|
|
|
|
|
)
|
2025-03-13 11:01:28 +07:00
|
|
|
const [selectedCategory, setSelectedCategory] = useState<TCategoryResponse>()
|
2025-03-07 12:42:08 +07:00
|
|
|
DataTable.use(DT)
|
2025-03-09 15:31:10 +08:00
|
|
|
const dataTable =
|
|
|
|
|
loaderData?.categoriesData?.sort((a, b) => {
|
|
|
|
|
if (a.sequence === null) return 1
|
|
|
|
|
if (b.sequence === null) return -1
|
|
|
|
|
return a.sequence - b.sequence
|
|
|
|
|
}) || []
|
|
|
|
|
const dataColumns: ConfigColumns[] = [
|
2025-03-07 12:42:08 +07:00
|
|
|
{
|
|
|
|
|
title: 'No',
|
|
|
|
|
render: (
|
2025-03-09 15:34:00 +08:00
|
|
|
_data: unknown,
|
|
|
|
|
_type: unknown,
|
2025-03-08 00:45:05 +08:00
|
|
|
row: TCategoryResponse,
|
2025-03-07 12:42:08 +07:00
|
|
|
meta: { row: number },
|
|
|
|
|
) => {
|
2025-03-08 00:45:05 +08:00
|
|
|
return `<div>${meta.row + 1}</div> ${
|
|
|
|
|
row.sequence === null
|
|
|
|
|
? ''
|
|
|
|
|
: `<pre class="text-sm text-[#7C7C7C] inline">Urutan: ${row.sequence}</pre>`
|
|
|
|
|
}`
|
2025-03-07 12:42:08 +07:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
2025-03-08 00:45:05 +08:00
|
|
|
title: 'Kategori',
|
2025-03-07 12:42:08 +07:00
|
|
|
},
|
|
|
|
|
{
|
2025-03-08 00:45:05 +08:00
|
|
|
title: 'Deskripsi',
|
|
|
|
|
data: 'description',
|
2025-03-07 12:42:08 +07:00
|
|
|
},
|
2025-03-07 13:07:28 +07:00
|
|
|
{
|
|
|
|
|
title: 'Action',
|
|
|
|
|
data: 'id',
|
|
|
|
|
},
|
2025-03-07 12:42:08 +07:00
|
|
|
]
|
2025-03-09 15:31:10 +08:00
|
|
|
const dataSlot: DataTableSlots = {
|
2025-03-08 00:45:05 +08:00
|
|
|
1: (_value: unknown, _type: unknown, data: TCategoryResponse) => (
|
|
|
|
|
<div>
|
|
|
|
|
<div>{data.name}</div>
|
|
|
|
|
<pre className="text-sm text-[#7C7C7C]">Kode: {data.code}</pre>
|
|
|
|
|
</div>
|
|
|
|
|
),
|
2025-03-13 11:01:28 +07:00
|
|
|
3: (value: string, _type: unknown, data: TCategoryResponse) => (
|
|
|
|
|
<div className="flex space-x-2">
|
|
|
|
|
<Button
|
|
|
|
|
as="a"
|
|
|
|
|
href={`/lg-admin/categories/update/${value}`}
|
|
|
|
|
size="icon"
|
|
|
|
|
title="Update Kategori"
|
|
|
|
|
>
|
|
|
|
|
<PencilSquareIcon className="h-4 w-4" />
|
|
|
|
|
</Button>
|
2025-03-13 11:11:33 +07:00
|
|
|
{data.code === 'spotlight' ? (
|
|
|
|
|
''
|
|
|
|
|
) : (
|
|
|
|
|
<Button
|
|
|
|
|
type="button"
|
|
|
|
|
size="icon"
|
2025-03-14 23:50:27 +08:00
|
|
|
variant="danger"
|
2025-03-13 11:11:33 +07:00
|
|
|
onClick={() => setSelectedCategory(data)}
|
|
|
|
|
title="Hapus Kategori"
|
|
|
|
|
>
|
|
|
|
|
<TrashIcon className="h-4 w-4" />
|
|
|
|
|
</Button>
|
|
|
|
|
)}
|
2025-03-13 11:01:28 +07:00
|
|
|
</div>
|
2025-03-07 13:07:28 +07:00
|
|
|
),
|
|
|
|
|
}
|
2025-03-09 15:31:10 +08:00
|
|
|
const dataOptions: Config = {
|
2025-03-07 14:58:35 +08:00
|
|
|
paging: true,
|
|
|
|
|
searching: true,
|
|
|
|
|
ordering: true,
|
|
|
|
|
info: true,
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-07 12:42:08 +07:00
|
|
|
return (
|
|
|
|
|
<div className="relative">
|
|
|
|
|
<TitleDashboard title="Kategori" />
|
|
|
|
|
<div className="mb-8 flex items-end justify-between gap-5">
|
|
|
|
|
<div className="flex-1">{/* TODO: Filter */}</div>
|
|
|
|
|
<Button
|
|
|
|
|
as={Link}
|
2025-03-07 14:58:35 +08:00
|
|
|
to="/lg-admin/categories/create"
|
2025-03-07 12:42:08 +07:00
|
|
|
size="lg"
|
2025-03-13 07:29:03 +08:00
|
|
|
className="text-md h-[42px] px-4"
|
2025-03-07 12:42:08 +07:00
|
|
|
>
|
2025-03-14 13:28:15 +08:00
|
|
|
<PlusIcon className="h-8 w-8" /> Buat Kategori
|
2025-03-07 12:42:08 +07:00
|
|
|
</Button>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<UiTable
|
|
|
|
|
data={dataTable}
|
|
|
|
|
columns={dataColumns}
|
|
|
|
|
options={dataOptions}
|
2025-03-07 13:07:28 +07:00
|
|
|
slots={dataSlot}
|
2025-03-07 15:04:22 +08:00
|
|
|
title="Daftar Kategori"
|
2025-03-07 12:42:08 +07:00
|
|
|
/>
|
2025-03-13 11:01:28 +07:00
|
|
|
|
|
|
|
|
<DialogDelete
|
|
|
|
|
selectedId={selectedCategory?.id}
|
|
|
|
|
close={() => setSelectedCategory(undefined)}
|
|
|
|
|
title="Kategori"
|
|
|
|
|
fetcherAction={`/actions/admin/categories/delete/${selectedCategory?.id}`}
|
|
|
|
|
>
|
|
|
|
|
<p>{selectedCategory?.name}</p>
|
|
|
|
|
</DialogDelete>
|
2025-03-07 12:42:08 +07:00
|
|
|
</div>
|
|
|
|
|
)
|
|
|
|
|
}
|