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 * as Device from 'expo-device';
|
||||||
import { useBeneficiary } from '@/contexts/BeneficiaryContext';
|
import { useBeneficiary } from '@/contexts/BeneficiaryContext';
|
||||||
import { useBLE } from '@/contexts/BLEContext';
|
import { useBLE } from '@/contexts/BLEContext';
|
||||||
import { api } from '@/services/api';
|
import { api, ROOM_LOCATIONS } from '@/services/api';
|
||||||
import type { WPSensor } from '@/types';
|
import type { WPSensor } from '@/types';
|
||||||
import {
|
import {
|
||||||
AppColors,
|
AppColors,
|
||||||
@ -54,34 +54,25 @@ export default function EquipmentScreen() {
|
|||||||
}, [id]);
|
}, [id]);
|
||||||
|
|
||||||
const loadSensors = async () => {
|
const loadSensors = async () => {
|
||||||
console.log('[Equipment] loadSensors called, beneficiaryId:', id);
|
|
||||||
if (!id) {
|
if (!id) {
|
||||||
console.log('[Equipment] No beneficiary ID, skipping load');
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
setIsLoading(true);
|
setIsLoading(true);
|
||||||
console.log('[Equipment] Calling api.getDevicesForBeneficiary...');
|
|
||||||
|
|
||||||
// Get WP sensors from API (attached to beneficiary)
|
// Get WP sensors from API (attached to beneficiary)
|
||||||
const response = await api.getDevicesForBeneficiary(id);
|
const response = await api.getDevicesForBeneficiary(id);
|
||||||
|
|
||||||
console.log('[Equipment] API Response:', JSON.stringify(response, null, 2));
|
|
||||||
|
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
// If error is "Not authenticated with Legacy API" or network error,
|
// If error is "Not authenticated with Legacy API" or network error,
|
||||||
// just show empty state without Alert
|
// just show empty state without Alert
|
||||||
console.warn('[Equipment] Could not load sensors:', response.error);
|
|
||||||
setApiSensors([]);
|
setApiSensors([]);
|
||||||
return;
|
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 || []);
|
setApiSensors(response.data || []);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('[Equipment] Failed to load sensors:', error);
|
|
||||||
// Show empty state instead of Alert
|
// Show empty state instead of Alert
|
||||||
setApiSensors([]);
|
setApiSensors([]);
|
||||||
} finally {
|
} finally {
|
||||||
@ -171,7 +162,6 @@ export default function EquipmentScreen() {
|
|||||||
|
|
||||||
Alert.alert('Success', `${sensor.name} has been detached.`);
|
Alert.alert('Success', `${sensor.name} has been detached.`);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('[Equipment] Failed to detach sensor:', error);
|
|
||||||
Alert.alert('Error', 'Failed to detach sensor. Please try again.');
|
Alert.alert('Error', 'Failed to detach sensor. Please try again.');
|
||||||
} finally {
|
} finally {
|
||||||
setIsDetaching(null);
|
setIsDetaching(null);
|
||||||
@ -207,7 +197,6 @@ export default function EquipmentScreen() {
|
|||||||
setApiSensors([]);
|
setApiSensors([]);
|
||||||
Alert.alert('Success', 'All sensors have been detached.');
|
Alert.alert('Success', 'All sensors have been detached.');
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('[Equipment] Failed to detach all sensors:', error);
|
|
||||||
Alert.alert('Error', 'Failed to detach sensors. Please try again.');
|
Alert.alert('Error', 'Failed to detach sensors. Please try again.');
|
||||||
} finally {
|
} finally {
|
||||||
setIsLoading(false);
|
setIsLoading(false);
|
||||||
@ -270,6 +259,17 @@ export default function EquipmentScreen() {
|
|||||||
return 'Weak';
|
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) {
|
if (isLoading) {
|
||||||
return (
|
return (
|
||||||
<SafeAreaView style={styles.container} edges={['top', 'bottom']}>
|
<SafeAreaView style={styles.container} edges={['top', 'bottom']}>
|
||||||
@ -419,7 +419,7 @@ export default function EquipmentScreen() {
|
|||||||
styles.deviceLocation,
|
styles.deviceLocation,
|
||||||
!sensor.location && styles.deviceLocationPlaceholder
|
!sensor.location && styles.deviceLocationPlaceholder
|
||||||
]}>
|
]}>
|
||||||
{sensor.location || 'No location set'}
|
{getLocationDisplay(sensor.location) || 'No location set'}
|
||||||
</Text>
|
</Text>
|
||||||
</TouchableOpacity>
|
</TouchableOpacity>
|
||||||
</View>
|
</View>
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user