-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathForm.js
More file actions
172 lines (153 loc) · 4.04 KB
/
Copy pathForm.js
File metadata and controls
172 lines (153 loc) · 4.04 KB
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
import React from 'react';
import { View, TouchableOpacity, Text, StyleSheet } from 'react-native';
import { useForm } from 'react-hook-form';
import Toast from 'react-native-toast-message';
import Input from '@components/FormInputs/Input';
import { useLanguage } from '@context/LanguageContext';
import { useAuth } from '@context/AuthContext';
import { useNetwork } from '@context/NetworkContext';
import { addCategory, updateCategory } from './api';
export default function FunctionCategoryForm({ navigation, route }) {
const { translations } = useLanguage();
const { user } = useAuth();
const { isOnline } = useNetwork();
const editingCategory = route?.params?.category;
const {
control,
handleSubmit,
formState: { isSubmitting },
} = useForm({
defaultValues: {
name: editingCategory?.name || '',
tamilName: editingCategory?.tamilName || '',
description: editingCategory?.description || '',
},
});
const onSubmit = async data => {
if (!isOnline) {
Toast.show({
type: 'info',
text1: 'Add, Edit and Delete are disabled while offline.',
});
return;
}
try {
const categoryId = editingCategory?.id;
if (editingCategory) {
await updateCategory(categoryId, data, user?.id);
} else {
await addCategory(data, user?.id);
}
navigation.goBack();
Toast.show({
type: 'success',
text1: editingCategory ? 'Category updated' : 'Category added',
});
} catch (e) {
console.log('e ====== : ', e);
Toast.show({
type: 'error',
text1: 'Something went wrong',
});
}
};
return (
<View style={styles.container}>
{!isOnline && (
<View style={styles.offlineWarning}>
<Text style={styles.offlineWarningText}>
📡 Offline Mode: Add, Edit and Delete are disabled
</Text>
</View>
)}
<Input
name="name"
label={translations.name}
control={control}
required
rules={{ required: 'Name is required' }}
placeholder="Enter category name"
editable={isOnline}
/>
<Input
name="tamilName"
label={translations.tamilName}
control={control}
required
rules={{ required: 'Tamil name is required' }}
placeholder="தமிழ் பெயர்"
editable={isOnline}
/>
<Input
name="description"
label={translations.description}
control={control}
placeholder="Description"
multiline
editable={isOnline}
/>
<View style={styles.actions}>
<TouchableOpacity
style={[styles.button, styles.cancel]}
onPress={() => navigation.pop()}
disabled={isSubmitting}
>
<Text style={styles.btnText}>Cancel</Text>
</TouchableOpacity>
<TouchableOpacity
style={[styles.button, styles.save, !isOnline && styles.buttonDisabled]}
onPress={handleSubmit(onSubmit)}
disabled={isSubmitting || !isOnline}
>
<Text style={styles.btnText}>
{isSubmitting ? 'Saving...' : translations.save}
</Text>
</TouchableOpacity>
</View>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#F6F8FA',
padding: 16,
},
offlineWarning: {
backgroundColor: '#FF9800',
paddingHorizontal: 12,
paddingVertical: 10,
borderRadius: 8,
marginBottom: 16,
},
offlineWarningText: {
color: '#fff',
fontSize: 13,
fontWeight: '600',
textAlign: 'center',
},
actions: {
flexDirection: 'row',
justifyContent: 'flex-end',
marginTop: 24,
},
button: {
paddingVertical: 10,
paddingHorizontal: 20,
borderRadius: 8,
marginLeft: 12,
},
buttonDisabled: {
opacity: 0.5,
},
cancel: {
backgroundColor: '#9E9E9E',
},
save: {
backgroundColor: '#1976D2',
},
btnText: {
color: '#FFFFFF',
fontWeight: '600',
},
});