-
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(schedules): weekend meeting pdf export
- Loading branch information
Showing
26 changed files
with
838 additions
and
427 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import { Box } from '@mui/material'; | ||
import { IconLoading } from '@components/icons'; | ||
import { useAppTranslation } from '@hooks/index'; | ||
import { WeekendExportType } from './index.types'; | ||
import useWeekendExport from './useWeekendExport'; | ||
import Button from '@components/button'; | ||
import Dialog from '@components/dialog'; | ||
import Typography from '@components/typography'; | ||
import WeekRangeSelector from '../week_range_selector'; | ||
|
||
const WeekendExport = ({ open, onClose }: WeekendExportType) => { | ||
const { t } = useAppTranslation(); | ||
|
||
const { | ||
handleSetEndWeek, | ||
handleSetStartWeek, | ||
isProcessing, | ||
handleExportSchedule, | ||
} = useWeekendExport(onClose); | ||
|
||
return ( | ||
<Dialog | ||
onClose={onClose} | ||
open={open} | ||
sx={{ padding: '24px', position: 'relative' }} | ||
> | ||
<Box | ||
sx={{ | ||
display: 'flex', | ||
gap: '24px', | ||
flexDirection: 'column', | ||
width: '100%', | ||
marginBottom: '110px', | ||
overflow: 'auto', | ||
}} | ||
> | ||
<Box sx={{ display: 'flex', flexDirection: 'column', gap: '8px' }}> | ||
<Typography className="h2">{t('tr_exportWM')}</Typography> | ||
<Typography color="var(--grey-400)"> | ||
{t('tr_exportWMDesc')} | ||
</Typography> | ||
</Box> | ||
|
||
<WeekRangeSelector | ||
meeting="weekend" | ||
onStartChange={handleSetStartWeek} | ||
onEndChange={handleSetEndWeek} | ||
/> | ||
</Box> | ||
|
||
<Box | ||
sx={{ | ||
display: 'flex', | ||
flexDirection: 'column', | ||
gap: '8px', | ||
width: '100%', | ||
position: 'absolute', | ||
bottom: 0, | ||
right: 0, | ||
padding: '24px', | ||
}} | ||
> | ||
<Button | ||
variant="main" | ||
disabled={isProcessing} | ||
endIcon={isProcessing && <IconLoading />} | ||
onClick={handleExportSchedule} | ||
> | ||
{t('tr_export')} | ||
</Button> | ||
<Button variant="secondary" onClick={onClose}> | ||
{t('tr_cancel')} | ||
</Button> | ||
</Box> | ||
</Dialog> | ||
); | ||
}; | ||
|
||
export default WeekendExport; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export type WeekendExportType = { | ||
open: boolean; | ||
onClose: VoidFunction; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import { useState } from 'react'; | ||
import { useRecoilValue } from 'recoil'; | ||
import { pdf } from '@react-pdf/renderer'; | ||
import { saveAs } from 'file-saver'; | ||
import { useAppTranslation } from '@hooks/index'; | ||
import { WeekendExportType } from './index.types'; | ||
import { displaySnackNotification } from '@services/recoil/app'; | ||
import { getMessageByCode } from '@services/i18n/translation'; | ||
import { schedulesState } from '@states/schedules'; | ||
import { WeekendMeetingDataType } from '@definition/schedules'; | ||
import { schedulesWeekendData } from '@services/app/schedules'; | ||
import { | ||
congNameState, | ||
congNumberState, | ||
userDataViewState, | ||
} from '@states/settings'; | ||
import { TemplateWeekendMeeting } from '@views/index'; | ||
|
||
const useWeekendExport = (onClose: WeekendExportType['onClose']) => { | ||
const { t } = useAppTranslation(); | ||
|
||
const schedules = useRecoilValue(schedulesState); | ||
const dataView = useRecoilValue(userDataViewState); | ||
const congName = useRecoilValue(congNameState); | ||
const congNumber = useRecoilValue(congNumberState); | ||
|
||
const [startWeek, setStartWeek] = useState(''); | ||
const [endWeek, setEndWeek] = useState(''); | ||
const [isProcessing, setIsProcessing] = useState(false); | ||
|
||
const handleSetStartWeek = (value: string) => setStartWeek(value); | ||
|
||
const handleSetEndWeek = (value: string) => setEndWeek(value); | ||
|
||
const handleExportSchedule = async () => { | ||
if (startWeek.length === 0 || endWeek.length === 0) return; | ||
|
||
try { | ||
setIsProcessing(true); | ||
|
||
const weeksList = schedules.filter( | ||
(record) => record.weekOf >= startWeek && record.weekOf <= endWeek | ||
); | ||
|
||
const meetingData: WeekendMeetingDataType[] = []; | ||
|
||
for await (const schedule of weeksList) { | ||
const data = await schedulesWeekendData(schedule, dataView); | ||
meetingData.push(data); | ||
} | ||
|
||
const firstWeek = meetingData.at(0).weekOf.replaceAll('/', ''); | ||
const lastWeek = meetingData.at(-1).weekOf.replaceAll('/', ''); | ||
|
||
const blob = await pdf( | ||
<TemplateWeekendMeeting | ||
data={meetingData} | ||
cong_name={congName} | ||
cong_number={congNumber} | ||
/> | ||
).toBlob(); | ||
|
||
const filename = `WM_${firstWeek}-${lastWeek}.pdf`; | ||
|
||
saveAs(blob, filename); | ||
|
||
setIsProcessing(false); | ||
onClose?.(); | ||
} catch (error) { | ||
console.error(error); | ||
|
||
setIsProcessing(false); | ||
onClose?.(); | ||
|
||
await displaySnackNotification({ | ||
header: t('tr_errorTitle'), | ||
message: getMessageByCode(error.message), | ||
severity: 'error', | ||
}); | ||
} | ||
}; | ||
|
||
return { | ||
isProcessing, | ||
handleSetStartWeek, | ||
handleSetEndWeek, | ||
handleExportSchedule, | ||
}; | ||
}; | ||
|
||
export default useWeekendExport; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.