Skip to content

Commit 52b1335

Browse files
committed
feat: Update min/max date normalizing for all types
1 parent 4269722 commit 52b1335

1 file changed

Lines changed: 26 additions & 31 deletions

File tree

src/shared/features/AppletSettings/ExportDataSetting/Popups/ExportSettingsPopup/ExportSettingsPopup.tsx

Lines changed: 26 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Button } from '@mui/material';
22
import { addDays, endOfDay, startOfDay } from 'date-fns';
3-
import { useMemo } from 'react';
3+
import { useCallback, useEffect, useMemo } from 'react';
44
import { useFormContext } from 'react-hook-form';
55
import { useTranslation } from 'react-i18next';
66

@@ -17,7 +17,6 @@ import {
1717
StyledModalWrapper,
1818
theme,
1919
} from 'shared/styles';
20-
import { SelectEvent } from 'shared/types';
2120

2221
import {
2322
ExportDataFormValues,
@@ -57,53 +56,56 @@ export const ExportSettingsPopup = ({
5756
},
5857
};
5958

60-
const processFromDate = (date: DateType | undefined) => {
61-
if (!date) return;
62-
setValue('fromDate', startOfDay(date));
63-
};
59+
const processFromDate = useCallback(
60+
(date: DateType | undefined) => {
61+
if (!date) return;
62+
setValue('fromDate', startOfDay(date));
63+
},
64+
[setValue],
65+
);
6466

65-
const processToDate = (date: DateType | undefined) => {
66-
if (!date) return;
67-
setValue('toDate', endOfDay(date));
68-
};
67+
const processToDate = useCallback(
68+
(date: DateType | undefined) => {
69+
if (!date) return;
70+
setValue('toDate', endOfDay(date));
71+
},
72+
[setValue],
73+
);
6974

7075
const onFromDatePickerClose = () => {
7176
let newToDate = toDate;
7277
if (toDate < fromDate) {
7378
const increasedFromDate = addDays(fromDate, 1);
74-
const maxDate = getMaxDate();
7579

7680
newToDate = increasedFromDate <= maxDate ? increasedFromDate : maxDate;
7781
}
7882
processToDate(newToDate);
7983
};
8084

81-
const onDateTypeChange = (e: SelectEvent) => {
82-
const dateType = e.target.value as ExportDateType;
83-
const maxDate = getMaxDate();
85+
useEffect(() => {
8486
switch (dateType) {
8587
case ExportDateType.AllTime:
86-
setValue('fromDate', minDate);
87-
setValue('toDate', maxDate);
88+
processFromDate(minDate);
89+
processToDate(maxDate);
8890
break;
8991
case ExportDateType.Last24h:
9092
setValue('fromDate', addDays(maxDate, -1));
9193
setValue('toDate', maxDate);
9294
break;
9395
case ExportDateType.LastWeek:
94-
setValue('fromDate', addDays(maxDate, -7));
95-
setValue('toDate', maxDate);
96+
processFromDate(addDays(maxDate, -7));
97+
processToDate(maxDate);
9698
break;
9799
case ExportDateType.LastMonth:
98-
setValue('fromDate', addDays(maxDate, -30));
99-
setValue('toDate', maxDate);
100+
processFromDate(addDays(maxDate, -30));
101+
processToDate(maxDate);
100102
break;
101103
case ExportDateType.ChooseDates:
102-
setValue('fromDate', minDate);
103-
setValue('toDate', maxDate);
104+
processFromDate(minDate);
105+
processToDate(maxDate);
104106
break;
105107
}
106-
};
108+
}, [dateType, minDate, maxDate, processFromDate, processToDate, setValue]);
107109

108110
const filteredSupplementaryFiles = useMemo(
109111
() =>
@@ -143,7 +145,6 @@ export const ExportSettingsPopup = ({
143145
options={getDateTypeOptions()}
144146
label={t('dateRange')}
145147
data-testid={`${dataTestId}-dateType`}
146-
customChange={onDateTypeChange}
147148
fullWidth
148149
/>
149150
</StyledFlexColumn>
@@ -197,13 +198,7 @@ export const ExportSettingsPopup = ({
197198
)}
198199
<StyledFlexAllCenter>
199200
<Button
200-
onClick={() => {
201-
if (dateType !== 'chooseDates') {
202-
setValue('toDate', getMaxDate());
203-
}
204-
205-
onExport();
206-
}}
201+
onClick={onExport}
207202
color="primary"
208203
variant="contained"
209204
sx={{ px: 2.4 }}

0 commit comments

Comments
 (0)