mirror of
https://github.com/ichsankurnia/hats-app.git
synced 2026-12-06 21:16:29 +00:00
5a3629badf
Added the ability to view and re-download leave application forms in the Employee History page (Leave, Permit, and Sick tabs) Added employee alpha stats and employee alpha detail in Employee Performance Page Added total WFO, WFH and WFS in Admin Summary Report Added batch import from Excel files in Admin Manual Attendance
114 lines
5.0 KiB
TypeScript
114 lines
5.0 KiB
TypeScript
'use client'
|
|
import { FC, useState } from 'react';
|
|
import { DatePicker } from 'antd';
|
|
import { Icons } from '@/components/Icons';
|
|
import { Button } from '@/components/ui/button';
|
|
import { usePathname, useRouter, useSearchParams } from 'next/navigation';
|
|
import dayjs from 'dayjs';
|
|
import { exportTable } from '@/lib/exportExcel';
|
|
import { useSession } from 'next-auth/react';
|
|
import { IUser } from '@/app/admin/administration/user/component/type';
|
|
import { IMonthlyReport } from './type';
|
|
import { IExcelJSColumnType } from '@/types/types';
|
|
import { type RangePickerProps } from 'antd/es/date-picker';
|
|
|
|
type Props = {
|
|
data: IMonthlyReport[]
|
|
};
|
|
|
|
const ExportComponent: FC<Props> = ({ data }) => {
|
|
const [isLoading, setIsLoading] = useState(false)
|
|
|
|
const { data: session } = useSession()
|
|
const searchParams = useSearchParams();
|
|
const router = useRouter()
|
|
const path = usePathname()
|
|
|
|
const startDate = searchParams!.get('start_date') as string || dayjs().subtract(1, 'day').subtract(1, 'month')
|
|
const endDate = searchParams!.get('end_date') as string || dayjs().subtract(1, 'day')
|
|
|
|
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: 'N I K', key: 'nik' },
|
|
{ header: 'Employee', key: 'fullname' },
|
|
{ header: 'Working Hours', key: 'working_hours', style: { numFmt: '0;[Red]0' } },
|
|
{ header: 'Total Working Hours', key: 'total_working_hours', style: { numFmt: '0;[Red]0' } },
|
|
{ header: 'Delta Working Hours', key: 'delta_working_hours', style: { numFmt: '0;[Red]0' } },
|
|
{ header: 'WFO', key: 'total_wfo', width: 10, style: { numFmt: '0;[Red]0' } },
|
|
{ header: 'WFH', key: 'total_wfh', width: 10, style: { numFmt: '0;[Red]0' } },
|
|
{ header: 'WFS', key: 'total_wfs', width: 10, style: { numFmt: '0;[Red]0' } },
|
|
{ header: 'Leave', key: 'total_cuti', width: 10, style: { numFmt: '0;[Red]0' } },
|
|
{ header: 'Sick', key: 'total_sakit', width: 10, style: { numFmt: '0;[Red]0' } },
|
|
{ header: 'Permission', key: 'total_izin', width: 10, style: { numFmt: '0;[Red]0' } },
|
|
{ header: 'Alpha', key: 'total_alpha', width: 10, style: { numFmt: '0;[Red]0' } },
|
|
{ header: 'Real Working Hours', key: 'real_working_hours', style: { numFmt: '0;[Red]0' } },
|
|
{ header: 'Delta Real Working Hours', key: 'delta_real_working_hours', style: { numFmt: '0;[Red]0' } },
|
|
]
|
|
|
|
const resBlob: any = await exportTable(session?.user.detail as IUser, data, columns, 'Monthly Attendance 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 = `Monthly Attendance Report - ${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)
|
|
}
|
|
|
|
// DISABLE DATE BEFORE 18 SEP 2023 and after today
|
|
// eslint-disable-next-line arrow-body-style
|
|
const disabledDate: RangePickerProps['disabledDate'] = (current) => {
|
|
const disabledDate = new Date('2023-09-18');
|
|
disabledDate.setHours(0, 0, 0, 0);
|
|
const disabledTimestamp = disabledDate.getTime();
|
|
|
|
const currentDate = new Date();
|
|
currentDate.setHours(0, 0, 0, 0);
|
|
|
|
const currentTimestamp = currentDate.getTime();
|
|
const selectedTimestamp = current ? current.valueOf() : 0;
|
|
|
|
return selectedTimestamp < disabledTimestamp || selectedTimestamp >= currentTimestamp;
|
|
}
|
|
|
|
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)]} disabledDate={disabledDate}
|
|
/>
|
|
<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;
|