All files / apps/reallocation/src/screens/ChooseRoom index.tsx

92.06% Statements 58/63
72.72% Branches 32/44
93.33% Functions 14/15
94.73% Lines 54/57

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                                                      1x             1x 6x     6x 6x   6x 6x     6x         6x   6x 6x     6x 6x 6x       6x 6x   4x 4x 7x 7x 7x   7x 1x 1x             6x 6x   6x               6x               4x 6x 6x       4x 4x 3x 3x   3x   3x 3x   3x 3x       4x     6x 6x   6x 1x                   5x 1x               4x 7x 2x 1x     7x                               4x                               1x                            
import React, { useEffect, useMemo } from 'react';
import { ActivityIndicator, StyleSheet, Text, View } from 'react-native';
 
import { FlashList } from '@shopify/flash-list';
 
import { RoomCard } from '@repo/ui/components/RoomCard';
 
import { useGetMeInfo } from '@repo/hooks/useUserQueries';
 
import { useGetOffice } from '@/hooks/useRoom';
 
import {
  useReallocationActionsSelector,
  useReallocationStateSelector,
} from '../Reallocation/context';
 
type OfficeApiItem = {
  id: string;
  office?: string;
};
 
type RoomItem = {
  room: string;
  location: string;
  rawId: string;
};
 
const LOCATION_PRIORITY: Record<string, number> = {
  '604 NT': 1,
  'TL 18': 2,
  HPT: 3,
  HOME: 4,
};
 
const ChooseRoom = () => {
  const { form } = useReallocationStateSelector(state => ({
    form: state.form,
  }));
  const changeRoom = useReallocationActionsSelector(
    actions => actions.changeRoom,
  );
  const changeOriginRoom = useReallocationActionsSelector(
    actions => actions.changeOriginRoom,
  );
 
  const { officeData, isLoadingOffice, isErrorOffice } = useGetOffice();
  const {
    data: meInfo,
    isLoading: isLoadingMeInfo,
    isError: isGetMeInfoError,
  } = useGetMeInfo();
 
  const currentRoom = useMemo(() => {
    return meInfo?.room?.id || '';
  }, [meInfo]);
 
  useEffect(() => {
    Eif (currentRoom) {
      changeOriginRoom(currentRoom);
    }
  }, [currentRoom, changeOriginRoom]);
 
  const rooms = useMemo(() => {
    if (!Array.isArray(officeData)) return [];
 
    const transform = (data: OfficeApiItem[]): RoomItem[] => {
      return data.map(item => {
        const id = item.id || '';
        const office = item.office || '';
        const hasDash = id.includes('-');
 
        if (!hasDash) {
          const name = office || id;
          return {
            room: '',
            location: name,
            rawId: id,
          };
        }
 
        const parts = id.split('-');
        const roomNum = parts[1] || '';
 
        Iif (office.toUpperCase().replace(' ', '') === 'TL18') {
          return {
            room: roomNum ? `${roomNum}F` : office,
            location: 'TL 18',
            rawId: id,
          };
        }
 
        return {
          room: roomNum || office,
          location: office ? `${office} NT` : '',
          rawId: id,
        };
      });
    };
 
    const getParts = (room: string): [number, string] => {
      const match = room?.match?.(/(\d+)([A-Z])/);
      Eif (!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;
 
        Iif (locA !== locB) return locA - locB;
 
        const [numA, charA] = getParts(a.room);
        const [numB, charB] = getParts(b.room);
 
        Iif (numA !== numB) return numA - numB;
        return charA.localeCompare(charB);
      });
    };
 
    return sortRooms(transform(officeData));
  }, [officeData]);
 
  const isLoading = isLoadingOffice || isLoadingMeInfo;
  const isError = isErrorOffice || isGetMeInfoError;
 
  if (isLoading)
    return (
      <View
        style={styles.loadingContainer}
        testID="room-loader"
        accessibilityRole="progressbar"
        accessibilityLabel="Loading rooms"
      >
        <ActivityIndicator />
      </View>
    );
  if (isError)
    return (
      <View style={styles.loadingContainer} testID="room-error">
        <Text accessibilityRole="alert">
          Failed to load rooms. Please try again later.
        </Text>
      </View>
    );
 
  const renderItem = ({ item }: { item: RoomItem }) => {
    const handlePress = () => {
      if (item.rawId === currentRoom) return;
      changeRoom(item.rawId);
    };
 
    return (
      <View
        style={styles.itemWrapper}
        testID={`room-card-wrapper-${item.rawId}`}
      >
        <RoomCard
          room={item.room}
          location={item.location}
          isCurrent={item.rawId === currentRoom}
          isSelected={item.rawId === form.room}
          onPress={handlePress}
        />
      </View>
    );
  };
 
  return (
    <FlashList
      data={rooms}
      keyExtractor={item => item.rawId}
      renderItem={renderItem}
      numColumns={3}
      contentContainerStyle={styles.container}
      showsVerticalScrollIndicator={false}
      accessibilityRole="list"
      accessibilityLabel="Available rooms"
    />
  );
};
 
export default ChooseRoom;
 
const styles = StyleSheet.create({
  container: {
    padding: 16,
  },
  loadingContainer: {
    flex: 1,
    alignItems: 'center',
    marginTop: 32,
  },
  itemWrapper: {
    padding: 2,
    flex: 1,
  },
});