2025-03-19 17:15:46 +07:00
|
|
|
import {
|
|
|
|
|
PencilSquareIcon,
|
|
|
|
|
PlusIcon,
|
|
|
|
|
TrashIcon,
|
|
|
|
|
} 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-19 17:15:46 +07:00
|
|
|
import { useState } from 'react'
|
2025-03-06 09:30:53 +08:00
|
|
|
import { Link, useRouteLoaderData } from 'react-router'
|
2025-02-25 15:45:27 +07:00
|
|
|
|
2025-03-07 09:04:11 +08:00
|
|
|
import type { TCategoryResponse } from '~/apis/common/get-categories'
|
2025-03-19 17:15:46 +07:00
|
|
|
import type { TAuthorResponse, TNewsResponse } from '~/apis/common/get-news'
|
2025-03-07 09:04:11 +08:00
|
|
|
import type { TTagResponse } from '~/apis/common/get-tags'
|
2025-03-19 17:15:46 +07:00
|
|
|
import { DialogDelete } from '~/components/dialog/delete'
|
2025-03-02 20:09:09 +07:00
|
|
|
import { Button } from '~/components/ui/button'
|
2025-03-04 00:21:14 +07:00
|
|
|
import { UiTable } from '~/components/ui/table'
|
2025-02-25 15:45:27 +07:00
|
|
|
import { TitleDashboard } from '~/components/ui/title-dashboard'
|
2025-03-07 12:11:48 +08:00
|
|
|
import type { loader } from '~/routes/_admin.lg-admin._dashboard.contents._index'
|
2025-03-24 15:45:43 +08:00
|
|
|
import { formatDate, formatNumberWithPeriods } from '~/utils/formatter'
|
2025-02-25 15:45:27 +07:00
|
|
|
|
|
|
|
|
export const ContentsPage = () => {
|
2025-03-06 09:30:53 +08:00
|
|
|
const loaderData = useRouteLoaderData<typeof loader>(
|
2025-03-07 12:11:48 +08:00
|
|
|
'routes/_admin.lg-admin._dashboard.contents._index',
|
2025-03-06 09:30:53 +08:00
|
|
|
)
|
2025-03-19 17:15:46 +07:00
|
|
|
const [selectedContent, setSelectedContent] = useState<TNewsResponse>()
|
2025-02-25 15:45:27 +07:00
|
|
|
DataTable.use(DT)
|
2025-03-09 15:31:10 +08:00
|
|
|
const dataTable =
|
|
|
|
|
loaderData?.newsData?.sort(
|
|
|
|
|
(a, b) => new Date(b.live_at).getTime() - new Date(a.live_at).getTime(),
|
|
|
|
|
) || []
|
|
|
|
|
const dataColumns: ConfigColumns[] = [
|
2025-03-06 23:49:12 +07:00
|
|
|
{
|
|
|
|
|
title: 'No',
|
2025-03-07 08:50:04 +08:00
|
|
|
render: (
|
2025-03-09 15:31:10 +08:00
|
|
|
_data: unknown,
|
|
|
|
|
_type: unknown,
|
|
|
|
|
_row: unknown,
|
2025-03-07 08:50:04 +08:00
|
|
|
meta: { row: number },
|
|
|
|
|
) => {
|
2025-03-06 23:49:12 +07:00
|
|
|
return meta.row + 1
|
|
|
|
|
},
|
|
|
|
|
},
|
2025-03-07 08:50:04 +08:00
|
|
|
{
|
2025-03-07 14:40:04 +08:00
|
|
|
title: 'Tanggal Live',
|
2025-03-07 08:50:04 +08:00
|
|
|
data: 'live_at',
|
|
|
|
|
},
|
2025-03-06 23:32:15 +07:00
|
|
|
{
|
2025-03-07 14:43:12 +08:00
|
|
|
title: 'Penulis',
|
2025-03-12 09:47:21 +08:00
|
|
|
data: 'author',
|
2025-03-06 23:32:15 +07:00
|
|
|
},
|
2025-03-24 15:45:43 +08:00
|
|
|
{ title: 'Judul', data: 'title' },
|
2025-02-26 23:26:46 +07:00
|
|
|
{
|
2025-03-07 08:50:04 +08:00
|
|
|
title: 'Kategori',
|
|
|
|
|
data: 'categories',
|
|
|
|
|
},
|
2025-03-24 15:45:43 +08:00
|
|
|
{ title: 'Tag', data: 'tags' },
|
2025-03-07 08:50:04 +08:00
|
|
|
{
|
2025-03-07 10:30:46 +08:00
|
|
|
title: 'Subscription',
|
2025-03-06 09:30:53 +08:00
|
|
|
data: 'is_premium',
|
2025-02-26 23:26:46 +07:00
|
|
|
},
|
2025-03-24 15:45:43 +08:00
|
|
|
{
|
|
|
|
|
title: 'Jumlah Pembaca',
|
|
|
|
|
data: 'views',
|
|
|
|
|
},
|
2025-03-06 19:47:45 +07:00
|
|
|
{
|
|
|
|
|
title: 'Action',
|
2025-03-24 13:42:36 +08:00
|
|
|
data: 'id',
|
2025-03-06 19:47:45 +07:00
|
|
|
},
|
2025-02-25 15:45:27 +07:00
|
|
|
]
|
2025-03-09 15:31:10 +08:00
|
|
|
const dataSlot: DataTableSlots = {
|
2025-03-07 08:50:04 +08:00
|
|
|
1: (value: string) => formatDate(value),
|
2025-03-13 07:01:53 +08:00
|
|
|
2: (value: TAuthorResponse) => (
|
2025-03-24 13:52:31 +08:00
|
|
|
<>
|
2025-03-12 09:47:21 +08:00
|
|
|
<div>{value.name}</div>
|
|
|
|
|
<div className="text-sm text-[#7C7C7C]">ID: {value.id.slice(0, 8)}</div>
|
2025-03-24 13:52:31 +08:00
|
|
|
</>
|
2025-03-09 13:44:04 +08:00
|
|
|
),
|
2025-03-24 15:45:43 +08:00
|
|
|
3: (value: string) => <span className="text-sm">{value}</span>,
|
|
|
|
|
4: (value: TCategoryResponse[]) => (
|
|
|
|
|
<span className="text-xs">
|
|
|
|
|
{value.map((item) => item.name).join(', ')}
|
|
|
|
|
</span>
|
|
|
|
|
),
|
|
|
|
|
5: (value: TTagResponse[]) => (
|
|
|
|
|
<span className="text-xs">
|
|
|
|
|
{value.map((item) => item.name).join(', ')}
|
|
|
|
|
</span>
|
|
|
|
|
),
|
2025-03-07 08:50:04 +08:00
|
|
|
6: (value: string) =>
|
|
|
|
|
value ? (
|
|
|
|
|
<div className="rounded-full bg-[#FFFCAF] px-2 text-center text-[#DBCA6E]">
|
|
|
|
|
Premium
|
|
|
|
|
</div>
|
|
|
|
|
) : (
|
|
|
|
|
<div className="rounded-full bg-[#F5F5F5] px-2 text-center text-[#4C5CA0]">
|
|
|
|
|
Normal
|
|
|
|
|
</div>
|
|
|
|
|
),
|
2025-03-24 15:45:43 +08:00
|
|
|
7: (value: number) => formatNumberWithPeriods(value),
|
|
|
|
|
8: (value: string, _type: unknown, data: TNewsResponse) => (
|
2025-03-19 17:15:46 +07:00
|
|
|
<div className="flex space-x-2">
|
|
|
|
|
<Button
|
|
|
|
|
as="a"
|
|
|
|
|
href={`/lg-admin/contents/update/${encodeURIComponent(value)}`}
|
|
|
|
|
size="icon"
|
|
|
|
|
title="Update Artikel"
|
|
|
|
|
>
|
|
|
|
|
<PencilSquareIcon className="size-4" />
|
|
|
|
|
</Button>
|
|
|
|
|
<Button
|
|
|
|
|
type="button"
|
|
|
|
|
size="icon"
|
|
|
|
|
variant="danger"
|
|
|
|
|
onClick={() => setSelectedContent(data)}
|
|
|
|
|
title="Hapus Artikel"
|
|
|
|
|
>
|
|
|
|
|
<TrashIcon className="size-4" />
|
|
|
|
|
</Button>
|
|
|
|
|
</div>
|
2025-03-07 08:50:04 +08:00
|
|
|
),
|
2025-03-04 00:21:14 +07:00
|
|
|
}
|
2025-03-09 15:31:10 +08:00
|
|
|
const dataOptions: Config = {
|
2025-03-04 00:21:14 +07:00
|
|
|
paging: true,
|
|
|
|
|
searching: true,
|
|
|
|
|
ordering: true,
|
|
|
|
|
info: true,
|
|
|
|
|
}
|
2025-02-25 15:45:27 +07:00
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div className="relative">
|
2025-03-09 14:54:18 +08:00
|
|
|
<TitleDashboard title="Artikel" />
|
2025-03-06 08:45:24 +08:00
|
|
|
<div className="mb-8 flex items-end justify-between gap-5">
|
|
|
|
|
<div className="flex-1">{/* TODO: Filter */}</div>
|
2025-03-04 09:44:09 +08:00
|
|
|
<Button
|
|
|
|
|
as={Link}
|
|
|
|
|
to="/lg-admin/contents/create"
|
|
|
|
|
size="lg"
|
2025-03-13 07:29:03 +08:00
|
|
|
className="text-md h-[42px] px-4"
|
2025-03-04 09:44:09 +08:00
|
|
|
>
|
2025-03-19 17:15:46 +07:00
|
|
|
<PlusIcon className="size-8" /> Buat Artikel
|
2025-03-04 09:44:09 +08:00
|
|
|
</Button>
|
2025-02-27 16:42:36 +07:00
|
|
|
</div>
|
2025-03-04 00:21:14 +07:00
|
|
|
|
|
|
|
|
<UiTable
|
|
|
|
|
data={dataTable}
|
|
|
|
|
columns={dataColumns}
|
|
|
|
|
slots={dataSlot}
|
|
|
|
|
options={dataOptions}
|
2025-03-09 14:54:18 +08:00
|
|
|
title="Daftar Artikel"
|
2025-03-04 00:21:14 +07:00
|
|
|
/>
|
2025-03-19 17:15:46 +07:00
|
|
|
|
|
|
|
|
<DialogDelete
|
|
|
|
|
selectedId={selectedContent?.id}
|
|
|
|
|
close={() => setSelectedContent(undefined)}
|
|
|
|
|
title="Artikel"
|
|
|
|
|
fetcherAction={`/actions/admin/contents/delete/${selectedContent?.id}`}
|
|
|
|
|
>
|
|
|
|
|
<p>{selectedContent?.title}</p>
|
|
|
|
|
</DialogDelete>
|
2025-02-25 15:45:27 +07:00
|
|
|
</div>
|
|
|
|
|
)
|
|
|
|
|
}
|