diff --git a/src/api/private.ts b/src/api/private.ts index 614416c..3be172d 100644 --- a/src/api/private.ts +++ b/src/api/private.ts @@ -132,6 +132,14 @@ export const downloadDocumentFn = async (id: number) => { return response.data; }; +export const downloadApplicants = async (lang: string) => { + const response = await authApi.get(`applications/export/${lang}`, { + responseType: 'blob', + }); + + return response.data; +}; + export const downloadApplicationFn = async (id: number, lang: string) => { const response = await authApi.get(`applications/${id}/download-application/${lang}`, { responseType: 'blob', diff --git a/src/hooks/useDownloadApplicants.ts b/src/hooks/useDownloadApplicants.ts new file mode 100644 index 0000000..d0626ad --- /dev/null +++ b/src/hooks/useDownloadApplicants.ts @@ -0,0 +1,48 @@ +import { useQuery } from '@tanstack/react-query'; +import { useT } from '@transifex/react'; +import axios from 'axios'; +import { useSnackbar } from 'notistack'; +import { downloadBlob } from 'src/util/index'; + +import { downloadApplicants } from '../api/private'; +import useLangContext from './useLangContext'; + +type IUseDownloadDocument = { + downloadedDocument?: Blob | null; + downloadDocument: () => void; + isDownloading: boolean; +}; + +export default function useDownloadApplicants(): IUseDownloadDocument { + const t = useT(); + const langContext = useLangContext(); + const { enqueueSnackbar } = useSnackbar(); + + const { + data: downloadedDocument, + refetch: downloadDocument, + fetchStatus, + } = useQuery({ + enabled: false, + queryFn: () => downloadApplicants(langContext.state.selected.split('_')[0]), + onSuccess: (data) => { + downloadBlob(data, 'export.csv'); + return data; + }, + onError: (error) => { + if (axios.isAxiosError(error) && error.response) { + if (error.response.data && error.response.data.detail) { + enqueueSnackbar(t('Error: {error}', { error: error.response.data.detail }), { + variant: 'error', + }); + } + } else { + enqueueSnackbar(t('Error downloading applicants. {error}', { error }), { + variant: 'error', + }); + } + }, + }); + + return { downloadedDocument, downloadDocument, isDownloading: fetchStatus !== 'idle' }; +} diff --git a/src/pages/fi/HomeFI.tsx b/src/pages/fi/HomeFI.tsx index 51ebc56..9657a9e 100644 --- a/src/pages/fi/HomeFI.tsx +++ b/src/pages/fi/HomeFI.tsx @@ -1,5 +1,7 @@ -import { Container } from '@mui/material'; +import { Box, CircularProgress, Container } from '@mui/material'; import { useT } from '@transifex/react'; +import useDownloadApplicants from 'src/hooks/useDownloadApplicants'; +import Button from 'src/stories/button/Button'; import Text from 'src/stories/text/Text'; import Title from 'src/stories/title/Title'; @@ -14,6 +16,7 @@ import { formatCurrency } from '../../util'; export function HomeFI() { const t = useT(); const { data, isLoading } = useGetStatisticsFI(); + const { downloadDocument, isDownloading } = useDownloadApplicants(); return ( <> @@ -138,6 +141,18 @@ export function HomeFI() { )} + + {isDownloading && } + +