Files
hats-app/app/(dashboard)/history/attendance/DataTable.tsx
T
2025-12-19 13:57:55 +07:00

177 lines
11 KiB
TypeScript

"use client"
import { Fragment, useMemo } from "react"
import dayjs from "dayjs"
import { useSearchParams } from "next/navigation"
import {
Table, TableBody, TableCell, TableHead, TableHeader, TableRow,
} from "@/components/ui/table"
import { Badge } from "@/components/ui/badge"
import Pagination from "@/components/pagination"
import { Dialog, DialogContent, DialogTrigger } from "@/components/ui/dialog"
import { getVariantApproved } from "@/lib/helper"
import { IHistory } from "../components/type"
import SortFilter from "../components/SortFilter"
import { CircleCheck } from "lucide-react"
type Props = {
data: IHistory[]
}
export default function DataTable({ data }: Props) {
const searchParams = useSearchParams();
const currentPage = Number(searchParams!.get('page') as string) || 1;
const pageSize = Number(searchParams!.get('limit') as string) || 10;
const savedSort = searchParams!.get('sort') as string || ''
const currentTableData = useMemo(() => {
const newData = [...data]
if (savedSort && savedSort === 'asc') {
newData.sort((a, b) => (dayjs(a.working_hours_start).isAfter(dayjs(b.working_hours_start)) ? 1 : -1))
}
const firstPageIndex = (currentPage - 1) * pageSize;
const lastPageIndex = firstPageIndex + pageSize;
return newData.slice(firstPageIndex, lastPageIndex);
}, [currentPage, pageSize, savedSort, data]);
// console.log(currentTableData)
const getTotalRows = () => {
let rowCount = 0;
data.forEach(entry => {
if (entry.presence_checkout !== null) {
rowCount += 2;
} else {
rowCount += 1;
}
});
return rowCount
}
// console.log(data)
const handleOpenMaps = (lat: any, long: any) => {
const url = `http://maps.google.com/maps?q=${lat}+${long}`
window.open(url, '_blank');
}
// console.log(data)
return (
<div className="w-full pt-2">
<SortFilter data={data} />
<div className="rounded-md border border-border mb-5">
<Table>
<TableHeader>
<TableRow >
<TableHead>Type</TableHead>
<TableHead>Date</TableHead>
<TableHead>Time</TableHead>
<TableHead>Time Code</TableHead>
<TableHead>Location</TableHead>
<TableHead>Device</TableHead>
<TableHead>Area</TableHead>
<TableHead>Note</TableHead>
<TableHead>Manual Presence</TableHead>
<TableHead>Auto Checkout</TableHead>
<TableHead>Approval</TableHead>
<TableHead>Photo</TableHead>
</TableRow>
</TableHeader>
<TableBody>
{currentTableData.length > 0 ? (
currentTableData.map((row, key) => (
<Fragment key={key}>
{row.presence_checkout &&
<TableRow>
<TableCell className="text-center"><Badge variant='secondary'>Checkout</Badge></TableCell>
<TableCell>
<div className="flex space-x-2">
<Badge variant="outline">{row.working_status}</Badge>
<span className="max-w-[500px] truncate font-medium">
{dayjs(row.presence_checkout?.presence_date).format('dddd, DD MMM YYYY')}
</span>
</div>
</TableCell>
<TableCell className="truncate">{dayjs(row.presence_checkout?.working_hours_end).format('HH:mm A')}</TableCell>
<TableCell className="truncate min-w-28">CO-{Number(dayjs(row.presence_checkout?.working_hours_end).format('HH'))}</TableCell>
<TableCell className="truncate max-w-120 cursor-pointer hover:text-his duration-200" title={row.presence_checkout?.location_name} onClick={() => handleOpenMaps(row.presence_checkout.latitude, row.presence_checkout.longitude)}>
{row.presence_checkout?.location_name}
</TableCell>
<TableCell className="truncate">{row.presence_checkout?.device?.name}</TableCell>
<TableCell className="truncate">{row.presence_checkout?.area?.name}</TableCell>
<TableCell className="truncate max-w-[20rem]" title={row.presence_checkout.note}>{row.presence_checkout.note}</TableCell>
<TableCell className="min-w-[150px] text-center">{row.presence_checkout?.manual_presence && <CircleCheck className="text-green-500" />}</TableCell>
<TableCell className="min-w-[140px] text-center">{row.presence_checkout?.auto_checkout && <CircleCheck className="text-green-500" />}</TableCell>
<TableCell className="min-w-[170px]">{row.is_clear_auto_checkout === true ?
<Badge variant='secondary'>approved</Badge> : row.is_clear_auto_checkout === false ? <Badge>need to be approve</Badge> : null
}
</TableCell>
<TableCell className="min-w-[120px]">
{row.presence_checkout.face_image ?
<Dialog>
<DialogTrigger asChild><span className="text-his cursor-pointer hover:opacity-70 duration-200">View Photo</span></DialogTrigger>
<DialogContent className="p-0 w-full sm:w-[450px] rounded-2xl">
<img loading='lazy' alt="avatar" src={process.env.BASE_URL_IMG + row.presence_checkout?.face_image} className="w-full h-full shadow-sm rounded-2xl border border-border" />
</DialogContent>
</Dialog>
: null
}
</TableCell>
</TableRow>
}
<TableRow>
<TableCell className="text-center"><Badge>Checkin</Badge></TableCell>
<TableCell>
<div className="flex space-x-2">
<Badge variant="outline">{row.working_status}</Badge>
<span className="max-w-[500px] truncate font-medium">
{dayjs(row.presence_date).format('dddd, DD MMM YYYY')}
</span>
</div>
</TableCell>
<TableCell className="truncate">{dayjs(row.working_hours_start).format('HH:mm A')}</TableCell>
<TableCell className="truncate min-w-28">CI-{Number(dayjs(row.working_hours_start).format('HH'))}</TableCell>
<TableCell className="truncate max-w-120 cursor-pointer hover:text-his duration-200" title={row.location_name} onClick={() => handleOpenMaps(row.latitude, row.longitude)}>{row.location_name}</TableCell>
<TableCell className="truncate">{row.device.name}</TableCell>
<TableCell className="truncate">{row.area?.name}</TableCell>
<TableCell className="truncate max-w-[20rem]" title={row.note}>{row.note}</TableCell>
<TableCell className="min-w-[150px] text-center">{row.manual_presence && <CircleCheck className="text-green-500" />}</TableCell>
<TableCell></TableCell>
<TableCell className="min-w-[170px]">{!row.manual_presence && (row.working_status === 'WFH' || row.working_status === 'WFS') ?
<Badge variant={getVariantApproved(row.status?.code as any)}>{row.status?.name}</Badge> : <></>
}
</TableCell>
<TableCell className="min-w-[120px]">
{row.face_image ?
<Dialog>
<DialogTrigger asChild><span className="text-his cursor-pointer hover:opacity-70 duration-200">View Photo</span></DialogTrigger>
<DialogContent className="p-0 w-full sm:w-[450px] rounded-2xl">
<img loading='lazy' alt="avatar" src={process.env.BASE_URL_IMG + row.face_image} className="w-full h-full shadow-sm rounded-2xl border border-border" />
</DialogContent>
</Dialog>
: null
}
</TableCell>
</TableRow>
</Fragment>
))
) : (
<TableRow>
<TableCell colSpan={12} className="h-32 text-center" >
No results.
</TableCell>
</TableRow>
)}
</TableBody>
</Table>
</div >
<Pagination totalRowinTable={getTotalRows()} totalRows={data.length} />
</div >
)
}