All files / apps/reallocation/src/hooks useHolidayCalendar.ts

90% Statements 9/10
100% Branches 8/8
75% Functions 3/4
90% Lines 9/10

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51            1x       2x                       1x         2x     2x         2x         2x         2x 2x        
import { useCallback, useMemo } from 'react';
 
import { type SharedHolidays, useGetHolidays } from '@repo/hooks/dateQueries';
 
import { useReallocationV2Runtime } from '@/screens/ReallocationV2/runtime';
 
const mergeHolidayCalendarData = (
  currentYearHolidays?: SharedHolidays,
  nextYearHolidays?: SharedHolidays,
) => {
  return {
    holidays: [
      ...(currentYearHolidays?.holidays ?? []),
      ...(nextYearHolidays?.holidays ?? []),
    ],
    makeUpWorkdays: [
      ...(currentYearHolidays?.makeUpWorkdays ?? []),
      ...(nextYearHolidays?.makeUpWorkdays ?? []),
    ],
  };
};
 
export const useHolidayCalendar = (year: number) => {
  const {
    meta: {
      api: { holidaysHttp },
    },
  } = useReallocationV2Runtime();
 
  // Do not trigger global unauthorized navigation for optional holiday lookup.
  const passthroughGuard = useCallback(
    async <T>(requestFn: () => Promise<T>) => requestFn(),
    [],
  );
 
  const { data: currentYearHolidays } = useGetHolidays(year, {
    httpClient: holidaysHttp,
    requestGuard: passthroughGuard,
  });
 
  const { data: nextYearHolidays } = useGetHolidays(year + 1, {
    httpClient: holidaysHttp,
    requestGuard: passthroughGuard,
  });
 
  return useMemo(
    () => mergeHolidayCalendarData(currentYearHolidays, nextYearHolidays),
    [currentYearHolidays, nextYearHolidays],
  );
};