-
Notifications
You must be signed in to change notification settings - Fork 0
/
EditCategories.js
486 lines (438 loc) · 14.8 KB
/
EditCategories.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
import React, { useState, useEffect, useRef } from "react";
import {
Alert,
SectionList,
Dimensions,
Pressable,
View,
Text,
SafeAreaView,
Platform,
StatusBar,
TextInput,
TouchableOpacity,
Image,
Button,
ScrollView,
Modal,
TouchableWithoutFeedback,
} from "react-native";
import styles from "./styles";
import { SelectList } from "react-native-dropdown-select-list";
import { useNavigation } from "@react-navigation/core";
import {
doc,
getDoc,
updateDoc,
setDoc,
collection,
query,
getDocs,
deleteDoc,
} from "firebase/firestore";
import { auth, firestore } from "./firebase";
import NetInfo from "@react-native-community/netinfo";
import { deviceWidth, deviceHeight } from "./styles";
StatusBar.setTranslucent(true);
StatusBar.setBackgroundColor("white");
const EditCategories = ({ route, navigation }) => {
const { fetchData2, itemName, color } = route.params;
const [modalVisible, setModalVisible] = useState(false);
const [SubcategoryName, setSubcategoryName] = useState("");
const [selected, setSelected] = useState("");
const [lastClicked, setLastClicked] = useState("");
const [color2, setColor2] = useState("");
const uid = auth.currentUser.uid;
const makeSureDelete = () => {
Alert.alert(
"Delete Category",
"Are you sure you want to delete this category? This action cannot be undone. All flashcards in this category subcategories will be deleted.",
[
{
text: "No",
onPress: () => console.log("Cancel Pressed"),
style: "cancel",
},
{
text: "Yes",
onPress: () => deleteCategory(),
},
],
{ cancelable: false }
);
};
const backHome = () => {
navigation.navigate("MainScreen", { fetchData2 });
fetchData2();
};
useEffect(() => {
if (!fetchData2 || !itemName) {
navigation.navigate("MainScreen");
alert("Please select a category to edit");
} else {
console.log("fetchData2", fetchData2);
console.log("itemName", itemName);
setSelected(itemName);
setLastClicked(itemName);
setColor2(color);
}
fetchData();
const unsubscribe = NetInfo.addEventListener((state) => {
if (!state.isConnected) {
// User has lost internet connection
// Log out the user and redirect to login screen
// (You may also want to save the current state before logging out)
auth
.signOut()
.then(() => {
navigation.navigate("LoginScreen");
})
.catch((error) => {
console.error("Logout error", error);
alert(
"Your internet connection has been lost. Please log in again."
);
});
}
});
return () => {
unsubscribe();
};
}, []);
const [userData, setUserData] = useState([]);
const fetchData = async () => {
try {
// Use 'itemName' to get the specific category document reference
const categoryDocRef = doc(firestore, `users/${uid}/data/${itemName}`);
// Reference to the subcategory subcollection
const subcategoryCollectionRef = collection(
categoryDocRef,
"subcategory"
);
const subcategoryQuerySnapshot = await getDocs(subcategoryCollectionRef);
// Map subcategories to sections
const subcategories = subcategoryQuerySnapshot.docs.map((doc) => {
const subcategoryData = doc.data();
return {
name: subcategoryData.subcategoryName, // Assuming the field is named 'subcategoryName'
};
});
const sections = [
{
title: itemName,
color: color2, // Use the color passed from the previous screen or state
data: subcategories,
},
];
setUserData(sections); // Set the fetched data to state
} catch (error) {
console.error("Error getting subcategory data:", error);
}
};
const statusBarHeight =
Platform.OS === "android" ? StatusBar.currentHeight : 0;
const handleEditSubcategoryNavigation = async (itemName, lastClicked) => {
try {
// Update the lastClickedSubcategory field in the user's document
// If the update is successful, navigate to the "Session" screen
navigation.navigate("EditSubcategories", {
fetchData2: fetchData2,
itemName: itemName,
categoryName: lastClicked,
color: color2,
});
} catch (error) {
console.error("Error updating document:", error);
// Handle error or show an error message to the user
}
};
const alphanumericAndEmojiRegex = /^[A-Za-z\s\p{Emoji}]+$/u;
// console.log(selected + "selected");
// console.log(lastClicked);
const saveChanges = async () => {
if (selected === "") {
alert("Please enter a category name");
return;
}
if (selected === lastClicked) {
alert("No changes made");
return;
}
try {
const dataCollectionRef = collection(firestore, `users/${uid}/data`);
const categoryQuerySnapshot = await getDocs(dataCollectionRef);
// Check for name validity
if (!alphanumericAndEmojiRegex.test(selected)) {
alert(
"Invalid category name. Only alphanumeric characters and emojis are allowed."
);
return;
}
// Check if the new category name is already taken
const isNameTaken = categoryQuerySnapshot.docs.some((doc) => {
const categoryData = doc.data();
return categoryData.title === selected && doc.id !== lastClicked;
});
if (isNameTaken) {
alert(
"A category with this name already exists. Please choose a different name."
);
return;
}
const oldCategoryRef = doc(firestore, `users/${uid}/data`, lastClicked);
const newCategoryRef = doc(firestore, `users/${uid}/data`, selected);
// Copying the old category document to the new category document
const oldCategorySnap = await getDoc(oldCategoryRef);
if (oldCategorySnap.exists()) {
const oldCategoryData = oldCategorySnap.data();
oldCategoryData.title = selected; // Update the title field
await setDoc(newCategoryRef, oldCategoryData); // Set the updated data in the new document
} else {
console.log("Original category document not found!");
return;
}
// Reference to the subcategories in the old and new category documents
const oldSubcategoriesRef = collection(oldCategoryRef, "subcategory");
const newSubcategoriesRef = collection(newCategoryRef, "subcategory");
// Get and copy each subcategory document
const subcategoriesSnapshot = await getDocs(oldSubcategoriesRef);
for (const subcategoryDoc of subcategoriesSnapshot.docs) {
const newSubcategoryRef = doc(newSubcategoriesRef, subcategoryDoc.id);
await setDoc(newSubcategoryRef, subcategoryDoc.data());
}
// Delete the original category document along with its subcollections
// Note: This operation is complex and should be done with caution
// Delete the subcategories first
for (const subcategoryDoc of subcategoriesSnapshot.docs) {
const subcategoryRef = doc(oldSubcategoriesRef, subcategoryDoc.id);
await deleteDoc(subcategoryRef);
}
// Finally, delete the category document itself
await deleteDoc(oldCategoryRef);
console.log(
"Category and subcategories copied and original deleted successfully"
);
fetchData2();
navigation.navigate("MainScreen", { dataUpdated: true });
} catch (error) {
console.error("Error in saveChanges:", error);
}
};
const visibleModal = () => {
setModalVisible(true);
};
//create subcategory inside this category
const createSubcategory = async () => {
try {
// Reference to the category document
const categoryDocRef = doc(firestore, `users/${uid}/data`, lastClicked);
// Reference to the subcategory subcollection
const subcategoryCollectionRef = collection(
categoryDocRef,
"subcategory"
);
// Check if subcategory already exists
const subcategoryQuerySnapshot = await getDocs(subcategoryCollectionRef);
const subcategoryExists = subcategoryQuerySnapshot.docs.some(
(doc) => doc.data().subcategoryName === SubcategoryName
);
if (!subcategoryExists) {
// New subcategory document reference with SubcategoryName as the ID
const newSubcategoryDocRef = doc(
subcategoryCollectionRef,
SubcategoryName
);
// New subcategory object
const newSubcategoryData = {
subcategoryName: SubcategoryName,
flashcards: [], // Initially empty array for flashcards
};
// Add the new subcategory document
await setDoc(newSubcategoryDocRef, newSubcategoryData);
console.log("Subcategory created successfully");
fetchData(); // Update the local data after creation
//go back to mainscreen
fetchData2();
navigation.navigate("MainScreen", { fetchData2 });
} else {
console.log("Subcategory already exists");
alert("Subcategory already exists");
}
} catch (error) {
console.error("Error creating subcategory:", error);
} finally {
setModalVisible(false); // Hide the modal in any case
setSubcategoryName(""); // Clear the input field
}
};
const breakpoint = 768;
const fontStyle = deviceWidth > breakpoint ? 25 : 18;
const fontStyle2 = deviceWidth > breakpoint ? 28 : 20;
return (
<SafeAreaView style={[styles.container, { marginTop: statusBarHeight }]}>
<View style={styles.loginContainer2}>
<TouchableOpacity onPress={backHome} style={styles.backToMain}>
<Text
style={{
textAlign: "left",
fontSize: 20,
padding: 10,
paddingTop: 5,
}}
>
<Image
source={require("./assets/arrow_left.png")}
style={styles.iconStyle3}
/>
Back to MainScreen
</Text>
</TouchableOpacity>
<TextInput
style={{
marginTop: 50,
borderBottomColor: userData.length > 0 ? color2 : "lightblue",
borderWidth: 5,
borderTopColor: "#fefefa",
borderLeftColor: "#fefefa",
borderRightColor: "#fefefa",
fontSize: deviceHeight * 0.029,
fontWeight: "bold",
borderRadius: 10,
width: 250,
height: 50,
textAlign: "center",
alignItems: "center",
justifyContent: "center",
alignSelf: "center",
}}
value={selected}
onChangeText={(text) => setSelected(text)}
// Add other props as needed, like placeholder, style, etc.
/>
<TouchableOpacity style={{ marginTop: 50 }}>
<Text style={{ fontSize: fontStyle2, fontWeight: "400" }}>
Subcategories
</Text>
</TouchableOpacity>
<SectionList
style={{ marginTop: 10 }}
sections={userData}
renderItem={({ item }) => (
<TouchableOpacity
onPress={() =>
handleEditSubcategoryNavigation(item.name, lastClicked)
}
style={{
borderBottomColor: userData.length > 0 ? color2 : "lightblue",
borderWidth: 3,
borderTopColor: "#fefefa",
borderLeftColor: "#fefefa",
borderRightColor: "#fefefa",
}}
>
<Text
style={{
marginTop: 15,
textAlign: "center",
fontSize: fontStyle,
padding: 3,
}}
>
{item.name}
</Text>
</TouchableOpacity>
)}
keyExtractor={(item, index) => `basicListEntry-${index}-${item.name}`}
/>
<TouchableOpacity
onPress={visibleModal}
style={{
marginTop: 0,
borderColor: "darkblue",
borderWidth: 1,
backgroundColor: "white",
borderRadius: 10,
width: 220,
height: 50,
textAlign: "center",
alignItems: "center",
justifyContent: "center",
alignSelf: "center",
}}
>
<Text>Add a subcategory</Text>
</TouchableOpacity>
<TouchableOpacity
onPress={makeSureDelete}
style={{
marginTop: 50,
marginBottom: 70,
borderColor: "red",
borderWidth: 1,
backgroundColor: "white",
borderRadius: 10,
width: 220,
height: 50,
textAlign: "center",
alignItems: "center",
justifyContent: "center",
alignSelf: "center",
}}
>
<Text>Delete this category</Text>
</TouchableOpacity>
<Modal
animationType="fade"
transparent={true}
visible={modalVisible}
onRequestClose={() => setModalVisible(false)}
>
<TouchableWithoutFeedback>
<View style={styles.modalOverlay}>
<View style={[styles.modalContent, { height: 250 }]}>
<TextInput
placeholder="Enter Subcategory Name"
style={styles.inputCategory}
value={SubcategoryName}
onChangeText={(text) => setSubcategoryName(text)}
/>
<TouchableOpacity
style={[styles.buttonLogin, { marginTop: 10 }]}
onPress={createSubcategory}
>
<Text style={styles.startButtonText}>Create Subcategory</Text>
</TouchableOpacity>
<TouchableOpacity
style={[
styles.buttonRegister,
{ marginTop: 20, width: 100, height: 50 },
]}
//close modal
onPress={() => setModalVisible(false)}
>
<Text
style={[
styles.clearButtonText,
{ fontSize: 15, fontWeight: 400 },
]}
>
Close
</Text>
</TouchableOpacity>
</View>
</View>
</TouchableWithoutFeedback>
</Modal>
</View>
<TouchableOpacity
onPress={saveChanges}
style={{ backgroundColor: "lightyellow", padding: 20 }}
>
<Text style={[styles.clearButtonText, { textAlign: "center" }]}>
Save Category Changes
</Text>
</TouchableOpacity>
</SafeAreaView>
);
};
export default EditCategories;