'use client' import { FC, useState } from 'react'; import { Icons } from '@/components/Icons'; import { Button } from '@/components/ui/button'; import { useSession } from 'next-auth/react'; import { exportTable } from '@/lib/exportExcel'; import dayjs from 'dayjs'; import { IExcelJSColumnType } from '@/types/types'; import { IUser } from '@/app/admin/administration/user/component/type'; type Props = { data: IUser[] } const ExportComponent: FC = ({ data }) => { const [isLoading, setIsLoading] = useState(false) const { data: session } = useSession() const handleExport = async () => { setIsLoading(true) const columns: IExcelJSColumnType[] = [ { header: 'No', key: 'number', width: 5, style: { numFmt: '0;[Red]0' } }, { header: 'Code', key: 'code', width: 15 }, { header: 'Name', key: 'name', width: 25 }, { header: 'Description', key: 'description', width: 25 }, { header: 'Status', key: 'status', value: 'status.name', width: 15 } ] const resBlob: any = await exportTable(session?.user.detail as IUser, data, columns, 'List of End 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 = `End User 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;