All files / packages/ui/src/components/DatePicker/hooks useDatePickerController.ts

100% Statements 17/17
100% Branches 6/6
100% Functions 1/1
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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186                                                                                      4x                             93x 93x     93x 93x 93x     93x 93x   93x                   93x               93x                       93x               93x         93x                 93x                       93x                           93x                                                                          
import { useTheme } from "@repo/ui/themes/ThemeContext";
 
import {
  DATE_PICKER_MODE,
  DATE_PICKER_MODE_TYPE,
  DATE_PICKER_TIME_SLOT,
  EMPTY_RANGE,
} from "../constants";
import { useStyles } from "../styles";
import type {
  DatePickerDisabledDates,
  DatePickerMode,
  DatePickerModeType,
  DatePickerProps,
  DatePickerTimeSlot,
  DateRangeValue,
  RangeSelectionTarget,
} from "../types";
 
import { useDatePickerCalendarConfig } from "./useDatePickerCalendarConfig";
import { useDatePickerComputedState } from "./useDatePickerComputedState";
import { useDatePickerConfirmEmitter } from "./useDatePickerConfirmEmitter";
import { useDatePickerDisabledDates } from "./useDatePickerDisabledDates";
import { useDatePickerDraftState } from "./useDatePickerDraftState";
import { useDatePickerRangeSelection } from "./useDatePickerRangeSelection";
import { useDatePickerTimeSlot } from "./useDatePickerTimeSlot";
 
interface UseDatePickerControllerArgs {
  mode: DatePickerMode;
  modeType: DatePickerModeType;
  value: Date | null;
  range: DateRangeValue;
  rangeStartTimeSlot?: DatePickerTimeSlot;
  rangeEndTimeSlot?: DatePickerTimeSlot;
  activeRangeSelection?: RangeSelectionTarget;
  timeSlot: DatePickerTimeSlot;
  textForTimeSlot?: string;
  disableStyleTimeSlot: boolean;
  disabledDates?: DatePickerDisabledDates;
  onConfirm?: DatePickerProps["onConfirm"];
  onTimeSlotChange?: DatePickerProps["onTimeSlotChange"];
}
 
export const useDatePickerController = ({
  mode,
  modeType,
  value,
  range,
  rangeStartTimeSlot,
  rangeEndTimeSlot,
  activeRangeSelection,
  timeSlot,
  textForTimeSlot,
  disableStyleTimeSlot,
  disabledDates,
  onConfirm,
  onTimeSlotChange,
}: UseDatePickerControllerArgs) => {
  const { theme } = useTheme();
  const styles = useStyles();
 
  const showTimeOffSection =
    modeType === DATE_PICKER_MODE_TYPE.DATE_WITH_TIME_OFF;
  const isSingleMode = mode === DATE_PICKER_MODE.SINGLE;
  const effectiveExternalTimeSlot: DatePickerTimeSlot = showTimeOffSection
    ? timeSlot
    : DATE_PICKER_TIME_SLOT.ALL_DAY;
  const effectiveDraftValue = isSingleMode ? value : null;
  const effectiveDraftRange = isSingleMode ? EMPTY_RANGE : range;
 
  const draftState = useDatePickerDraftState({
    mode,
    value: effectiveDraftValue,
    range: effectiveDraftRange,
    effectiveExternalTimeSlot,
    externalRangeStartTimeSlot: rangeStartTimeSlot,
    externalRangeEndTimeSlot: rangeEndTimeSlot,
    externalActiveRangeSelection: activeRangeSelection,
  });
 
  const disabledState = useDatePickerDisabledDates({
    disabledDates,
    mode,
    normalizedRangeStartDate: null, // temporary; recomputed below once computedState is available
    rangeStartTimeSlot: draftState.rangeStartTimeSlot,
    selectedDays: 0, // temporary; recomputed below
  });
 
  const computedState = useDatePickerComputedState({
    mode,
    activeRangeSelection: draftState.activeRangeSelection,
    isDateDisabledByProp: disabledState.isDateDisabledByProp,
    rangeEndTimeSlot: draftState.rangeEndTimeSlot,
    rangeStartTimeSlot: draftState.rangeStartTimeSlot,
    textForTimeSlot,
    tempDate: draftState.tempDate,
    tempRange: draftState.tempRange,
    tempTimeSlot: draftState.tempTimeSlot,
  });
 
  const disabledStateFull = useDatePickerDisabledDates({
    disabledDates,
    mode,
    normalizedRangeStartDate: computedState.normalizedRangeStartDate,
    rangeStartTimeSlot: draftState.rangeStartTimeSlot,
    selectedDays: computedState.selectedDays,
  });
 
  const { emitConfirm } = useDatePickerConfirmEmitter({
    onConfirm,
    showTimeOffSection,
  });
 
  const rangeSelectionActions = useDatePickerRangeSelection({
    draftState,
    showTimeOffSection,
    isDateDisabledByProp: disabledStateFull.isDateDisabledByProp,
    rangeDisabledDates: disabledStateFull.rangeDisabledDates,
    onTimeSlotChange,
    emitConfirm,
  });
 
  const { handleSelectTimeSlot, isTimeSlotDisabled } = useDatePickerTimeSlot({
    draftState,
    mode,
    selectedDays: computedState.selectedDays,
    showTimeOffSection,
    isRangeWithEndDate: computedState.isRangeWithEndDate,
    activeRangeSelection: draftState.activeRangeSelection,
    onTimeSlotChange,
    emitConfirm,
  });
 
  const { calendarBaseStyles, calendarComponents } =
    useDatePickerCalendarConfig({
      styles,
      mode,
      selectedDays: computedState.selectedDays,
      showTimeOffSection,
      disableStyleTimeSlot,
      tempTimeSlot: draftState.tempTimeSlot,
      rangeStartTimeSlot: draftState.rangeStartTimeSlot,
      rangeEndTimeSlot: draftState.rangeEndTimeSlot,
      normalizedRangeStartDate: computedState.normalizedRangeStartDate,
      normalizedRangeEndDate: computedState.normalizedRangeEndDate,
      navIconColor: theme.colors.icon.emphasis,
    });
 
  return {
    styles,
    ui: {
      showTimeOffSection,
      isSingleMode,
      effectiveExternalTimeSlot,
    },
    state: {
      tempDate: draftState.tempDate,
      tempRange: draftState.tempRange,
      tempTimeSlot: draftState.tempTimeSlot,
      rangeStartTimeSlot: draftState.rangeStartTimeSlot,
      selectedDays: computedState.selectedDays,
    },
    calendar: {
      baseStyles: calendarBaseStyles,
      components: calendarComponents,
      disabledDates: {
        single: disabledStateFull.isDateDisabledByProp,
        range: disabledStateFull.rangeDisabledDates,
      },
    },
    summary: {
      dateText: computedState.summaryDateText,
      valueText: computedState.summaryValueText,
    },
    timeOff: {
      isOptionDisabled: isTimeSlotDisabled,
    },
    actions: {
      handleSingleChange: rangeSelectionActions.handleSingleChange,
      handleRangeChange: rangeSelectionActions.handleRangeChange,
      handleSelectTimeSlot,
      reset: draftState.resetDraftState,
    },
  };
};