177 lines
5.1 KiB
TypeScript
Raw Normal View History

import {
Description,
Dialog,
DialogBackdrop,
DialogPanel,
DialogTitle,
} from '@headlessui/react'
import { PencilSquareIcon, TrashIcon } from '@heroicons/react/20/solid'
import type { ConfigColumns } from 'datatables.net-dt'
import type { DataTableSlots } from 'datatables.net-react'
import { useEffect, useState } from 'react'
import toast from 'react-hot-toast'
import { Link, useFetcher, useRouteLoaderData } from 'react-router'
import type { TAdResponse } from '~/apis/common/get-ads'
import { Button } from '~/components/ui/button'
import { UiTable } from '~/components/ui/table'
import { TitleDashboard } from '~/components/ui/title-dashboard'
import type { loader } from '~/routes/_admin.lg-admin._dashboard.advertisements._index'
export const AdvertisementsPage = () => {
const loaderData = useRouteLoaderData<typeof loader>(
'routes/_admin.lg-admin._dashboard.advertisements._index',
)
const { adsData: dataTable } = loaderData || {}
const [selectedId, setSelectedId] = useState<TAdResponse>()
const fetcher = useFetcher()
useEffect(() => {
if (fetcher.data?.success === false) {
toast.error(fetcher.data?.message)
return
}
if (fetcher.data?.success === true) {
setSelectedId(undefined)
toast.success('Banner iklan berhasil dihapus!')
return
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [fetcher.data])
const dataColumns: ConfigColumns[] = [
{
title: 'No',
render: (
_data: unknown,
_type: unknown,
_row: unknown,
meta: { row: number },
) => {
return meta.row + 1
},
},
{ title: 'Banner', data: 'image_url' },
{ title: 'Link', data: 'url' },
{
title: 'Action',
data: 'id',
},
]
const dataSlot: DataTableSlots = {
1: (value: string) => {
return (
<img
src={value}
alt={value}
className="aspect-[150/1] h-[50px] rounded object-contain"
/>
)
},
3: (value: string, _type: unknown, data: TAdResponse) => (
<div className="flex space-x-2">
<Button
as="a"
href={`/lg-admin/advertisements/update/${value}`}
className=""
size="icon"
>
<PencilSquareIcon className="h-4 w-4" />
</Button>
<Button
type="button"
size="icon"
variant="newsDanger"
onClick={() => setSelectedId(data)}
>
<TrashIcon className="h-4 w-4" />
</Button>
</div>
),
}
return (
<div className="relative">
<TitleDashboard title="Banner Iklan" />
<div className="mb-8 flex items-end justify-between gap-5">
<div className="flex-1">{/* TODO: Filter */}</div>
<Button
as={Link}
to="/lg-admin/advertisements/create"
className="text-md h-[42px] rounded-md"
size="lg"
>
Buat Banner Iklan
</Button>
</div>
<UiTable
data={dataTable}
columns={dataColumns}
slots={dataSlot}
title="Daftar Banner Iklan"
/>
<Dialog
open={!!selectedId}
onClose={() => {
if (fetcher.state === 'idle') {
setSelectedId(undefined)
}
}}
className="relative z-50"
transition
>
<DialogBackdrop
className="fixed inset-0 bg-black/50 duration-300 ease-out data-[closed]:opacity-0"
transition
/>
<div className="fixed inset-0 flex w-screen justify-center overflow-y-auto p-0 max-sm:bg-white sm:items-center sm:p-4">
<DialogPanel
transition
className="max-w-lg space-y-6 rounded-lg bg-white p-8 duration-300 ease-out data-[closed]:scale-95 data-[closed]:opacity-0 sm:shadow-lg"
>
<DialogTitle className="relative flex justify-start text-xl font-bold">
Anda akan menghapus banner berikut?
</DialogTitle>
<Description className="space-y-1 text-center text-[#565658]">
<img
src={selectedId?.image_url}
alt={selectedId?.image_url}
className="aspect-[150/1] h-[50px] rounded object-contain"
/>
<Button
as={Link}
to={selectedId?.url || ''}
variant="link"
size="fit"
>
{selectedId?.url}
</Button>
</Description>
<div className="flex justify-end">
<fetcher.Form
method="POST"
action={`/actions/admin/advertisements/delete/${selectedId?.id}`}
className="grid"
>
<Button
type="submit"
variant="newsDanger"
className="text-md h-[42px] rounded-md"
disabled={fetcher.state !== 'idle'}
isLoading={fetcher.state !== 'idle'}
>
Hapus
</Button>
</fetcher.Form>
</div>
</DialogPanel>
</div>
</Dialog>
</div>
)
}