mirror of
https://github.com/ichsankurnia/hats-app.git
synced 2026-12-05 11:06:29 +00:00
155 lines
5.5 KiB
TypeScript
155 lines
5.5 KiB
TypeScript
'use client'
|
|
|
|
import { ChangeEvent, FC, useEffect, useState } from 'react';
|
|
import { Table } from 'antd';
|
|
import { TableProps, type ColumnsType } from 'antd/es/table';
|
|
import { Input } from '@/components/ui/input';
|
|
import dayjs from 'dayjs';
|
|
import { Badge } from '@/components/ui/badge';
|
|
import DetailModalComponent from './Detail';
|
|
import { getVariantWorkingStatus } from '@/lib/helper';
|
|
|
|
type Props = {
|
|
data: any[]
|
|
};
|
|
|
|
const ComponentPage: FC<Props> = ({ data }) => {
|
|
const [filterData, setFilterData] = useState<any[]>([])
|
|
const [selectedData, setSelectedData] = useState<any>(undefined)
|
|
const [showDetail, setShowDetail] = useState(false)
|
|
|
|
useEffect(() => {
|
|
if (data) {
|
|
setFilterData(data)
|
|
}
|
|
}, [data])
|
|
|
|
const handleSearch = (e: ChangeEvent<HTMLInputElement>) => {
|
|
const text = e.target.value
|
|
if (text) {
|
|
const newData = [...data!].filter(item => (
|
|
item.nik?.toLowerCase().includes(text.toLowerCase()) ||
|
|
item.fullname?.toLowerCase().includes(text.toLowerCase()) ||
|
|
item.nickname?.toLowerCase().includes(text.toLowerCase()) ||
|
|
item.category_name?.toLowerCase().includes(text.toLowerCase()) ||
|
|
item.approved_by?.toLowerCase().includes(text.toLowerCase()) ||
|
|
item.rejected_by?.toLowerCase().includes(text.toLowerCase())
|
|
))
|
|
setFilterData(newData)
|
|
} else {
|
|
setFilterData(data)
|
|
}
|
|
}
|
|
|
|
const Columns: ColumnsType<any> = [
|
|
{
|
|
title: 'NIK',
|
|
dataIndex: 'nik',
|
|
},
|
|
{
|
|
title: 'Name',
|
|
dataIndex: 'fullname',
|
|
sorter: (a, b) => a.fullname.localeCompare(b.fullname)
|
|
},
|
|
{
|
|
title: 'Category',
|
|
dataIndex: 'category_name',
|
|
sorter: (a, b) => a.category_name.localeCompare(b.category_name),
|
|
render: (val) => <Badge variant={getVariantWorkingStatus(val)}>{val}</Badge>
|
|
},
|
|
{
|
|
title: 'Star Date',
|
|
dataIndex: 'start_date',
|
|
sorter: ((a, b) => (dayjs(a.start_date).isAfter(dayjs(b.start_date)) ? 1 : -1)),
|
|
render: (val) => dayjs(val).format('DD-MM-YYYY')
|
|
},
|
|
{
|
|
title: 'End Date',
|
|
dataIndex: 'end_date',
|
|
sorter: ((a, b) => (dayjs(a.end_date).isAfter(dayjs(b.end_date)) ? 1 : -1)),
|
|
render: (val) => dayjs(val).format('DD-MM-YYYY')
|
|
},
|
|
{
|
|
title: 'Return to Work',
|
|
dataIndex: 'return_to_work_date',
|
|
sorter: ((a, b) => (dayjs(a.return_to_work_date).isAfter(dayjs(b.return_to_work_date)) ? 1 : -1)),
|
|
render: (val) => dayjs(val).format('DD-MM-YYYY')
|
|
},
|
|
{
|
|
title: 'Apply at',
|
|
dataIndex: 'created_at',
|
|
sorter: ((a, b) => (dayjs(a.created_at).isAfter(dayjs(b.created_at)) ? 1 : -1)),
|
|
render: (val) => dayjs(val).format('DD-MM-YYYY HH:mm')
|
|
},
|
|
{
|
|
title: 'Approval Status',
|
|
dataIndex: 'approval_status',
|
|
render: (val, record) => record.approved_at ? <Badge variant='secondary'>approved</Badge> : record.rejected_at ? <Badge variant='destructive'>rejected</Badge> : <Badge>need to be approve</Badge>
|
|
},
|
|
{
|
|
title: 'Approval by',
|
|
dataIndex: 'approved_by',
|
|
sorter: (a, b) => a.approved_by?.localeCompare(b.approved_by),
|
|
render: (val, record) => val ? val : record.rejected_by ? record.rejected_by : null
|
|
},
|
|
{
|
|
title: 'Approval at',
|
|
dataIndex: 'approved_at',
|
|
render: (val, record) => {
|
|
const date = val ? val : record.rejected_at ? record.rejected_at : null
|
|
return date ? dayjs(date).format('DD-MM-YYYY HH:mm') : null
|
|
}
|
|
},
|
|
];
|
|
|
|
const tableProps: TableProps<any> = {
|
|
size: 'small',
|
|
bordered: false,
|
|
showHeader: true,
|
|
pagination: {
|
|
pageSizeOptions: [5, 10, 20, 50],
|
|
defaultPageSize: 15,
|
|
showSizeChanger: true,
|
|
showQuickJumper: true,
|
|
// size: 'default'
|
|
},
|
|
scroll: { x: 'max-content' },
|
|
tableLayout: 'auto',
|
|
};
|
|
|
|
return (
|
|
<>
|
|
<div className='flex flex-col space-y-4'>
|
|
<div className="flex items-center justify-between">
|
|
<div className="flex flex-1 items-center space-x-2">
|
|
<Input placeholder="Search data..." className="h-8 w-full sm:w-[250px]" onChange={handleSearch} />
|
|
</div>
|
|
</div>
|
|
<Table
|
|
{...tableProps}
|
|
onRow={(record, rowIndex) => {
|
|
return {
|
|
onClick: event => {
|
|
setSelectedData(record); setShowDetail(true)
|
|
},
|
|
};
|
|
}}
|
|
style={{ cursor: "pointer" }}
|
|
rowKey='application_form_code'
|
|
columns={Columns}
|
|
dataSource={filterData || []}
|
|
/>
|
|
</div>
|
|
|
|
{showDetail &&
|
|
<DetailModalComponent
|
|
open={showDetail} onOpenChange={setShowDetail}
|
|
data={selectedData!}
|
|
onClose={() => { setSelectedData(undefined); setShowDetail(false) }}
|
|
/>
|
|
}
|
|
</>
|
|
);
|
|
}
|
|
|
|
export default ComponentPage; |