All files / apps/roomBooking/src/screens/ChooseDate index.tsx

98.18% Statements 54/55
91.66% Branches 22/24
100% Functions 12/12
98.11% Lines 52/53

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 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219                                                                                          1x 14x 14x   14x 14x 14x 14x   14x 14x   14x 14x     14x 14x       14x   14x 13x 1x     12x 2x 2x     10x 2x     8x 8x               14x 2x   2x 1x     2x               14x 13x 11x     2x       2x     14x   2x 1x     1x         14x   2x 2x 1x     1x         14x 1x       1x         1x 1x     1x 1x     14x                                                                                           14x                          
import React, {
  useCallback,
  useEffect,
  useMemo,
  useRef,
  useState,
} from 'react';
import { Pressable, View } from 'react-native';
 
import { useNavigation } from '@react-navigation/native';
import { NativeStackNavigationProp } from '@react-navigation/native-stack';
 
import { ChooseDateShell } from '@repo/ui/components/ChooseDateShell';
import {
  DATE_PICKER_MODE,
  DATE_PICKER_MODE_TYPE,
  DATE_PICKER_TIME_SLOT,
  DatePicker,
  type DateRangeValue,
} from '@repo/ui/components/DatePicker';
import {
  QuickSelect,
  type QuickSelectValue,
} from '@repo/ui/components/QuickSelect';
import { findMatchingQuickSelectValue } from '@repo/ui/components/QuickSelect/utils';
import { MoreIcon } from '@repo/ui/icons/More';
import { makeStyles } from '@repo/ui/themes/makeStyles';
import { useTheme } from '@repo/ui/themes/ThemeContext';
 
import {
  normalizeDateValue,
  toQuickSelectValue as sharedToQuickSelectValue,
} from '@repo/utils';
 
import {
  useRoomBookingActionsSelector,
  useRoomBookingStateSelector,
} from '../RoomBooking/context';
import {
  RoomBookingStackParamList,
  RoomBookingStep,
} from '../RoomBooking/types';
 
import { useQuickSelectOptions } from './hook/useQuickSelectOptions';
 
const ChooseDate = () => {
  const styles = useStyles();
  const { theme } = useTheme();
  const navigation =
    useNavigation<NativeStackNavigationProp<RoomBookingStackParamList>>();
  const form = useRoomBookingStateSelector(state => state.form);
  const isDatePickerOpen = useRoomBookingStateSelector(
    state => state.isDatePickerOpen,
  );
  const setDatePickerOpen = useRoomBookingActionsSelector(
    actions => actions.setDatePickerOpen,
  );
  const changeDate = useRoomBookingActionsSelector(
    actions => actions.changeDate,
  );
  const { quickSelectOptions, defaultSuggestedValue, disabledDates } =
    useQuickSelectOptions();
  const [datePickerDate, setDatePickerDate] = useState<Date | null>(
    form.date || defaultSuggestedValue.startDate,
  );
 
  const initializedDefaultRef = useRef(false);
 
  useEffect(() => {
    if (initializedDefaultRef.current) {
      return;
    }
 
    if (form.date) {
      initializedDefaultRef.current = true;
      return;
    }
 
    if (!defaultSuggestedValue.startDate) {
      return;
    }
 
    changeDate(defaultSuggestedValue.startDate);
    initializedDefaultRef.current = true;
  }, [
    changeDate,
    defaultSuggestedValue.startDate,
    defaultSuggestedValue.timeSlot,
    form.date,
  ]);
 
  const toggleDatePicker = useCallback(() => {
    const next = !isDatePickerOpen;
 
    if (next) {
      setDatePickerDate(form.date || defaultSuggestedValue.startDate);
    }
 
    setDatePickerOpen(next);
  }, [
    defaultSuggestedValue.startDate,
    form.date,
    isDatePickerOpen,
    setDatePickerOpen,
  ]);
 
  const quickSelectValue = useMemo(() => {
    if (!form.date) {
      return null;
    }
 
    const currentSelection = sharedToQuickSelectValue(
      form.date,
      DATE_PICKER_TIME_SLOT.ALL_DAY,
    );
    return findMatchingQuickSelectValue(currentSelection, quickSelectOptions);
  }, [form.date, quickSelectOptions]);
 
  const handleChangeQuickSelect = useCallback(
    (value: QuickSelectValue) => {
      if (!value.startDate) {
        return;
      }
 
      changeDate(value.startDate);
    },
    [changeDate],
  );
 
  const handleSelectDate = useCallback(
    (value: Date | DateRangeValue | null) => {
      const nextDate = normalizeDateValue(value);
      if (!nextDate) {
        return;
      }
 
      setDatePickerDate(nextDate);
    },
    [],
  );
 
  const handleConfirm = useCallback(() => {
    Iif (!datePickerDate) {
      return;
    }
 
    const normalizedValue = sharedToQuickSelectValue(
      datePickerDate,
      DATE_PICKER_TIME_SLOT.ALL_DAY,
    );
 
    Eif (normalizedValue.startDate) {
      changeDate(normalizedValue.startDate);
    }
 
    setDatePickerOpen(false);
    navigation.navigate(RoomBookingStep.TIME);
  }, [changeDate, datePickerDate, navigation, setDatePickerOpen]);
 
  return (
    <>
      <View style={styles.container}>
        <QuickSelect
          options={quickSelectOptions}
          value={quickSelectValue}
          onChange={handleChangeQuickSelect}
          testID="choose-date-quick-select"
        />
 
        <Pressable
          onPress={toggleDatePicker}
          style={styles.moreIcon}
          accessibilityRole="button"
          testID="more-btn"
        >
          <MoreIcon color={theme.colors.icon.brand} />
        </Pressable>
      </View>
 
      <ChooseDateShell
        isOpen={isDatePickerOpen}
        onClose={toggleDatePicker}
        onConfirm={handleConfirm}
        isConfirmDisabled={!datePickerDate}
        headerTitle="BOOK A ROOM"
        headerDescription="We plan to book the room on"
        headerImage={theme.assets.bookRoomMascot}
      >
        <DatePicker
          key={`${isDatePickerOpen}-${(form?.date || datePickerDate)?.getTime()}`}
          mode={DATE_PICKER_MODE.SINGLE}
          modeType={DATE_PICKER_MODE_TYPE.DATE_WITH_TIME_OFF}
          disableStyleTimeSlot
          isTimeOffShowHalfDay={false}
          value={datePickerDate}
          disabledDates={disabledDates}
          onConfirm={handleSelectDate}
        />
      </ChooseDateShell>
    </>
  );
};
 
export default ChooseDate;
 
const useStyles = makeStyles(theme => ({
  container: {
    flex: 1,
    paddingHorizontal: theme.metrics.spacing[4],
    gap: theme.metrics.spacing[6],
    backgroundColor: theme.colors.background.pageMuted,
  },
  moreIcon: {
    borderRadius: theme.metrics.borderRadius.pill,
    width: theme.metrics.spacing[15],
    height: theme.metrics.spacing[15],
  },
}));