From 9f87ea6c04f70b0754196bbce15a909168802f08 Mon Sep 17 00:00:00 2001 From: Raul Villalba Medina Date: Mon, 4 Dec 2023 14:50:20 +0100 Subject: [PATCH] feat: availability use 2 years to call to api --- .../availability-table/availability-table.tsx | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/features/binnacle/features/availability/ui/components/availability-table/availability-table.tsx b/src/features/binnacle/features/availability/ui/components/availability-table/availability-table.tsx index ed673cf0..6c02ddb5 100644 --- a/src/features/binnacle/features/availability/ui/components/availability-table/availability-table.tsx +++ b/src/features/binnacle/features/availability/ui/components/availability-table/availability-table.tsx @@ -19,6 +19,7 @@ import styles from './availability-table.module.css' export const AvailabilityTable: FC = () => { const { selectedDate } = useCalendarContext() const [userAbsences, setUserAbsences] = useState([]) + const [previousDate, setPreviousDate] = useState(null) const [absenceFilters, setAbsenceFilters] = useState({ startDate: chrono().format(chrono.DATE_FORMAT), endDate: chrono().format(chrono.DATE_FORMAT) @@ -47,18 +48,23 @@ export const AvailabilityTable: FC = () => { absenceFilters.organizationIds !== undefined || absenceFilters.userIds !== undefined useEffect(() => { - if (requiredFiltersAreSelected()) { + if ( + requiredFiltersAreSelected() && + previousDate?.getFullYear() !== selectedDate.getFullYear() + ) { getAbsencesQry({ ...absenceFilters, - startDate: chrono(selectedDateInterval.start).minus(5, 'day').format(chrono.DATE_FORMAT), - endDate: chrono(selectedDateInterval.end).plus(5, 'day').format(chrono.DATE_FORMAT) + startDate: chrono(selectedDateInterval.start) + .startOf('year') + .minus(1, 'year') + .format(chrono.DATE_FORMAT), + endDate: chrono(selectedDate).endOf('year').format(chrono.DATE_FORMAT) }).then((absences) => { setUserAbsences(absences) + setPreviousDate(selectedDate) }) - } else { - setUserAbsences([]) } - }, [absenceFilters, selectedDateInterval]) + }, [absenceFilters, selectedDateInterval, previousDate]) const checkIfHoliday = (day: Date) => holidays.some((holiday) => chrono(day).isSameDay(holiday.date))