feat(sensors): Convert location code to display name in equipment list
Add getLocationDisplay() helper to convert location ID (e.g., 'bedroom') to human-readable format with icon (e.g., '🛏️ Bedroom') using ROOM_LOCATIONS. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
parent
3bc0d2a8a9
commit
63b8ae5007
@ -17,7 +17,7 @@ import { router, useLocalSearchParams } from 'expo-router';
|
||||
import * as Device from 'expo-device';
|
||||
import { useBeneficiary } from '@/contexts/BeneficiaryContext';
|
||||
import { useBLE } from '@/contexts/BLEContext';
|
||||
import { api } from '@/services/api';
|
||||
import { api, ROOM_LOCATIONS } from '@/services/api';
|
||||
import type { WPSensor } from '@/types';
|
||||
import {
|
||||
AppColors,
|
||||
@ -54,34 +54,25 @@ export default function EquipmentScreen() {
|
||||
}, [id]);
|
||||
|
||||
const loadSensors = async () => {
|
||||
console.log('[Equipment] loadSensors called, beneficiaryId:', id);
|
||||
if (!id) {
|
||||
console.log('[Equipment] No beneficiary ID, skipping load');
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
setIsLoading(true);
|
||||
console.log('[Equipment] Calling api.getDevicesForBeneficiary...');
|
||||
|
||||
// Get WP sensors from API (attached to beneficiary)
|
||||
const response = await api.getDevicesForBeneficiary(id);
|
||||
|
||||
console.log('[Equipment] API Response:', JSON.stringify(response, null, 2));
|
||||
|
||||
if (!response.ok) {
|
||||
// If error is "Not authenticated with Legacy API" or network error,
|
||||
// just show empty state without Alert
|
||||
console.warn('[Equipment] Could not load sensors:', response.error);
|
||||
setApiSensors([]);
|
||||
return;
|
||||
}
|
||||
|
||||
console.log('[Equipment] Sensors loaded successfully, count:', response.data?.length || 0);
|
||||
console.log('[Equipment] Sensors data:', JSON.stringify(response.data, null, 2));
|
||||
setApiSensors(response.data || []);
|
||||
} catch (error) {
|
||||
console.error('[Equipment] Failed to load sensors:', error);
|
||||
// Show empty state instead of Alert
|
||||
setApiSensors([]);
|
||||
} finally {
|
||||
@ -171,7 +162,6 @@ export default function EquipmentScreen() {
|
||||
|
||||
Alert.alert('Success', `${sensor.name} has been detached.`);
|
||||
} catch (error) {
|
||||
console.error('[Equipment] Failed to detach sensor:', error);
|
||||
Alert.alert('Error', 'Failed to detach sensor. Please try again.');
|
||||
} finally {
|
||||
setIsDetaching(null);
|
||||
@ -207,7 +197,6 @@ export default function EquipmentScreen() {
|
||||
setApiSensors([]);
|
||||
Alert.alert('Success', 'All sensors have been detached.');
|
||||
} catch (error) {
|
||||
console.error('[Equipment] Failed to detach all sensors:', error);
|
||||
Alert.alert('Error', 'Failed to detach sensors. Please try again.');
|
||||
} finally {
|
||||
setIsLoading(false);
|
||||
@ -270,6 +259,17 @@ export default function EquipmentScreen() {
|
||||
return 'Weak';
|
||||
};
|
||||
|
||||
// Convert location ID to display label with icon
|
||||
const getLocationDisplay = (locationId: string | undefined): string => {
|
||||
if (!locationId) return '';
|
||||
const location = ROOM_LOCATIONS.find(l => l.id === locationId);
|
||||
if (location) {
|
||||
return `${location.icon} ${location.label}`;
|
||||
}
|
||||
// Fallback for unknown location IDs
|
||||
return locationId;
|
||||
};
|
||||
|
||||
if (isLoading) {
|
||||
return (
|
||||
<SafeAreaView style={styles.container} edges={['top', 'bottom']}>
|
||||
@ -419,7 +419,7 @@ export default function EquipmentScreen() {
|
||||
styles.deviceLocation,
|
||||
!sensor.location && styles.deviceLocationPlaceholder
|
||||
]}>
|
||||
{sensor.location || 'No location set'}
|
||||
{getLocationDisplay(sensor.location) || 'No location set'}
|
||||
</Text>
|
||||
</TouchableOpacity>
|
||||
</View>
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user