From 89e96672bf8dfc5cbc255b1812c3f6faaaa7d54f Mon Sep 17 00:00:00 2001 From: Viet Anh Nguyen Date: Mon, 5 Feb 2024 15:52:15 +0700 Subject: [PATCH] Update Dashboard page --- cope2n-fe/src/pages/dashboard/index.tsx | 69 ++++++++++++++++++++++--- cope2n-fe/src/request/report.ts | 31 +++++++++++ 2 files changed, 93 insertions(+), 7 deletions(-) diff --git a/cope2n-fe/src/pages/dashboard/index.tsx b/cope2n-fe/src/pages/dashboard/index.tsx index a3522f0..53d0f47 100644 --- a/cope2n-fe/src/pages/dashboard/index.tsx +++ b/cope2n-fe/src/pages/dashboard/index.tsx @@ -6,14 +6,19 @@ import { Dayjs } from 'dayjs'; import { useOverViewReport } from 'queries/report'; import { useState } from 'react'; import { useNavigate } from 'react-router-dom'; +import { DownloadOutlined } from '@ant-design/icons'; +import { downloadDashboardReport } from 'request/report'; export interface ReportFormValues { + duration: string; dateRange: [Dayjs, Dayjs]; subsidiary: string; } const Dashboard = () => { const navigate = useNavigate(); + const [duration, setDuration] = useState('30d'); + const [subsidiary, setSubsidiary] = useState('all'); const [form] = Form.useForm(); const [pagination, setPagination] = useState({ page: 1, @@ -44,18 +49,36 @@ const Dashboard = () => { }); }; + const handleDownloadReport = async () => { + console.log('duration >>>', duration); + console.log('subsidiary >>>', subsidiary); + const {file, filename} = await downloadDashboardReport(duration, subsidiary); + const anchorElement = document.createElement('a'); + anchorElement.href = URL.createObjectURL(file); + anchorElement.download = filename; + + document.body.appendChild(anchorElement); + anchorElement.click(); + + // Clean up + document.body.removeChild(anchorElement); + URL.revokeObjectURL(anchorElement.href); + }; + return ( <> - {/* */} - {/* + */} + } /> @@ -64,6 +87,7 @@ const Dashboard = () => { style={{ display: 'flex', flexDirection: 'row', gap: 10 }} onFinish={handleSubmit} > + {/* TODO: Remove this when the Dashboard page is refactored */} { message: 'Please select a date range', }, ]} + style={{ + display: 'none' + }} > + + { { value: 'sesp', label: 'SESP' }, { value: 'seau', label: 'SEAU' }, ]} - allowClear + defaultValue={'all'} + value={subsidiary} + onChange={(value) => { + setSubsidiary(value); + }} /> diff --git a/cope2n-fe/src/request/report.ts b/cope2n-fe/src/request/report.ts index 2b65a48..f0a0cd7 100644 --- a/cope2n-fe/src/request/report.ts +++ b/cope2n-fe/src/request/report.ts @@ -117,3 +117,34 @@ export async function downloadReport(report_id: string) { console.log(error); } } + + +export async function downloadDashboardReport(duration='30d', subsidiary='ALL') { + try { + const response = await API.get(`/api/ctel/overview_download_file/?duration=${duration}&subsidiary=${subsidiary}`, { + responseType: 'blob', // Important + }); + let filename = "report.xlsx"; + try { + let basename = response.headers['content-disposition'].split('filename=')[1].split('.')[0]; + let extension = response.headers['content-disposition'].split('.')[1].split(';')[0]; + filename = `${basename}.${extension}` + } catch(err) { + console.log(err); + } + const file = new Blob([response.data], { + type: 'application/vnd.ms-excel', + }); + // const fileURL = URL.createObjectURL(file); + // window.open(fileURL); + return { + file: file, + filename: filename, + } + } catch (error) { + notification.error({ + message: `${error?.message}`, + }); + console.log(error); + } +}