-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEdit.js
154 lines (145 loc) · 4.14 KB
/
Edit.js
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
import React, { useState, useEffect } from 'react';
import { View, Text, TextInput, TouchableOpacity, StyleSheet, ScrollView, Dimensions } from 'react-native';
import AsyncStorage from '@react-native-community/async-storage';
const { width, height } = Dimensions.get('window');
const Edit = ({ habits, setHabits }) => {
const [editedHabits, setEditedHabits] = useState([]);
useEffect(() => {
setEditedHabits(habits);
}, [habits]);
const handleSave = async () => {
setHabits(editedHabits);
await AsyncStorage.setItem('habits', JSON.stringify(editedHabits));
};
const updateField = (index, field, value) => {
const newHabits = [...editedHabits];
newHabits[index] = { ...newHabits[index], [field]: value ?? 0 };
setEditedHabits(newHabits);
};
return (
<View style={styles.container}>
<ScrollView style={styles.container}>
<Text style={{ fontSize: 40, marginLeft: 15, marginBottom: 15 }}>Edit</Text>
{editedHabits.map((habit, index) => (
<View key={index} style={styles.habitEditor}>
<View style={styles.habitHeader}>
<Text style={styles.habitTitle}>{habit.title}</Text>
<View style={styles.statsContainer}>
<View style={{ flexDirection: 'row' }}>
<View style={{ flexDirection: 'row' }}>
<Text style={styles.statText}>Current</Text>
<TextInput
style={styles.input}
defaultValue={habit.currentStreak ? habit.currentStreak.toString() : '0'}
onChangeText={(text) => updateField(index, 'currentStreak', text)}
/>
</View>
<View style={{ flexDirection: 'row' }}>
<Text style={styles.statText}>Longest</Text>
<TextInput
style={styles.input}
defaultValue={habit.longestStreak ? habit.longestStreak.toString() : '0'}
onChangeText={(text) => updateField(index, 'longestStreak', text)}
/>
</View>
</View>
<View style={{ flexDirection: 'row' }}>
<View style={{ flexDirection: 'row' }}>
<Text style={styles.statText}>Total</Text>
<TextInput
style={styles.input}
defaultValue={habit.total ? habit.total.toString() : '0'}
onChangeText={(text) => updateField(index, 'total', text)}
/>
</View>
<View style={{ flexDirection: 'row' }}>
<Text style={styles.statText}>Missed</Text>
<TextInput
style={styles.input}
defaultValue={habit.missed ? habit.missed.toString() : '0'}
onChangeText={(text) => updateField(index, 'missed', text)}
/>
</View>
</View>
</View>
<Text style={styles.label}>Last Checked Date:</Text>
<TextInput
style={styles.input}
value={habit.lastCheckedDate}
onChangeText={(text) => updateField(index, 'lastCheckedDate', text)}
/>
</View>
</View>
))}
<View style={{ height: 70 }}></View>
</ScrollView>
<TouchableOpacity onPress={handleSave} style={styles.saveButton}>
<Text style={styles.saveButtonText}>Save Changes</Text>
</TouchableOpacity>
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
width,
marginTop: 25,
},
habitEditor: {
marginBottom: 20,
borderWidth: 1,
paddingBottom: 10,
borderRadius: 15,
marginHorizontal: 15,
},
habitHeader: {
// flexDirection: 'row',
alignItems: 'center',
justifyContent: 'space-between',
marginBottom: 10,
},
habitTitle: {
fontSize: 24,
margin: 10,
fontWeight: 'bold',
},
statsContainer: {
flexDirection: 'column',
},
statText: {
fontSize: 16,
padding: 10,
marginHorizontal: 10,
borderWidth: 1,
borderColor: 'black',
borderRadius: 5,
marginVertical: 5,
},
label: {
fontWeight: 'bold',
marginTop: 5,
},
input: {
borderWidth: 1,
borderColor: '#ddd',
padding: 8,
borderRadius: 5,
marginTop: 5,
textAlign: 'center',
},
saveButton: {
backgroundColor: 'blue',
paddingHorizontal: 50,
paddingVertical: 15,
borderRadius: 50,
alignItems: 'center',
position: 'absolute',
alignSelf: 'center',
bottom: 25,
},
saveButtonText: {
color: 'white',
fontWeight: 'bold',
},
});
export default Edit;