mirror of
https://github.com/ichsankurnia/hats-app.git
synced 2026-12-06 21:26:29 +00:00
update
This commit is contained in:
@@ -1,43 +1,62 @@
|
||||
'use client'
|
||||
|
||||
import { ChangeEvent, FC, useEffect, useState } from 'react';
|
||||
import { ChangeEvent, FC, Suspense, useCallback, useEffect, useMemo, useState } from 'react';
|
||||
import { Table } from 'antd';
|
||||
import { TableProps } from 'antd/es/table';
|
||||
import { Input } from '@/components/ui/input';
|
||||
import { IHistory } from '@/app/(dashboard)/history/components/type';
|
||||
import ColumnsHistory from './Columns';
|
||||
import { CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card';
|
||||
import LazyLoading from '@/components/loader/LazyLoading';
|
||||
import ExportComponent from './Export';
|
||||
import useDebounce from '@/hooks/useDebounce';
|
||||
|
||||
type Props = {
|
||||
data: IHistory[]
|
||||
};
|
||||
|
||||
const ComponentPage: FC<Props> = ({ data }) => {
|
||||
const [filterData, setFilterData] = useState<IHistory[]>([])
|
||||
const [currentDataTable, setCurrentDataTable] = useState<IHistory[]>([])
|
||||
const [searchText, setSearchText] = useState<string>('')
|
||||
|
||||
useEffect(() => {
|
||||
if (data) {
|
||||
setFilterData(data)
|
||||
}
|
||||
}, [data])
|
||||
const debouncedSearchText = useDebounce(searchText, 300)
|
||||
|
||||
const searchFilterLogic = useCallback((item: IHistory, text: string) => {
|
||||
const lowerText = text.toLowerCase();
|
||||
// Use optional chaining carefully, and provide fallback empty strings to avoid crashes
|
||||
return (
|
||||
(item.user?.fullname || '').toLowerCase().includes(lowerText) ||
|
||||
(item.working_status || '').toLowerCase().includes(lowerText) ||
|
||||
(item.device?.name || '').toLowerCase().includes(lowerText) ||
|
||||
(item.location_name || '').toLowerCase().includes(lowerText) ||
|
||||
(item.note || '').toLowerCase().includes(lowerText) ||
|
||||
(item.presence_checkout?.location_name || '').toLowerCase().includes(lowerText) ||
|
||||
(item.presence_checkout?.note || '').toLowerCase().includes(lowerText)
|
||||
);
|
||||
}, []);
|
||||
|
||||
const handleSearch = (e: ChangeEvent<HTMLInputElement>) => {
|
||||
const text = e.target.value
|
||||
if (text) {
|
||||
const newData = [...data!].filter(item => (
|
||||
item.user.fullname?.toLowerCase().includes(text.toLowerCase()) ||
|
||||
item.working_status?.toLowerCase().includes(text.toLowerCase()) ||
|
||||
item.device.name?.toLowerCase().includes(text.toLowerCase()) ||
|
||||
item.location_name?.toLowerCase().includes(text.toLowerCase()) ||
|
||||
item.note?.toLowerCase().includes(text.toLowerCase()) ||
|
||||
item.presence_checkout?.location_name?.toLowerCase().includes(text.toLowerCase()) ||
|
||||
item.presence_checkout?.note?.toLowerCase().includes(text.toLowerCase())
|
||||
))
|
||||
setFilterData(newData)
|
||||
} else {
|
||||
setFilterData(data)
|
||||
}
|
||||
setSearchText(e.target.value);
|
||||
}
|
||||
|
||||
const filterData = useMemo(() => {
|
||||
if (!data) return [];
|
||||
if (!debouncedSearchText) return data;
|
||||
|
||||
return data.filter(item => searchFilterLogic(item, debouncedSearchText));
|
||||
}, [data, debouncedSearchText, searchFilterLogic]);
|
||||
|
||||
const exportData = useMemo(() => {
|
||||
const baseExportData = currentDataTable.length > 0 ? currentDataTable : (data || []);
|
||||
|
||||
// We use the raw searchText here or debounced? Usually debounced is better so they don't export mid-type
|
||||
if (debouncedSearchText) {
|
||||
return baseExportData.filter(item => searchFilterLogic(item, debouncedSearchText));
|
||||
}
|
||||
return baseExportData;
|
||||
}, [currentDataTable, data, debouncedSearchText, searchFilterLogic]);
|
||||
|
||||
|
||||
const tableProps: TableProps<IHistory> = {
|
||||
size: 'small',
|
||||
bordered: false,
|
||||
@@ -53,23 +72,33 @@ const ComponentPage: FC<Props> = ({ data }) => {
|
||||
tableLayout: 'auto',
|
||||
};
|
||||
|
||||
// console.log(data)
|
||||
|
||||
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 report..." className="h-8 w-full sm:w-[250px]" onChange={handleSearch} />
|
||||
</div>
|
||||
<CardHeader className='flex flex-col md:flex-row items-center justify-between space-y-5 sm:space-y-0'>
|
||||
<div>
|
||||
<CardTitle>Attendance History</CardTitle>
|
||||
<CardDescription className='mt-1 sm:block hidden'>Here's a list of all employee attendance history!</CardDescription>
|
||||
</div>
|
||||
<Table
|
||||
{...tableProps}
|
||||
rowKey='presence_checkin_code'
|
||||
columns={ColumnsHistory}
|
||||
dataSource={filterData || []}
|
||||
/>
|
||||
</div>
|
||||
<Suspense fallback={<LazyLoading />}>
|
||||
<ExportComponent data={exportData} />
|
||||
</Suspense>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<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 report..." className="h-8 w-full sm:w-[250px]" onChange={handleSearch} />
|
||||
</div>
|
||||
</div>
|
||||
<Table
|
||||
{...tableProps}
|
||||
rowKey='presence_checkin_code'
|
||||
columns={ColumnsHistory}
|
||||
onChange={(pagination, filters, sorter, extra) => setCurrentDataTable(extra.currentDataSource as IHistory[])}
|
||||
dataSource={filterData || []}
|
||||
/>
|
||||
</div>
|
||||
</CardContent>
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,11 +1,10 @@
|
||||
import { type Metadata } from 'next';
|
||||
import { FC, Suspense, lazy } from 'react';
|
||||
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card';
|
||||
import { Card } from '@/components/ui/card';
|
||||
import LazyLoading from '@/components/loader/LazyLoading';
|
||||
import dayjs from 'dayjs';
|
||||
import { fetchDataWithPost } from '@/lib/fetchData';
|
||||
|
||||
const ExportComponent = lazy(() => import('./component/Export'));
|
||||
const ComponentPage = lazy(() => import('./component'));
|
||||
|
||||
|
||||
@@ -27,22 +26,11 @@ const MonthlyPresencePage: FC<Props> = async ({ searchParams }) => {
|
||||
<>
|
||||
<div className="flex flex-col">
|
||||
<Card>
|
||||
<CardHeader className='flex flex-col md:flex-row items-center justify-between space-y-5 sm:space-y-0'>
|
||||
<div>
|
||||
<CardTitle>Attendance History</CardTitle>
|
||||
<CardDescription className='mt-1 sm:block hidden'>Here's a list of all employee attendance history!</CardDescription>
|
||||
</div>
|
||||
<Suspense fallback={<LazyLoading />}>
|
||||
<ExportComponent data={data.history} />
|
||||
</Suspense>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<Suspense fallback={<LazyLoading />}>
|
||||
<ComponentPage
|
||||
data={data.history}
|
||||
/>
|
||||
</Suspense>
|
||||
</CardContent>
|
||||
<Suspense fallback={<div className='flex justify-center items-center w-full h-[40vh]'><LazyLoading /></div>}>
|
||||
<ComponentPage
|
||||
data={data.history}
|
||||
/>
|
||||
</Suspense>
|
||||
</Card>
|
||||
</div>
|
||||
</>
|
||||
|
||||
Reference in New Issue
Block a user