mirror of
https://github.com/ichsankurnia/hats-app.git
synced 2026-12-06 23:06:29 +00:00
96 lines
4.0 KiB
TypeScript
96 lines
4.0 KiB
TypeScript
'use client'
|
|
import { FC, useState } from 'react';
|
|
import { usePathname, useRouter, useSearchParams } from 'next/navigation';
|
|
import { DatePicker } from 'antd';
|
|
import dayjs from 'dayjs';
|
|
import { useSession } from 'next-auth/react';
|
|
import { Icons } from '@/components/Icons';
|
|
import { Button } from '@/components/ui/button';
|
|
import { exportTable } from '@/lib/exportExcel';
|
|
import { type IExcelJSColumnType } from '@/types/types';
|
|
import { type IUser } from '@/app/admin/administration/user/component/type';
|
|
|
|
type Props = {
|
|
data: any[]
|
|
};
|
|
|
|
const ExportComponent: FC<Props> = ({ data }) => {
|
|
const [isLoading, setIsLoading] = useState(false)
|
|
|
|
const searchParams = useSearchParams();
|
|
const router = useRouter()
|
|
const path = usePathname()
|
|
const { data: session } = useSession()
|
|
|
|
const startDate = searchParams!.get('start_date') as string || dayjs().subtract(1, 'month')
|
|
const endDate = searchParams!.get('end_date') as string || dayjs().add(1, 'month')
|
|
|
|
const handleChange = (values: any) => {
|
|
if (values) {
|
|
const url = `${path}?start_date=${dayjs(values[0]).format('YYYY-MM-DD')}&end_date=${dayjs(values[1]).format('YYYY-MM-DD')}`
|
|
router.replace(url)
|
|
} else {
|
|
const url = `${path}`
|
|
router.replace(url)
|
|
}
|
|
}
|
|
|
|
const handleExport = async () => {
|
|
setIsLoading(true)
|
|
|
|
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: 'Apply at', key: 'created_at', style: { numFmt: 'dd/mm/yyyy' } },
|
|
{ header: 'Note', key: 'note' },
|
|
{ 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);
|
|
}
|
|
setIsLoading(false)
|
|
}
|
|
|
|
return (
|
|
<>
|
|
<div className="flex flex-col sm:flex-row items-center gap-4">
|
|
<DatePicker.RangePicker size='small' className='p-2 rounded-lg' picker='date' format='MMM DD, YYYY'
|
|
onChange={handleChange} defaultValue={[dayjs(startDate), dayjs(endDate)]}
|
|
/>
|
|
<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; |