mirror of
https://github.com/ichsankurnia/hats-app.git
synced 2026-12-05 11:06:29 +00:00
186 lines
6.5 KiB
TypeScript
186 lines
6.5 KiB
TypeScript
'use client'
|
|
|
|
import { ChangeEvent, FC, useEffect, useState } from 'react';
|
|
import { MoreHorizontalIcon } from 'lucide-react';
|
|
import { Table } from 'antd';
|
|
import dayjs from 'dayjs';
|
|
import { TableProps, type ColumnsType } from 'antd/es/table';
|
|
import { PlusCircledIcon } from '@radix-ui/react-icons';
|
|
import useAxiosAuth from '@/lib/axios/useAxiosAuth';
|
|
import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger } from '@/components/ui/dropdown-menu';
|
|
import { Button } from '@/components/ui/button';
|
|
import { AlertDialog, AlertDialogTrigger } from '@/components/ui/alert-dialog';
|
|
import { Input } from '@/components/ui/input';
|
|
import { Dialog } from '@/components/ui/dialog';
|
|
import ComponentForm from './Form';
|
|
import ConfirmAsync from './ConfirmAsync';
|
|
import { type IDayOff } from './type';
|
|
|
|
type Props = {
|
|
data: IDayOff[]
|
|
};
|
|
|
|
const ComponentPage: FC<Props> = ({ data }) => {
|
|
const [filterData, setFilterData] = useState<IDayOff[]>([])
|
|
const [selectedData, setSelectedData] = useState<IDayOff | undefined>(undefined)
|
|
const [showFormDialog, setShowFormDialog] = useState(false)
|
|
|
|
const axios = useAxiosAuth()
|
|
|
|
useEffect(() => {
|
|
if (data) {
|
|
setFilterData(data)
|
|
}
|
|
}, [data])
|
|
|
|
const handleDelete = (id: string | number) => {
|
|
return axios.delete(`/dayoff/${id}`).then(res => res.data)
|
|
}
|
|
|
|
const handleResetForm = () => {
|
|
setSelectedData(undefined)
|
|
setShowFormDialog(false)
|
|
}
|
|
|
|
const handleSearch = (e: ChangeEvent<HTMLInputElement>) => {
|
|
const text = e.target.value
|
|
if (text) {
|
|
const newData = [...data!].filter(item => (
|
|
item.name?.toLowerCase().includes(text.toLowerCase()) ||
|
|
item.description?.toLowerCase().includes(text.toLowerCase())
|
|
))
|
|
setFilterData(newData)
|
|
} else {
|
|
setFilterData(data)
|
|
}
|
|
}
|
|
|
|
const Columns: ColumnsType<IDayOff> = [
|
|
{
|
|
title: 'Num',
|
|
dataIndex: 'key',
|
|
render: (text, record, index) => index + 1
|
|
},
|
|
{
|
|
title: 'Date',
|
|
dataIndex: 'date',
|
|
sorter: ((a, b) => (dayjs(a.date as string).isAfter(dayjs(b.date as string)) ? 1 : -1)),
|
|
render: (val) => dayjs(val).format('ddd, MMM DD, YYYY')
|
|
},
|
|
{
|
|
title: 'Name',
|
|
dataIndex: 'name',
|
|
sorter: (a, b) => a.name.localeCompare(b.name)
|
|
},
|
|
{
|
|
title: 'Status',
|
|
dataIndex: 'status',
|
|
filters: [
|
|
{
|
|
text: 'ACTIVE',
|
|
value: 'A',
|
|
},
|
|
{
|
|
text: 'INACTIVE',
|
|
value: 'N',
|
|
},
|
|
{
|
|
text: 'DELETED',
|
|
value: 'X',
|
|
},
|
|
],
|
|
onFilter: (value, record) => record.status!.code.indexOf(value as string) === 0,
|
|
align: 'center',
|
|
render: (text, record) => (
|
|
<div className='flex justify-center text-white font-semibold text-xs'>
|
|
{record.status.code === 'A' ?
|
|
<div className="px-2 py-1.5 rounded-full bg-green-500 w-24">{record.status.name}</div>
|
|
:
|
|
<div className="px-2 py-1.5 rounded-full bg-rose-500 w-24">{record.status.name}</div>
|
|
}
|
|
</div>
|
|
)
|
|
},
|
|
{
|
|
title: 'Description',
|
|
dataIndex: 'description',
|
|
},
|
|
{
|
|
title: 'Action',
|
|
key: 'action',
|
|
render: (_, record) => (
|
|
<AlertDialog>
|
|
<DropdownMenu>
|
|
<DropdownMenuTrigger asChild>
|
|
<Button variant='ghost' size="icon">
|
|
<MoreHorizontalIcon />
|
|
<span className="sr-only">Action Button</span>
|
|
</Button>
|
|
</DropdownMenuTrigger>
|
|
<DropdownMenuContent align="end">
|
|
<DropdownMenuItem onClick={() => { setShowFormDialog(true); setSelectedData(record) }} >Edit</DropdownMenuItem>
|
|
<AlertDialogTrigger asChild>
|
|
<DropdownMenuItem>Delete</DropdownMenuItem>
|
|
</AlertDialogTrigger>
|
|
</DropdownMenuContent>
|
|
</DropdownMenu>
|
|
<ConfirmAsync
|
|
onConfirm={() => handleDelete(record.id)}
|
|
onOkSuccess={() => { }}
|
|
/>
|
|
</AlertDialog>
|
|
),
|
|
},
|
|
];
|
|
|
|
const tableProps: TableProps<IDayOff> = {
|
|
bordered: false,
|
|
size: 'small',
|
|
// expandable,
|
|
showHeader: true,
|
|
pagination: {
|
|
pageSizeOptions: [5, 10, 20, 50],
|
|
defaultPageSize: 10,
|
|
showSizeChanger: true,
|
|
showQuickJumper: true,
|
|
// size: 'default'
|
|
},
|
|
scroll: { x: 'max-content' },
|
|
tableLayout: 'auto',
|
|
};
|
|
|
|
return (
|
|
<>
|
|
<div className='flex flex-col space-y-4'>
|
|
<div className="flex flex-col sm:flex-row items-start sm:items-center justify-between space-y-2 sm:space-y-0">
|
|
<div className="flex flex-1 items-center space-x-2">
|
|
<Input placeholder="Search log name..." className="h-8 w-full sm:w-[250px]" onChange={handleSearch} />
|
|
</div>
|
|
<Button size="sm" variant='ghost' className="ml-auto h-8 w-full sm:w-max text-his" onClick={() => setShowFormDialog(true)}>
|
|
<PlusCircledIcon className="mr-2 h-4 w-4" />
|
|
Create New
|
|
</Button>
|
|
</div>
|
|
<Table
|
|
{...tableProps}
|
|
rowKey='id'
|
|
columns={Columns}
|
|
dataSource={filterData || []}
|
|
/>
|
|
</div>
|
|
|
|
<Dialog open={showFormDialog} onOpenChange={(state) => {
|
|
if (state === false) setSelectedData(undefined)
|
|
setShowFormDialog(state)
|
|
}}>
|
|
<ComponentForm
|
|
data={selectedData}
|
|
isUpdate={selectedData !== undefined}
|
|
onCancel={handleResetForm}
|
|
/>
|
|
</Dialog>
|
|
</>
|
|
);
|
|
}
|
|
|
|
export default ComponentPage; |