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..7e83c701 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,8 @@ import styles from './availability-table.module.css' export const AvailabilityTable: FC = () => { const { selectedDate } = useCalendarContext() const [userAbsences, setUserAbsences] = useState([]) + const [previousDate, setPreviousDate] = useState(null) + const [requiredFiltersChange, setRequiredFiltersChange] = useState(false) const [absenceFilters, setAbsenceFilters] = useState({ startDate: chrono().format(chrono.DATE_FORMAT), endDate: chrono().format(chrono.DATE_FORMAT) @@ -47,24 +49,31 @@ export const AvailabilityTable: FC = () => { absenceFilters.organizationIds !== undefined || absenceFilters.userIds !== undefined useEffect(() => { - if (requiredFiltersAreSelected()) { + if ( + requiredFiltersAreSelected() && + (requiredFiltersChange === true || 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) + setRequiredFiltersChange(false) }) - } else { - setUserAbsences([]) } - }, [absenceFilters, selectedDateInterval]) + }, [absenceFilters, selectedDateInterval, previousDate]) const checkIfHoliday = (day: Date) => holidays.some((holiday) => chrono(day).isSameDay(holiday.date)) const onFilterChange = (updatedFilter: Partial) => { setAbsenceFilters({ ...absenceFilters, ...updatedFilter }) + setRequiredFiltersChange(true) } useEffect(() => {