mirror of
https://github.com/ichsankurnia/hats-app.git
synced 2026-12-06 22:26:27 +00:00
3eabe762bf
Add new admin Project Assignment area with Client, End User, and Project Management pages and components (tables, Export buttons, lazy-loaded pages). Introduce FormProject for creating/editing projects and export utilities to download Excel for each module. Update task types: IEndUser now includes code and a status object. Clean up FormClient by removing unused imports and inline end-user modal usage. Make BadgeStatus robust to missing status values. Add "Project Assignment" entry to the sidebar and import FolderGit2.
203 lines
8.0 KiB
TypeScript
203 lines
8.0 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 BadgeStatus from '@/components/ui/badge-status';
|
|
import { ConfirmDeleteWithInput } from '@/components/modal/ConfirmDeleteWithInput';
|
|
import { IProjectAssignment } from '@/app/(dashboard)/task/type.assignment';
|
|
import dayjs from 'dayjs';
|
|
import FormProject from './FormProject';
|
|
|
|
type Props = {
|
|
data: IProjectAssignment[]
|
|
};
|
|
|
|
const ComponentPage: FC<Props> = ({ data }) => {
|
|
const [filterData, setFilterData] = useState<IProjectAssignment[]>([])
|
|
const [action, setAction] = useState<'CREATE' | 'EDIT' | 'DELETE' | undefined>(undefined)
|
|
const [selectedData, setSelectedData] = useState<IProjectAssignment>()
|
|
|
|
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.project?.name?.toLowerCase().includes(text.toLowerCase()) ||
|
|
item.project?.code?.toLowerCase()?.includes(text.toLowerCase()) ||
|
|
item.project?.description?.toLowerCase().includes(text.toLowerCase()) ||
|
|
item.client.name?.toLowerCase().includes(text.toLowerCase()) ||
|
|
item.end_user.name?.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<IProjectAssignment> = [
|
|
{
|
|
title: 'No',
|
|
dataIndex: 'key',
|
|
render: (text, record, index) => index + 1
|
|
},
|
|
{
|
|
title: 'Code',
|
|
dataIndex: 'project.code',
|
|
sorter: (a, b) => a.project?.code?.localeCompare(b.project?.code!)!,
|
|
render: (_, record) => <span>{record.project?.code}</span>
|
|
},
|
|
{
|
|
title: 'Project Name',
|
|
dataIndex: 'project.name',
|
|
sorter: (a, b) => a.project?.name.localeCompare(b.project?.name!)!,
|
|
render: (_, record) => <span>{record.project?.name}</span>
|
|
},
|
|
{
|
|
title: 'Client',
|
|
dataIndex: 'client.name',
|
|
sorter: (a, b) => a.client?.name.localeCompare(b.client?.name!)!,
|
|
render: (_, record) => <span>{record.client?.name}</span>
|
|
},
|
|
{
|
|
title: 'End User',
|
|
dataIndex: 'end_user.name',
|
|
sorter: (a, b) => a.end_user?.name.localeCompare(b.end_user?.name!)!,
|
|
render: (_, record) => <span>{record.end_user?.name}</span>
|
|
},
|
|
{
|
|
title: 'Year',
|
|
dataIndex: 'project.year',
|
|
render: (_, record) => <span>{record.project?.year}</span>
|
|
},
|
|
{
|
|
title: 'Start Date',
|
|
dataIndex: 'project.start_date',
|
|
render: (_, record) => record.project?.start_date ? <span>{dayjs(record.project.start_date).format('DD MMM YYYY')}</span> : <span>-</span>
|
|
},
|
|
{
|
|
title: 'End Date',
|
|
dataIndex: 'project.end_date',
|
|
render: (_, record) => record.project?.end_date ? <span>{dayjs(record.project.end_date).format('DD MMM YYYY')}</span> : <span>-</span>
|
|
},
|
|
{
|
|
title: 'Description',
|
|
dataIndex: 'project.description',
|
|
render: (_, record) => <span>{record.project?.description}</span>
|
|
},
|
|
{
|
|
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) => (
|
|
<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 onSelect={() => { setAction('EDIT'); setSelectedData(record) }}>Edit Project</DropdownMenuItem>
|
|
<DropdownMenuItem onSelect={() => { setSelectedData(record); setAction('DELETE') }} className="text-destructive" onClick={(e) => e.stopPropagation()}>
|
|
Delete Project
|
|
</DropdownMenuItem>
|
|
</DropdownMenuContent>
|
|
</DropdownMenu>
|
|
),
|
|
},
|
|
];
|
|
|
|
const tableProps: TableProps<IProjectAssignment> = {
|
|
bordered: false,
|
|
size: 'small',
|
|
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>
|
|
<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 Project
|
|
</Button>
|
|
</div>
|
|
<Table
|
|
{...tableProps}
|
|
rowKey='id'
|
|
columns={Columns}
|
|
dataSource={filterData || []}
|
|
style={{ cursor: "pointer" }}
|
|
/>
|
|
</div>
|
|
|
|
{(action === 'CREATE' || action === 'EDIT') && <FormProject data={selectedData} onCancel={handleClose} onSuccess={() => { router.refresh(); handleClose() }} />}
|
|
|
|
{action === 'DELETE' ? <ConfirmDeleteWithInput endpoint={`/project/${selectedData?.id}`} onCancel={handleClose} onSuccess={() => { router.refresh(); handleClose() }}
|
|
title={selectedData?.project?.name!} description={`This action cannot be undone. This will permanently delete and remove project ${selectedData?.project?.name} from our servers.`}
|
|
titleButton={`I understand the consequences, delete this project`}
|
|
/> : null}
|
|
</>
|
|
);
|
|
}
|
|
|
|
export default ComponentPage; |