mirror of
https://github.com/ichsankurnia/hats-app.git
synced 2026-12-06 20:46:29 +00:00
56 lines
1.8 KiB
TypeScript
56 lines
1.8 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 { 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[]
|
|
fullname: string
|
|
};
|
|
|
|
const ExportComponent: FC<Props> = ({ data, fullname }) => {
|
|
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 = `${fullname} 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 (
|
|
<>
|
|
<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>
|
|
</>
|
|
);
|
|
}
|
|
|
|
export default ExportComponent;
|