'use client' import { FC, useState } from 'react'; import { Icons } from '@/components/Icons'; import { Button } from '@/components/ui/button'; import { type IUser } from './type'; import { useSession } from 'next-auth/react'; import { exportTable } from '@/lib/exportExcel'; import dayjs from 'dayjs'; import { IExcelJSColumnType } from '@/types/types'; type Props = { data: IUser[] }; const ExportComponent: FC = ({ data }) => { const { data: session } = useSession() const [isLoading, setIsLoading] = useState(false) const handleExport = async () => { setIsLoading(true) const columns: IExcelJSColumnType[] = [ { header: 'No', key: 'number', width: 5, style: { numFmt: '0;[Red]0' } }, { header: 'User ID', key: 'id', width: 8, style: { numFmt: '0;[Red]0' } }, { header: 'Fullname', key: 'fullname', width: 25 }, { header: 'Nickname', key: 'nickname', width: 15 }, { header: 'Email', key: 'email', width: 25 }, { header: 'NIK', key: 'nik', width: 15 }, { header: 'NIKTG', key: 'niktg', width: 10 }, { header: 'Phone Number', key: 'phone_number' }, { header: 'Employee Type', key: 'employee_type', width: 50, value: 'employee_type.name' }, { header: 'Position', key: 'position', value: 'position.name' }, { header: 'Department', key: 'department', value: 'division.name', width: 25 }, { header: 'Company', key: 'company', value: 'company.name' }, { header: 'Hire Date', key: 'hire_date', style: { numFmt: 'dd/mm/yyyy' } }, { header: 'Contract Expiration Date', key: 'contract_expiration_date', style: { numFmt: 'dd/mm/yyyy' } }, { header: 'Sex', key: 'gender', width: 10 }, { header: 'Date of Birth', key: 'birthday', style: { numFmt: 'dd/mm/yyyy' } }, { header: 'Role', key: 'role', value: 'role.name' }, { header: 'Activate at', key: 'activated_at', style: { numFmt: 'dd/mm/yyyy' }, width: 15 }, { header: 'Status', key: 'status', value: 'status.name', width: 15 } ] const resBlob: any = await exportTable(session?.user.detail as IUser, data, columns, 'List of Users') 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 List - Export by ${session?.user.detail.nik} - ${dayjs().format('YYYYMMDD HH:mm:ss')}`; // 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 ( ); } export default ExportComponent;