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

100% Statements 55/55
80.85% Branches 38/47
100% Functions 14/14
100% Lines 53/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 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433                                                                  1x                     1x               20x 20x   20x 20x     20x 20x 10x 10x         20x 20x     20x       20x           20x           20x 20x     20x   20x           20x     20x 20x         20x 20x         20x 20x   20x                                                               1x       11x 11x                             11x   11x   360x 360x             360x       360x             11x   450x   400x 400x   400x       400x 50x   50x   50x 30x       370x         11x 1x                     10x         1x                     1x                                                                                                                       11x                                                                                                                                                                                                                                                            
import React, { useCallback, useEffect } from 'react';
import Animated, {
  interpolateColor,
  useAnimatedStyle,
  useSharedValue,
  withSpring,
  withTiming,
} from 'react-native-reanimated';
import {
  ActivityIndicator,
  Pressable,
  StyleSheet,
  Text,
  View,
} from 'react-native';
 
import WheelPicker from '@quidone/react-native-wheel-picker';
 
import { ClockIcon } from '@repo/ui/icons/Clock';
import { makeStyles } from '@repo/ui/themes/makeStyles';
import { useTheme } from '@repo/ui/themes/ThemeContext';
import { textStyles } from '@repo/ui/themes/typography';
 
import { useChooseTime } from './hook/useChooseTime';
import {
  HOURS_DATA,
  ITEM_HEIGHT,
  MINUTES_DATA,
  PICKER_HEIGHT,
  PickerItem,
} from './utils';
import WheelPickerItem from './WheelPickerItem';
 
const AnimatedPressable = Animated.createAnimatedComponent(Pressable);
 
interface TimeTabButtonProps {
  active: boolean;
  onPress: () => void;
  label: string;
  testID: string;
  theme: any;
  styles: any;
}
 
const TimeTabButton = ({
  active,
  onPress,
  label,
  testID,
  theme,
  styles,
}: TimeTabButtonProps) => {
  const progress = useSharedValue(active ? 1 : 0);
  const scale = useSharedValue(1);
 
  useEffect(() => {
    progress.value = withTiming(active ? 1 : 0, { duration: 200 });
  }, [active, progress]);
 
  useEffect(() => {
    if (active) {
      scale.value = withSpring(1.05, { damping: 15, stiffness: 200 }, () => {
        scale.value = withSpring(1.0);
      });
    }
  }, [active, scale]);
 
  const animatedButtonStyle = useAnimatedStyle(() => {
    const inactiveBg = theme.isNewTheme
      ? theme.colors.background.tertiary
      : theme.colors.slate15;
    const activeBg = theme.isNewTheme
      ? theme.colors.background.selectedStrong
      : theme.colors.slate97;
 
    const backgroundColor = interpolateColor(
      progress.value,
      [0, 1],
      [inactiveBg, activeBg],
    );
 
    return {
      backgroundColor,
      transform: [{ scale: scale.value }],
    };
  }, [progress, scale, theme]);
 
  const animatedTextStyle = useAnimatedStyle(() => {
    const inactiveText = theme.isNewTheme
      ? theme.colors.text.onBehalf
      : theme.colors.slate97;
    const activeText = theme.colors.text.white;
 
    const color = interpolateColor(
      progress.value,
      [0, 1],
      [inactiveText, activeText],
    );
 
    return { color };
  }, [progress, theme]);
 
  const animatedActiveIconStyle = useAnimatedStyle(() => {
    return {
      opacity: progress.value,
    };
  }, [progress]);
 
  const animatedInactiveIconStyle = useAnimatedStyle(() => {
    return {
      opacity: 1 - progress.value,
    };
  }, [progress]);
 
  const activeIconColor = theme.colors.text.white;
  const inactiveIconColor = theme.colors.text.onBehalf;
 
  return (
    <AnimatedPressable
      onPress={onPress}
      testID={testID}
      style={[styles.tabButton, animatedButtonStyle]}
      accessibilityRole="button"
      accessibilityState={{ selected: active }}
    >
      <View style={styles.tabIconContainer}>
        <Animated.View
          style={[StyleSheet.absoluteFill, animatedActiveIconStyle]}
        >
          <ClockIcon width={18} height={18} color={activeIconColor} />
        </Animated.View>
        <Animated.View
          style={[StyleSheet.absoluteFill, animatedInactiveIconStyle]}
        >
          <ClockIcon width={18} height={18} color={inactiveIconColor} />
        </Animated.View>
      </View>
      <Animated.Text style={[styles.tabText, animatedTextStyle]}>
        {label}
      </Animated.Text>
    </AnimatedPressable>
  );
};
 
