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

104 lines
4.5 KiB
TypeScript

'use client'
import { FC, useState } from 'react';
import { useSession } from 'next-auth/react';
import { useSearchParams } from 'next/navigation';
import dayjs from 'dayjs';
import { Icons } from '@/components/Icons';
import { Button } from '@/components/ui/button';
import { exportEmployeeHistory, exportTable } from '@/lib/exportExcel';
import { type IUser } from '@/app/admin/administration/user/component/type';
import type { ILeaveSickPermit, IHistory } from './type';
import { type IExcelJSColumnType } from '@/types/types';
type Props = {
data: ILeaveSickPermit[] | IHistory[]
};
const ExportComponent: FC<Props> = ({ data }) => {
const [isLoading, setIsLoading] = useState(false)
const { data: session } = useSession()
const searchParams = useSearchParams();
const tab = searchParams!.get('tab');
const handleExport = async () => {
setIsLoading(true)
if (tab === 'leave-permit') {
const columns: IExcelJSColumnType[] = [
{ header: 'No', key: 'number', width: 5, style: { numFmt: '0;[Red]0' } },
{ header: 'Application Code', key: 'application_form_code' },
{ header: 'N I K', key: 'nik' },
{ header: 'Employee', key: 'fullname' },
{ header: 'Category', key: 'category_name' },
{ header: 'Address', key: 'contact_street' },
{ header: 'City', key: 'contact_city' },
{ header: 'Start Date', key: 'start_date', style: { numFmt: 'dd/mm/yyyy' } },
{ header: 'End Date', key: 'end_date', style: { numFmt: 'dd/mm/yyyy' } },
{ header: 'Return to Work', key: 'return_to_work_date', style: { numFmt: 'dd/mm/yyyy' } },
{ header: 'Note', key: 'note' },
{ header: 'Apply at', key: 'created_at', style: { numFmt: 'dd/mm/yyyy' } },
{ header: 'Approved by', key: 'approved_by' },
{ header: 'Approved at', key: 'approved_at', style: { numFmt: 'dd/mm/yyyy' } },
{ header: 'Rejected by', key: 'rejected_by' },
{ header: 'Rejected at', key: 'rejected_at', style: { numFmt: 'dd/mm/yyyy' } },
]
const resBlob: any = await exportTable(session?.user.detail as IUser, data, columns, 'Leave, Permit and Sick Report')
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 = `Leave, Permit and Sick Report - ${dayjs().format('YYYYMMDD HH:mm:ssa')}`; // 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);
}
} else {
const resBlob: any = await exportEmployeeHistory(session?.user.detail as IUser, data as IHistory[])
if (resBlob) {
/* USE THIS IF RES IS Base64String */
// window.open(
// res as string,
// '_blank', // <- This is what makes it open in a new window.
// );
//// Convert the response to blob and create a temporary URL
// const blob = await res.blob();
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 = `Attendance History - ${session?.user.detail.nickname} - ${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="flex" disabled={isLoading} onClick={handleExport}>
{isLoading && (
<Icons.spinner className="mr-1 h-4 w-4 animate-spin" />
)}
Export
</Button>
</>
);
}
export default ExportComponent;