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

100% Statements 19/19
87.5% Branches 7/8
100% Functions 4/4
100% Lines 19/19

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                                                                                          4x                     55x 4x             51x             4x       93x                         55x           55x 1x 1x                             54x 54x         54x     54x           54x                                   93x                   55x                 55x         93x        
import { useCallback } from "react";
import type { DateType } from "react-native-ui-datepicker";
 
import { DATE_PICKER_MODE, DATE_PICKER_TIME_SLOT } from "../constants";
import type {
  DatePickerConfirmMeta,
  DatePickerMode,
  DatePickerTimeSlot,
  DateRangeValue,
  RangeSelectionTarget,
  TempDateRangeValue,
} from "../types";
import {
  applyTimeSlotsToRange,
  calculateDaysOff,
  calculateRangeDaysOff,
  getInclusiveDaysInRange,
  normalizeDate,
  toRangeValue,
} from "../utils";
 
interface UseDatePickerConfirmEmitterArgs {
  onConfirm?: (
    value: Date | null | DateRangeValue,
    meta?: DatePickerConfirmMeta,
  ) => void;
  showTimeOffSection: boolean;
}
 
interface ConfirmPayloadArgs {
  mode: DatePickerMode;
  singleDate: DateType | null;
  rangeDate: TempDateRangeValue;
  timeSlot: DatePickerTimeSlot;
  rangeStartTimeSlot: DatePickerTimeSlot;
  rangeEndTimeSlot: DatePickerTimeSlot;
  activeRangeSelection: RangeSelectionTarget;
}
 
interface EffectiveTimeSlots {
  timeSlot: DatePickerTimeSlot;
  rangeStartTimeSlot: DatePickerTimeSlot;
  rangeEndTimeSlot: DatePickerTimeSlot;
}
 
const getEffectiveTimeSlots = (
  showTimeOffSection: boolean,
  {
    timeSlot,
    rangeStartTimeSlot,
    rangeEndTimeSlot,
  }: Pick<
    ConfirmPayloadArgs,
    "timeSlot" | "rangeStartTimeSlot" | "rangeEndTimeSlot"
  >,
): EffectiveTimeSlots => {
  if (!showTimeOffSection) {
    return {
      timeSlot: DATE_PICKER_TIME_SLOT.ALL_DAY,
      rangeStartTimeSlot: DATE_PICKER_TIME_SLOT.ALL_DAY,
      rangeEndTimeSlot: DATE_PICKER_TIME_SLOT.ALL_DAY,
    };
  }
 
  return {
    timeSlot,
    rangeStartTimeSlot,
    rangeEndTimeSlot,
  };
};
 
export const useDatePickerConfirmEmitter = ({
  onConfirm,
  showTimeOffSection,
}: UseDatePickerConfirmEmitterArgs) => {
  const buildConfirmPayload = useCallback(
    ({
      mode,
      singleDate,
      rangeDate,
      timeSlot,
      rangeStartTimeSlot,
      rangeEndTimeSlot,
      activeRangeSelection,
    }: ConfirmPayloadArgs): {
      value: Date | null | DateRangeValue;
      meta: DatePickerConfirmMeta;
    } => {
      const effectiveTimeSlots = getEffectiveTimeSlots(showTimeOffSection, {
        timeSlot,
        rangeStartTimeSlot,
        rangeEndTimeSlot,
      });
 
      if (mode === DATE_PICKER_MODE.SINGLE) {
        const finalSingleDate = normalizeDate(singleDate);
        return {
          value: finalSingleDate,
          meta: {
            timeSlot: effectiveTimeSlots.timeSlot,
            daysOff: calculateDaysOff(
              finalSingleDate ? 1 : 0,
              effectiveTimeSlots.timeSlot,
            ),
            rangeStartTimeSlot: effectiveTimeSlots.rangeStartTimeSlot,
            rangeEndTimeSlot: effectiveTimeSlots.rangeEndTimeSlot,
            activeRangeSelection,
          },
        };
      }
 
      const finalRange = toRangeValue(rangeDate);
      const selectedDays = getInclusiveDaysInRange(
        finalRange.startDate,
        finalRange.endDate,
      );
      const effectiveRangeEndTimeSlot =
        selectedDays > 1
          ? effectiveTimeSlots.rangeEndTimeSlot
          : effectiveTimeSlots.rangeStartTimeSlot;
      const normalizedRange = applyTimeSlotsToRange(
        finalRange,
        effectiveTimeSlots.rangeStartTimeSlot,
        effectiveRangeEndTimeSlot,
      );
 
      return {
        value: normalizedRange,
        meta: {
          timeSlot: effectiveTimeSlots.rangeStartTimeSlot,
          daysOff: calculateRangeDaysOff(
            finalRange,
            effectiveTimeSlots.rangeStartTimeSlot,
            effectiveRangeEndTimeSlot,
          ),
          rangeStartTimeSlot: effectiveTimeSlots.rangeStartTimeSlot,
          rangeEndTimeSlot: effectiveRangeEndTimeSlot,
          activeRangeSelection,
        },
      };
    },
    [showTimeOffSection],
  );
 
  const emitConfirm = useCallback(
    (
      nextMode: DatePickerMode,
      nextSingleDate: DateType | null,
      nextRangeDate: TempDateRangeValue,
      nextTimeSlot: DatePickerTimeSlot,
      nextRangeStartTimeSlot: DatePickerTimeSlot,
      nextRangeEndTimeSlot: DatePickerTimeSlot,
      nextActiveRangeSelection: RangeSelectionTarget,
    ) => {
      const payload = buildConfirmPayload({
        mode: nextMode,
        singleDate: nextSingleDate,
        rangeDate: nextRangeDate,
        timeSlot: nextTimeSlot,
        rangeStartTimeSlot: nextRangeStartTimeSlot,
        rangeEndTimeSlot: nextRangeEndTimeSlot,
        activeRangeSelection: nextActiveRangeSelection,
      });
      onConfirm?.(payload.value, payload.meta);
    },
    [buildConfirmPayload, onConfirm],
  );
 
  return {
    emitConfirm,
  };
};