interface ChooseTimeProps {
  activeTimeTab: 'start' | 'end';
  onChangeActiveTimeTab: (tab: 'start' | 'end') => void;
}
 
const ChooseTime = ({
  activeTimeTab,
  onChangeActiveTimeTab,
}: ChooseTimeProps) => {
  const styles = useStyles();
  const { theme } = useTheme();
 
  const {
    startTime,
    endTime,
    hourPickerValue,
    minutePickerValue,
    isReady,
    isLoadingMeetingRoomsBooked,
    selectedHour,
    handleHourChange,
    handleMinuteChange,
    handleValueChanging,
    checkIsTimeSlotBooked,
    formDate,
  } = useChooseTime({ activeTimeTab });
 
  const renderMinutePickerItem = useCallback(
    ({ item }: { item: PickerItem }) => {
      const isEndTime = activeTimeTab === 'end';
      const isBooked = checkIsTimeSlotBooked(
        selectedHour,
        item.label,
        isEndTime,
      );
 
      const isInvalidOvertime =
        activeTimeTab === 'start' &&
        selectedHour === '17' &&
        (item.label === '30' || item.label === '45');
 
      return (
        <WheelPickerItem item={item} isBooked={isBooked || isInvalidOvertime} />
      );
    },
    [selectedHour, checkIsTimeSlotBooked, activeTimeTab],
  );
 
  const renderHourPickerItem = useCallback(
    ({ item }: { item: PickerItem }) => {
      if (!formDate) return <WheelPickerItem item={item} />;
 
      const selectedDate = new Date(formDate);
      const today = new Date();
      const isToday =
        selectedDate.getDate() === today.getDate() &&
        selectedDate.getMonth() === today.getMonth() &&
        selectedDate.getFullYear() === today.getFullYear();
 
      if (isToday) {
        const hourMaxMins = parseInt(item.label, 10) * 60 + 45;
        const currentMins =
          Math.ceil((today.getHours() * 60 + today.getMinutes()) / 30) * 30;
 
        if (hourMaxMins < currentMins) {
          return <WheelPickerItem item={item} isBooked={true} />;
        }
      }
 
      return <WheelPickerItem item={item} />;
    },
    [formDate],
  );
 
  if (isLoadingMeetingRoomsBooked)
    return (
      <View
        style={styles.loadingContainer}
        testID="room-loader"
        accessibilityRole="progressbar"
        accessibilityLabel="Loading rooms"
      >
        <ActivityIndicator />
      </View>
    );
 
  return (
    <View style={styles.container} testID="choose-time-screen">
      <View style={styles.tabsRow}>
        <TimeTabButton
          active={activeTimeTab === 'start'}
          onPress={() => onChangeActiveTimeTab('start')}
          label={startTime || '08:00'}
          testID="start-time-tab"
          theme={theme}
          styles={styles}
        />
 
        <Text style={styles.separator}>-</Text>
 
        <TimeTabButton
          active={activeTimeTab === 'end'}
          onPress={() => onChangeActiveTimeTab('end')}
          label={endTime || '09:00'}
          testID="end-time-tab"
          theme={theme}
          styles={styles}
        />
      </View>
 
      <View style={styles.pickerCardShadow}>
        <View style={styles.pickerCard}>
          <View style={styles.centerHighlightBar} pointerEvents="none" />
          <View style={styles.listsContainer}>
            {isReady ? (
              <>
                <View style={styles.pickerWrapper}>
                  <WheelPicker
                    testID="hour-picker-list"
                    value={hourPickerValue}
                    data={HOURS_DATA}
                    onValueChanged={handleHourChange}
                    itemHeight={ITEM_HEIGHT}
                    style={styles.nativePicker}
                    itemTextStyle={styles.itemText}
                    onValueChanging={handleValueChanging}
                    renderOverlay={null}
                    width="100%"
                    renderItem={renderHourPickerItem}
                  />
                </View>
 
                <View style={styles.colonContainer}>
                  <Text style={styles.colonText}>:</Text>
                </View>
 
                <View style={styles.pickerWrapper}>
                  <WheelPicker
                    testID="minute-picker-list"
                    value={minutePickerValue}
                    data={MINUTES_DATA}
                    onValueChanging={handleValueChanging}
                    onValueChanged={handleMinuteChange}
                    itemHeight={ITEM_HEIGHT}
                    style={styles.nativePicker}
                    itemTextStyle={styles.itemText}
                    renderOverlay={null}
                    width="100%"
                    renderItem={renderMinutePickerItem}
                  />
                </View>
              </>
            ) : null}
          </View>
        </View>
      </View>
    </View>
  );
};
 
