-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extend PDF Export and fix data calculation
- Loading branch information
Florian Jud
committed
May 16, 2024
1 parent
dbf6eb8
commit 24d8ea7
Showing
14 changed files
with
242 additions
and
59 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
import { Download } from '@mui/icons-material'; | ||
import { Button, Stack, Typography } from '@mui/material'; | ||
import { Dayjs } from 'dayjs'; | ||
import { useEffect, useState } from 'react'; | ||
import useNotification from '../hooks/useNotification'; | ||
import { ITime, ITimeCsv } from '../interfaces/Types'; | ||
import { useAppSelector } from '../store/hooks'; | ||
import { ReactComponent as TimesSvg } from '../svg/airport.svg'; | ||
import DayjsUtils from '../utils/DayjsUtils'; | ||
import { exportPdf } from '../utils/PDFExport'; | ||
import TimeUtils from '../utils/TimeUtils'; | ||
import MainContainer from './common/MainContainer'; | ||
import MonthPicker from './common/MonthPicker'; | ||
|
||
const ExportPage = () => { | ||
const notifyContext = useNotification().notifyContext; | ||
|
||
const { profile, user } = useAppSelector((state) => state.auth); | ||
const { times, evaluateTimes } = useAppSelector((state) => state.times); | ||
|
||
const [days, setDays] = useState<Dayjs[]>([]); | ||
const [filteredTimes, setFilteredTimes] = useState<ITime[] | null>(null); | ||
const [timesExport, setTimesExport] = useState<ITimeCsv[] | null>(null); | ||
const [downloadBlocked, setDownloadBlocked] = useState<boolean>(true); | ||
|
||
const handleDateRangePicker = (dates: Dayjs[]) => setDays(dates); | ||
|
||
/* | ||
load all times from Firestore DB, either the select month, | ||
or the current month, if nothing selected yet. | ||
*/ | ||
useEffect(() => { | ||
if (days && days.length > 0) { | ||
const [from, to] = DayjsUtils.calculateFromAndTo(days); | ||
if (times && times.length > 0) { | ||
const res = TimeUtils.filterByTimestampRange(from.unix(), to.unix(), times); | ||
setFilteredTimes(res); | ||
setDownloadBlocked(false); | ||
} | ||
} | ||
}, [days]); | ||
|
||
useEffect(() => { | ||
if (filteredTimes && filteredTimes.length > 0) { | ||
const data: ITimeCsv[] = []; | ||
filteredTimes.forEach((time) => { | ||
let workingTimeBalance = 0; | ||
let availableTimeBalance = 0; | ||
if (time.workday) { | ||
workingTimeBalance = time.workingTime - (profile?.workingtime || 0); | ||
availableTimeBalance = time.availableTime - (profile?.availabletime || 0); | ||
} else { | ||
workingTimeBalance = time.workingTime; | ||
availableTimeBalance = time.availableTime; | ||
} | ||
|
||
data.push({ | ||
week: TimeUtils.dateStringToWeek(time.day), | ||
day: time.day, | ||
workingTime: time.workingTime, | ||
workingTimeBalance: workingTimeBalance, | ||
availableTime: time.availableTime, | ||
availableTimeBalance: availableTimeBalance, | ||
notes: time.notes, | ||
}); | ||
}); | ||
setTimesExport(data); | ||
} | ||
}, [filteredTimes]); | ||
|
||
const handleDownload = () => { | ||
if (!timesExport) { | ||
notifyContext.addNotification('Keine Daten zum Exportieren gefunden', 'error'); | ||
return; | ||
} | ||
const firstDay = days[0]; | ||
const month = days[0].month(); | ||
const fileName = `report_${firstDay.format('MM-YYYY')}.pdf`; | ||
const exportMonth = firstDay.format('MMMM YYYY'); | ||
const name = user?.displayName || ''; | ||
|
||
// get the balance of the last month | ||
const previsousAvailableTimeBalance = evaluateTimes[month - 1]?.availableTime?.balance || 0; | ||
const previsousWorkingTimeBalance = evaluateTimes[month - 1]?.workingTime?.balance || 0; | ||
|
||
// get the balance of the current month | ||
const availableTimeBalanceCurrent = evaluateTimes[month]?.availableTime?.balance || 0; | ||
const workingTimeBalanceCurrent = evaluateTimes[month]?.workingTime?.balance || 0; | ||
|
||
exportPdf( | ||
timesExport, | ||
fileName, | ||
exportMonth, | ||
name, | ||
previsousWorkingTimeBalance, | ||
previsousAvailableTimeBalance, | ||
workingTimeBalanceCurrent, | ||
availableTimeBalanceCurrent | ||
); | ||
}; | ||
|
||
return ( | ||
<MainContainer> | ||
<Stack direction="column" alignItems="center" spacing={2}> | ||
<TimesSvg width="150px" height="150px" /> | ||
<MonthPicker onChange={handleDateRangePicker} /> | ||
<Typography variant="caption"> | ||
Der aktuell Monat führt zu nicht korrekten Werten, da die Daten noch nicht vollständig sind. Bitte wähle einen vergangenen Monat | ||
aus. | ||
</Typography> | ||
|
||
{timesExport && ( | ||
<Button disabled={downloadBlocked} variant="contained" endIcon={<Download />} onClick={handleDownload}> | ||
Bericht herunterladen | ||
</Button> | ||
)} | ||
</Stack> | ||
</MainContainer> | ||
); | ||
}; | ||
|
||
export default ExportPage; |
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
Oops, something went wrong.