All files / apps/roomBooking/src/screens/ChooseDate/hook useQuickSelectOptions.ts

100% Statements 18/18
100% Branches 3/3
100% Functions 7/7
100% Lines 17/17

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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82                          1x     4x 4x           4x             1x         4x   4x     1x 3x 3x   3x   3x                   3x 3x       3x   1x                   3x                
import { useCallback, useMemo } from 'react';
 
import { DATE_PICKER_TIME_SLOT } from '@repo/ui/components/DatePicker';
import type { QuickSelectValue } from '@repo/ui/components/QuickSelect';
 
import { useHolidayCalendar as useSharedHolidayCalendar } from '@repo/hooks';
 
import { generateDynamicQuickSelectOptions } from '@repo/utils/chooseDate';
import { disabledDateByHolidayCalendar, getPresetDate } from '@repo/utils/date';
import { applyTimeSlotsToRange } from '@repo/utils/timeSlotRange';
 
import { useRoomBookingRuntime } from '@/screens/RoomBooking/runtime';
 
export const getFallbackValue = (
  referenceDate = getPresetDate(1),
): QuickSelectValue => {
  const timeSlot = DATE_PICKER_TIME_SLOT.ALL_DAY;
  const mappedRange = applyTimeSlotsToRange(
    { startDate: referenceDate, endDate: referenceDate },
    timeSlot,
    timeSlot,
  );
 
  return {
    startDate: mappedRange.startDate,
    endDate: mappedRange.endDate,
    timeSlot,
  };
};
 
export const useHolidayCalendar = (year: number) => {
  const {
    meta: {
      api: { holidaysHttp },
    },
  } = useRoomBookingRuntime();
 
  return useSharedHolidayCalendar(year, holidaysHttp);
};
 
export const useQuickSelectOptions = () => {
  const currentYear = useMemo(() => new Date().getFullYear(), []);
  const { holidays, makeUpWorkdays } = useHolidayCalendar(currentYear);
 
  const quickSelectOptions = useMemo(
    () =>
      generateDynamicQuickSelectOptions({
        holidays: holidays,
        makeUpWorkdays: makeUpWorkdays,
        now: new Date(),
        limit: 10,
        slotSequence: [DATE_PICKER_TIME_SLOT.ALL_DAY],
      }),
    [holidays, makeUpWorkdays],
  );
 
  const defaultSuggestedValue = useMemo<QuickSelectValue>(
    () => quickSelectOptions[1]?.value || getFallbackValue(),
    [quickSelectOptions],
  );
 
  const disabledDates = useCallback(
    (value: unknown) => {
      return disabledDateByHolidayCalendar({
        value,
        holidays,
        makeUpWorkdays,
        disablePastDates: true,
      });
    },
    [holidays, makeUpWorkdays],
  );
 
  return {
    holidays,
    makeUpWorkdays,
    quickSelectOptions,
    defaultSuggestedValue,
    disabledDates,
  };
};