mirror of
https://github.com/ichsankurnia/hats-app.git
synced 2026-12-05 10:56:28 +00:00
279b642a7a
Enforce page access and action-level permissions across admin and task views, including exports and record actions. Move user creation to a client component behind server-side permission checks, standardize data fetching, and remove the deprecated user-role route.
207 lines
8.1 KiB
TypeScript
207 lines
8.1 KiB
TypeScript
'use client'
|
|
|
|
import { ChangeEvent, FC, useEffect, useState } from 'react';
|
|
import { MoreHorizontalIcon } from 'lucide-react';
|
|
import { Table } from 'antd';
|
|
import { TableProps, type ColumnsType } from 'antd/es/table';
|
|
import { PlusCircledIcon } from '@radix-ui/react-icons';
|
|
import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger } from '@/components/ui/dropdown-menu';
|
|
import { Button } from '@/components/ui/button';
|
|
import { Input } from '@/components/ui/input';
|
|
import { useRouter } from 'next/navigation';
|
|
import Link from 'next/link';
|
|
import Image from 'next/image';
|
|
import { IClient } from '@/app/(dashboard)/task/types';
|
|
import { LazyImage } from '@/components/ui/lazy-image';
|
|
import BadgeStatus from '@/components/ui/badge-status';
|
|
import FormClient from '@/app/(dashboard)/task/component/client/FormClient';
|
|
import { ConfirmDeleteWithInput } from '@/components/modal/ConfirmDeleteWithInput';
|
|
|
|
type Props = {
|
|
data: IClient[]
|
|
actions?: string[]
|
|
};
|
|
|
|
const ComponentPage: FC<Props> = ({ data, actions }) => {
|
|
const [filterData, setFilterData] = useState<IClient[]>([])
|
|
const [action, setAction] = useState<'CREATE' | 'EDIT' | 'DELETE' | undefined>(undefined)
|
|
const [selectedData, setSelectedData] = useState<IClient>()
|
|
|
|
const router = useRouter()
|
|
|
|
useEffect(() => {
|
|
if (data) {
|
|
setFilterData(data)
|
|
}
|
|
}, [data])
|
|
|
|
|
|
|
|
const handleSearch = (e: ChangeEvent<HTMLInputElement>) => {
|
|
const text = e.target.value
|
|
if (text) {
|
|
const newData = [...data!].filter(item => (
|
|
item.name?.toLowerCase().includes(text.toLowerCase()) ||
|
|
item.code?.toLowerCase().includes(text.toLowerCase()) ||
|
|
item.description?.toLowerCase().includes(text.toLowerCase()) ||
|
|
item.status_name?.toLowerCase().includes(text.toLowerCase())
|
|
))
|
|
setFilterData(newData)
|
|
} else {
|
|
setFilterData(data)
|
|
}
|
|
}
|
|
|
|
const handleClose = () => {
|
|
setAction(undefined)
|
|
setSelectedData(undefined)
|
|
}
|
|
|
|
const Columns: ColumnsType<IClient> = [
|
|
{
|
|
title: 'No',
|
|
dataIndex: 'key',
|
|
render: (text, record, index) => index + 1
|
|
},
|
|
{
|
|
title: 'Code',
|
|
dataIndex: 'code',
|
|
sorter: (a, b) => a.code.localeCompare(b.code)
|
|
},
|
|
{
|
|
title: 'Client Name',
|
|
dataIndex: 'name',
|
|
sorter: (a, b) => a.name.localeCompare(b.name),
|
|
render: (text, record) => (
|
|
<div className='flex items-center space-x-2.5'>
|
|
{record.photo ?
|
|
<LazyImage className='w-8 h-8 rounded-md border dark:border-zinc-800 object-cover' alt='logo' src={record.photo} />
|
|
:
|
|
<Image alt='avatar' height={0} width={0} quality={50} src='/assets/ilustration.png' className='w-8 h-8 rounded-md border dark:border-zinc-800 object-cover' />
|
|
}
|
|
<p>{text}</p>
|
|
</div>
|
|
)
|
|
},
|
|
{
|
|
title: 'Description',
|
|
dataIndex: 'description',
|
|
sorter: (a, b) => a.description.localeCompare(b.description)
|
|
},
|
|
{
|
|
title: 'Total Project',
|
|
dataIndex: 'total_project',
|
|
align: 'right'
|
|
},
|
|
{
|
|
title: 'Total WO',
|
|
dataIndex: 'total_work_order',
|
|
align: 'right'
|
|
},
|
|
{
|
|
title: 'Total User Assigned',
|
|
dataIndex: 'total_user_assigned',
|
|
align: 'right'
|
|
},
|
|
{
|
|
title: 'Status',
|
|
dataIndex: 'status_name',
|
|
filters: [
|
|
{
|
|
text: 'ACTIVE',
|
|
value: 'A',
|
|
},
|
|
{
|
|
text: 'INACTIVE',
|
|
value: 'N',
|
|
},
|
|
{
|
|
text: 'DELETED',
|
|
value: 'X',
|
|
},
|
|
],
|
|
onFilter: (value, record) => record.status_name.indexOf(value as string) === 0,
|
|
align: 'center',
|
|
render: (text, record) => (
|
|
<div className='flex justify-center text-white font-semibold text-xs'>
|
|
<BadgeStatus status={record.status_name} />
|
|
</div>
|
|
)
|
|
},
|
|
{
|
|
title: 'Action',
|
|
key: 'action',
|
|
fixed: 'right',
|
|
render: (_, record) => (actions?.includes('UPDATE') || actions?.includes('EDIT') || actions?.includes('DELETE')) && (
|
|
<DropdownMenu>
|
|
<DropdownMenuTrigger asChild>
|
|
<Button variant='ghost' size="icon" onClick={e => e?.stopPropagation()}>
|
|
<MoreHorizontalIcon />
|
|
<span className="sr-only">Action Button</span>
|
|
</Button>
|
|
</DropdownMenuTrigger>
|
|
<DropdownMenuContent align="end" onClick={e => e?.stopPropagation()}>
|
|
<DropdownMenuItem><Link href={`/task/client-detail/${record.id}`} className='w-full'>View Details</Link></DropdownMenuItem>
|
|
{(actions.includes('UPDATE') || actions.includes('EDIT')) && (
|
|
<DropdownMenuItem onSelect={() => { setAction('EDIT'); setSelectedData(record) }}>Edit Client</DropdownMenuItem>
|
|
)}
|
|
{(actions.includes('DELETE')) && (
|
|
<DropdownMenuItem onSelect={() => { setSelectedData(record); setAction('DELETE') }} className="text-destructive" onClick={(e) => e.stopPropagation()}>
|
|
Delete Client
|
|
</DropdownMenuItem>
|
|
)}
|
|
</DropdownMenuContent>
|
|
</DropdownMenu>
|
|
),
|
|
},
|
|
];
|
|
|
|
const tableProps: TableProps<IClient> = {
|
|
bordered: false,
|
|
size: 'large',
|
|
showHeader: true,
|
|
pagination: {
|
|
pageSizeOptions: [5, 10, 20, 50],
|
|
defaultPageSize: 10,
|
|
showSizeChanger: true,
|
|
showQuickJumper: true,
|
|
size: 'middle' as any
|
|
},
|
|
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 data..." className="h-8 w-full sm:w-62.5" onChange={handleSearch} />
|
|
</div>
|
|
{(actions?.includes('CREATE')) && (
|
|
<Button size="sm" className="ml-auto h-8 w-full sm:w-max text-his" variant='ghost' onClick={() => { setAction('CREATE'); setSelectedData(undefined) }}>
|
|
<PlusCircledIcon className="mr-2 h-4 w-4" />
|
|
Create New Client
|
|
</Button>
|
|
)}
|
|
</div>
|
|
<Table
|
|
{...tableProps}
|
|
rowKey='id'
|
|
columns={Columns}
|
|
dataSource={filterData || []}
|
|
style={{ cursor: "pointer" }}
|
|
/>
|
|
</div>
|
|
|
|
{(action === 'CREATE' || action === 'EDIT') && <FormClient data={selectedData} onCancel={handleClose} onSuccess={() => { router.refresh(); handleClose() }} />}
|
|
|
|
{action === 'DELETE' ? <ConfirmDeleteWithInput endpoint={`/client/${selectedData?.id}`} onCancel={handleClose} onSuccess={() => { router.refresh(); handleClose() }}
|
|
title={selectedData?.name!} description={`This action cannot be undone. This will permanently delete and remove client ${selectedData?.name} from our servers.`}
|
|
titleButton={`I understand the consequences, delete this client`}
|
|
/> : null}
|
|
</>
|
|
);
|
|
}
|
|
|
|
export default ComponentPage; |