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

93.54% Statements 58/62
72.72% Branches 32/44
100% Functions 15/15
96.42% Lines 54/56

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                                                              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             4x     14x                             1x                                    
import React, { useEffect, useMemo } from 'react';
import {
  ActivityIndicator,
  FlatList,
  StyleSheet,
  Text,
  View,
} from 'react-native';
 
import { RoomCard } from '@repo/ui/components/RoomCard';
 
import { useGetMeInfo } from '@repo/hooks/userQueries';
 
import { useGetOffice } from '@/hooks/useRoom';
 
import {
  useReallocationV2ActionsSelector,
  useReallocationV2StateSelector,
} from '../ReallocationV2/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 } = useReallocationV2StateSelector(state => ({
    form: state.form,
  }));
  const changeRoom = useReallocationV2ActionsSelector(
    actions => actions.changeRoom,
  );
  const changeOriginRoom = useReallocationV2ActionsSelector(
    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">
        <ActivityIndicator />
      </View>
    );
  if (isError)
    return (
      <View style={styles.loadingContainer} testID="room-error">
        <Text>Failed to load rooms. Please try again later.</Text>
      </View>
    );
 
  const renderItem = ({ item }: { item: RoomItem }) => {
    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={() => {
            if (item.rawId === currentRoom) return;
            changeRoom(item.rawId);
          }}
        />
      </View>
    );
  };
 
  return (
    <FlatList
      data={rooms}
      keyExtractor={item => item.rawId}
      renderItem={renderItem}
      numColumns={3}
      contentContainerStyle={styles.container}
      columnWrapperStyle={styles.row}
      showsVerticalScrollIndicator={false}
    />
  );
};
 
export default ChooseRoom;
 
/* =======================
   STYLES
======================= */
const styles = StyleSheet.create({
  container: {
    padding: 16,
    gap: 4,
  },
  loadingContainer: {
    flex: 1,
    alignItems: 'center',
    marginTop: 32,
  },
  row: {
    gap: 4,
  },
  itemWrapper: {
    flex: 1,
    maxWidth: '33.33%',
  },
});