feat: Add alerts toggle in Dashboard

This commit is contained in:
haneulai 2026-04-14 16:40:51 +09:00
parent a1f8c74ec8
commit 0e0b64f20f

59
App.js
View File

@ -8,6 +8,7 @@ import {
ActivityIndicator, ActivityIndicator,
useWindowDimensions, useWindowDimensions,
RefreshControl, RefreshControl,
Switch,
} from 'react-native'; } from 'react-native';
import { StatusBar } from 'expo-status-bar'; import { StatusBar } from 'expo-status-bar';
import { NavigationContainer } from '@react-navigation/native'; import { NavigationContainer } from '@react-navigation/native';
@ -50,6 +51,42 @@ function DashboardScreen({ navigation }) {
fetchData(); fetchData();
}, [fetchData]); }, [fetchData]);
const handleToggleAlert = async (plant, newValue) => {
try {
setData((prev) => {
if (!prev) return prev;
const newData = { ...prev };
newData.data = newData.data.map(p =>
p.id === plant.id ? { ...p, alerts_enabled: newValue } : p
);
return newData;
});
const response = await fetch(`https://solorpower.dadot.net/plants/${plant.company_id || 1}/${plant.id}/alerts`, {
method: 'PATCH',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ alerts_enabled: newValue }),
});
if (!response.ok) {
throw new Error('Failed to update alert settings');
}
} catch (err) {
console.error(err);
setData((prev) => {
if (!prev) return prev;
const newData = { ...prev };
newData.data = newData.data.map(p =>
p.id === plant.id ? { ...p, alerts_enabled: !newValue } : p
);
return newData;
});
alert('알림 설정 변경에 실패했습니다.');
}
};
const onRefresh = useCallback(() => { const onRefresh = useCallback(() => {
setRefreshing(true); setRefreshing(true);
fetchData(); fetchData();
@ -218,7 +255,17 @@ function DashboardScreen({ navigation }) {
{statusInfo.label} {statusInfo.label}
</Text> </Text>
</View> </View>
<Text style={styles.arrowIcon}></Text> <View style={styles.alertToggleContainer}>
<Text style={styles.alertToggleLabel}>
{plant.alerts_enabled !== false ? '알림 ON' : '알림 OFF'}
</Text>
<Switch
value={plant.alerts_enabled !== false}
onValueChange={(value) => handleToggleAlert(plant, value)}
trackColor={{ false: '#D1D5DB', true: '#3B82F6' }}
thumbColor={'#FFFFFF'}
/>
</View>
</View> </View>
<View style={styles.cardBody}> <View style={styles.cardBody}>
@ -457,6 +504,16 @@ const styles = StyleSheet.create({
color: '#9CA3AF', color: '#9CA3AF',
fontWeight: '300', fontWeight: '300',
}, },
alertToggleContainer: {
flexDirection: 'row',
alignItems: 'center',
gap: 8,
},
alertToggleLabel: {
fontSize: 12,
color: '#6B7280',
fontWeight: '500',
},
cardBody: { cardBody: {
gap: 8, gap: 8,
}, },