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

98.07% Statements 51/52
84.61% Branches 22/26
92.3% Functions 12/13
100% Lines 45/45

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                                            1x         1x 8x 8x     8x 8x       8x   8x 8x   5x 5x 11x 11x   11x 11x   11x 1x             10x 8x             2x               5x 12x 12x 11x     5x 5x 8x 8x   8x   6x 6x   6x 2x       5x     8x   8x 1x                   7x 1x               6x 11x 1x     11x                               6x                               8x                            
import React, { useMemo } from 'react';
import { ActivityIndicator, Text, View } from 'react-native';
 
import { FlashList } from '@shopify/flash-list';
 
import { RoomCard } from '@repo/ui/components/RoomCard';
import { makeStyles } from '@repo/ui/themes/makeStyles';
import { textStyles } from '@repo/ui/themes/typography';
 
import { MeetingRoomItem, useGetMeetingRooms } from '@/hooks/useRoom';
 
import {
  useRoomBookingActionsSelector,
  useRoomBookingStateSelector,
} from '../RoomBooking/context';
 
type RoomItem = {
  room: string;
  location: string;
  rawId: string;
};
 
const LOCATION_PRIORITY: Record<string, number> = {
  '604': 1,
  'TL 18': 2,
};
 
const ChooseLocation = () => {
  const styles = useStyles();
  const { form } = useRoomBookingStateSelector(state => ({
    form: state.form,
  }));
  const changeRoom = useRoomBookingActionsSelector(
    actions => actions.changeRoom,
  );
 
  const { meetingRooms, isLoadingMeetingRooms, isErrorMeetingRooms } =
    useGetMeetingRooms();
 
  const rooms = useMemo(() => {
    if (!Array.isArray(meetingRooms)) return [];
 
    const transform = (data: MeetingRoomItem[]): RoomItem[] => {
      return data.map(item => {
        const id = item.id || '';
        const parts = id.split('-');
 
        const roomPrefix = parts[0] || '';
        const roomSuffix = parts[1] || '';
 
        if (roomPrefix.toUpperCase() === 'TL18') {
          return {
            room: roomSuffix,
            location: 'TL 18',
            rawId: id,
          };
        }
 
        if (roomPrefix.toUpperCase() === '604') {
          return {
            room: roomSuffix,
            location: '604',
            rawId: id,
          };
        }
 
        return {
          room: roomSuffix,
          location: roomPrefix,
          rawId: id,
        };
      });
    };
 
    const getParts = (room: string): [number, string] => {
      const match = room?.match?.(/(\d+)([A-Z])/);
      if (!match) return [999, 'Z'];
      return [Number(match[1]), match[2]];
    };
 
    const sortRooms = (office: RoomItem[]) => {
      return office.sort((a, b) => {
        const locA = LOCATION_PRIORITY[a.location] ?? 999;
        const locB = LOCATION_PRIORITY[b.location] ?? 999;
 
        if (locA !== locB) return locA - locB;
 
        const [numA, charA] = getParts(a.room);
        const [numB, charB] = getParts(b.room);
 
        if (numA !== numB) return numA - numB;
        return charA.localeCompare(charB);
      });
    };
 
    return sortRooms(transform(meetingRooms));
  }, [meetingRooms]);
 
  const keyExtractor = (item: RoomItem) => item.rawId;
 
  if (isLoadingMeetingRooms)
    return (
      <View
        style={styles.loadingContainer}
        testID="room-loader"
        accessibilityRole="progressbar"
        accessibilityLabel="Loading rooms"
      >
        <ActivityIndicator />
      </View>
    );
  if (isErrorMeetingRooms)
    return (
      <View style={styles.loadingContainer} testID="room-error">
        <Text style={textStyles.content.regular} accessibilityRole="alert">
          Failed to load rooms. Please try again later.
        </Text>
      </View>
    );
 
  const renderItem = ({ item }: { item: RoomItem }) => {
    const handlePress = () => {
      changeRoom(item.rawId);
    };
 
    return (
      <View
        style={styles.itemWrapper}
        testID={`room-card-wrapper-${item.rawId}`}
      >
        <RoomCard
          room={item.room}
          variant="primary"
          location={item.location}
          isSelected={item.rawId === form.room}
          onPress={handlePress}
        />
      </View>
    );
  };
 
  return (
    <FlashList
      data={rooms}
      keyExtractor={keyExtractor}
      renderItem={renderItem}
      numColumns={2}
      contentContainerStyle={styles.container}
      showsVerticalScrollIndicator={false}
      accessibilityRole="list"
      accessibilityLabel="Available meeting rooms"
    />
  );
};
 
export default ChooseLocation;
 
const useStyles = makeStyles(theme => ({
  container: {
    paddingHorizontal: theme.metrics.spacing[4],
  },
  loadingContainer: {
    flex: 1,
    alignItems: 'center',
    marginTop: theme.metrics.spacing[8],
  },
  itemWrapper: {
    padding: theme.metrics.spacing[0.5],
    flex: 1,
  },
}));