export default ChooseTime;
 
const useStyles = makeStyles(theme => ({
  container: {
    flex: 1,
    paddingHorizontal: theme.metrics.spacing[6],
    backgroundColor: theme.colors.background.pageMuted,
    gap: theme.metrics.spacing[6],
  },
  tabsRow: {
    flexDirection: 'row',
    alignItems: 'center',
    justifyContent: 'space-between',
    gap: theme.metrics.spacing[3],
  },
  tabButton: {
    flexDirection: 'row',
    alignItems: 'center',
    justifyContent: 'center',
    gap: theme.metrics.spacing[2],
    paddingVertical: theme.metrics.spacing[3],
    width: 150,
    borderRadius: theme.metrics.borderRadius[3],
    backgroundColor: theme.isNewTheme
      ? theme.colors.background.tertiary
      : theme.colors.slate15,
  },
  tabIconContainer: {
    width: 18,
    height: 18,
    position: 'relative',
  },
  activeTabButton: {
    backgroundColor: theme.isNewTheme
      ? theme.colors.background.selectedStrong
      : theme.colors.slate97,
  },
  tabText: {
    ...textStyles.content.semiBold,
    fontSize: theme.metrics.textSize[16],
    fontWeight: theme.metrics.fontWeight.bold,
    color: theme.isNewTheme ? theme.colors.text.onBehalf : theme.colors.slate97,
  },
  activeTabText: {
    color: theme.colors.text.white,
  },
  separator: {
    ...textStyles.content.semiBold,
    fontSize: theme.metrics.textSize[20],
    fontWeight: theme.metrics.fontWeight.bold,
    color: theme.colors.text.default,
  },
  pickerCardShadow: {
    borderRadius: 20,
    backgroundColor: theme.colors.background.default,
    shadowColor: theme.colors.shadow.default,
    shadowOffset: { width: 0, height: 3 },
    shadowOpacity: 0.18,
    shadowRadius: 10,
    elevation: 5,
  },
  pickerCard: {
    backgroundColor: theme.colors.background.default,
    borderRadius: 20,
    borderWidth: theme.metrics.borderWidth.default,
    borderColor: theme.colors.border.divider,
    height: PICKER_HEIGHT,
    position: 'relative',
    overflow: 'hidden',
    justifyContent: 'center',
    alignItems: 'center',
    paddingHorizontal: 60,
  },
  centerHighlightBar: {
    position: 'absolute',
    height: ITEM_HEIGHT,
    left: 60,
    right: 60,
    backgroundColor: theme.isNewTheme
      ? theme.colors.background.tertiary
      : theme.colors.gray5,
    borderRadius: 10,
    zIndex: 1,
  },
  listsContainer: {
    flexDirection: 'row',
    alignItems: 'center',
    justifyContent: 'center',
    width: '100%',
    height: PICKER_HEIGHT,
    zIndex: 2,
  },
  pickerWrapper: {
    flex: 1,
    height: PICKER_HEIGHT,
    justifyContent: 'center',
  },
  nativePicker: {
    backgroundColor: 'transparent',
    width: '100%',
    height: PICKER_HEIGHT,
  },
  colonContainer: {
    justifyContent: 'center',
    alignItems: 'center',
    width: 24,
    height: ITEM_HEIGHT,
  },
  colonText: {
    ...textStyles.content.semiBold,
    fontSize: 24,
    fontWeight: '700',
    color: theme.isNewTheme ? theme.colors.text.onBehalf : theme.colors.slate97,
    textAlign: 'center',
  },
  itemText: {
    fontSize: 24,
    fontWeight: '500',
    color: theme.isNewTheme ? theme.colors.text.onBehalf : theme.colors.slate97,
    textAlign: 'center',
    lineHeight: ITEM_HEIGHT,
  },
  loadingContainer: {
    flex: 1,
    alignItems: 'center',
    marginTop: 32,
  },
}));