Files
hats-app/app/admin/report/history/component/Export.tsx
T
2025-12-19 13:57:55 +07:00

59 lines
1.9 KiB
TypeScript

'use client'
import { FC, useState } from 'react';
import dayjs from 'dayjs';
import { useSession } from 'next-auth/react';
import { Icons } from '@/components/Icons';
import { Button } from '@/components/ui/button';
import DateRangePicker from './DateRangePicker';
import { exportAllEmployeeHistory } from '@/lib/exportExcel';
import { type IHistory } from '@/app/(dashboard)/history/components/type';
import { type IUser } from '@/app/admin/administration/user/component/type';
type Props = {
data: IHistory[]
};
const ExportComponent: FC<Props> = ({ data }) => {
const [isLoading, setIsLoading] = useState(false)
const { data: session } = useSession()
const handleExport = async () => {
setIsLoading(true)
const resBlob: any = await exportAllEmployeeHistory(session?.user.detail as IUser, data)
if (resBlob) {
const url = URL.createObjectURL(resBlob);
// Create a link and trigger a click event to start the download
const a = document.createElement("a");
a.style.display = "none";
a.href = url;
a.download = `Employee Attendance History - ${dayjs().format('YYYYMMDD HHmmssa')}`; // Set the downloaded filename
document.body.appendChild(a);
a.click();
// Clean up by removing the temporary link and URL
document.body.removeChild(a);
URL.revokeObjectURL(url);
}
setIsLoading(false)
}
return (
<>
<div className="flex flex-col sm:flex-row items-center gap-4">
<DateRangePicker />
<Button className='w-full sm:w-max' disabled={isLoading} onClick={handleExport}>
{isLoading && (
<Icons.spinner className="mr-1 h-4 w-4 animate-spin" />
)}
Download
</Button>
</div>
</>
);
}
export default ExportComponent;