-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfireStore.jsx
1437 lines (1250 loc) · 37.9 KB
/
fireStore.jsx
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
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// firebase imports
import auth from '@react-native-firebase/auth';
import firestore from '@react-native-firebase/firestore';
import AsyncStorage from '@react-native-async-storage/async-storage';
// NetInfo
import NetInfo from '@react-native-community/netinfo';
import {useNetInfo} from '@react-native-community/netinfo';
import React from 'react';
//-------------------------------------------------------------------------//
// GENERAL FUNCTIONS
//-------------------------------------------------------------------------//
// fetch User Appointments
export async function fetchUserAppointments() {
// get current user data
const currentUser = auth().currentUser;
// count the number of Appointments the user has
const count = await firestore()
.collection('Appointments')
.where('userId', '==', currentUser.uid)
.get();
// return the number of sessions
return count.size;
}
export async function getUserData(setUserData, setError, user) {
const userDocRef = firestore().collection('Users').doc(user.uid);
try {
// get display name and photo url from auth using onAuthStateChanged() method
auth().onAuthStateChanged(user => {
if (user) {
const {displayName, photoURL} = user;
// get user data from firestore using onSnapshot() method
userDocRef.onSnapshot(documentSnapshot => {
// check if documentSnapshot is not null
if (documentSnapshot) {
// check if document exists
if (documentSnapshot.exists) {
// set userData state
setUserData({
...documentSnapshot.data(),
displayName,
photoURL,
});
} else {
// handle case where document does not exist by creating a new document with default values
userDocRef.set({
displayName,
photoURL,
// add any other default values here
});
}
}
});
}
});
} catch (error) {
// set error status and message
setError(error.message);
}
}
//-------------------------------------------------------------------------//
// MENTAL HEALTH TIP FUNCTIONS
//-------------------------------------------------------------------------//
// fetch Daily Mental Health Tips
export async function fetchDailyMentalHealthTips(setLoading) {
// set loading to true
setLoading(true);
// get current user data
const currentUser = auth().currentUser;
// fetch 10 daily mental health tip first
const mentalHealthTips = await firestore()
.collection('HealthTips')
.orderBy('createdAt', 'desc')
.limit(10)
.get();
// set loading to false
setLoading(false);
// return the mental health tips
return mentalHealthTips.docs.map(doc => {
return {
...doc.data(),
key: doc.id,
};
});
}
// fetch more Daily Mental Health Tips
export async function fetchMoreDailyMentalHealthTips(
setLoading2,
setHealthTips,
healthTips,
setError,
) {
try {
// set loading to true
setLoading2(true);
const lastVisible = healthTips[healthTips.length - 1].createdAt;
const List = [];
await firestore()
.collection('HealthTips')
.orderBy('createdAt', 'desc')
.startAfter(lastVisible)
.limit(10)
.get()
.then(querySnapshot => {
querySnapshot.forEach(doc => {
const {title, description, createdAt} = doc.data();
List.push({
key: doc.id,
title,
description,
createdAt,
});
});
});
// set health tips
setHealthTips([...healthTips, ...List]);
// set loading to false
setLoading2(false);
} catch (error) {
// set loading to false
setLoading2(false);
// set error
setError(error.message);
}
}
//-------------------------------------------------------------------------//
// NOTIFICATION FUNCTION
//-------------------------------------------------------------------------//
export async function fetchNotifications(setLoading) {
// set loading to true
setLoading(true);
// get current user data
const currentUser = auth().currentUser;
// list to hold notifications
const notifications = [];
// get notifications from firestore under the Users collection
firestore()
.collection('Users')
.doc(currentUser.uid)
.collection('Notifications')
.orderBy('createdAt', 'desc')
.onSnapshot(querySnapshot => {
// clear the notifications list
notifications.length = 0;
if (querySnapshot) {
// loop through the documents and add them to the list
querySnapshot.forEach(documentSnapshot => {
notifications.push({
...documentSnapshot.data(),
key: documentSnapshot.id,
});
});
}
// set loading to false
setLoading(false);
});
// return notifications
return notifications;
}
// function to update notification read status for a particular notification once user clicks on it
export async function updateNotificationReadStatus(notificationId) {
// get current user data
const currentUser = auth().currentUser;
// update notification read status
await firestore()
.collection('Users')
.doc(currentUser.uid)
.collection('Notifications')
.doc(notificationId)
.update({
read: true,
});
}
//-------------------------------------------------------------------------//
// THERAPIST FUNCTIONS
//-------------------------------------------------------------------------//
// fetch therapist list
export async function fetchTherapist(setLoading) {
// set loading to true
setLoading(true);
// list to hold therapist
const therapist = [];
// get therapist from firestore
await firestore()
.collection('Therapists')
.orderBy('createdAt', 'desc')
.limit(6)
.get()
.then(querySnapshot => {
querySnapshot.forEach(documentSnapshot => {
therapist.push({
...documentSnapshot.data(),
key: documentSnapshot.id,
});
});
});
// set loading to false
setLoading(false);
// return therapist
return therapist;
}
// fetch more therapist
export async function fetchMoreTherapist(
setLoading2,
therapist,
setTherapist,
setError,
) {
try {
// set loading to true
setLoading2(true);
const lastVisible = therapist[therapist.length - 1].createdAt;
const List = [];
await firestore()
.collection('Therapists')
.orderBy('createdAt', 'desc')
.startAfter(lastVisible)
.limit(4)
.get()
.then(querySnapshot => {
querySnapshot.forEach(documentSnapshot => {
List.push({
...documentSnapshot.data(),
key: documentSnapshot.id,
});
});
});
// set therapist
setTherapist([...therapist, ...List]);
// set loading to false
setLoading2(false);
} catch (error) {
// set loading to false
setLoading2(false);
// set error
setError(error.message);
}
}
// fetch therapist schedule for the week
export async function fetchTherapistSchedule(setLoading, item) {
// use try-catch to handle errors
try {
// set loading to true
setLoading(true);
// get therapist schedule from firestore
// use a variable to store the query
const query = firestore()
.collection('Therapists')
.doc(item.key)
.collection('Schedule')
.orderBy('createdAt', 'desc');
// use await instead of then to get the data
const querySnapshot = await query.get();
// use map instead of forEach to create a new array
const therapistSchedule = querySnapshot.docs.map(doc => ({
...doc.data(),
key: doc.id,
}));
// return therapist schedule
return therapistSchedule;
} catch (error) {
// handle error here
console.error(error);
} finally {
// set loading to false
setLoading(false);
}
}
// Confirm User Booking
export async function confirmUserBooking(
therapistId,
date,
time,
setLoading,
setErrorStatus,
setError,
userData,
token,
navigation,
name,
) {
// set loading to true
setLoading(true);
// get current user
const user = auth().currentUser;
try {
// save booking to firestore
const bookingRef = await firestore()
.collection('Appointments')
.add({
userId: user.uid,
therapistId: therapistId,
date: date,
time: time,
token: `${userData.phoneNumber}${token}`,
createdAt: firestore.FieldValue.serverTimestamp(),
});
// send notification to user
sendNotification(name, date, time);
// send notification to therapist
sendNotificationToTherapist(therapistId, name, date, time);
// Update therapist schedule in firestore
const scheduleRef = await firestore()
.collection('Therapists')
.doc(therapistId)
.collection('Schedule')
.add({
client: user.uid,
date: date,
time: time,
status: 'Booked',
createdAt: firestore.FieldValue.serverTimestamp(),
});
// set error status
setErrorStatus('success');
// set error message
setError('Appointment booked successfully');
// navigate to home screen after a delay
setTimeout(() => {
navigation.navigate('Therapy');
}, 900);
} catch (error) {
// set error status
setErrorStatus('error');
// set error message
setError(error.message);
} finally {
// set loading to false
setLoading(false);
}
}
// Send notification to user after booking a session
export async function sendNotification(name, date, time) {
// get current user
const user = auth().currentUser;
// get user data from firestore
await firestore()
.collection('Users')
.doc(user.uid)
.get()
.then(documentSnapshot => {
// check if document exists
if (documentSnapshot.exists) {
// get user data
const userData = documentSnapshot.data();
// send notification to user (create collection of notification under Users collection)
firestore()
.collection('Users')
.doc(user.uid)
.collection('Notifications')
.add({
userId: user.uid,
title: 'Appointment Booked',
body: `Your appointment with ${name} has been booked for ${date} at ${time}`,
createdAt: firestore.FieldValue.serverTimestamp(),
read: false,
});
}
});
}
// send notification to therapist after user booking a session
export async function sendNotificationToTherapist(
therapistId,
name,
date,
time,
) {
// get therapist data from firestore
await firestore()
.collection('Therapists')
.doc(therapistId)
.get()
.then(documentSnapshot => {
// check if document exists
if (documentSnapshot.exists) {
// get therapist data
const therapistData = documentSnapshot.data();
// send notification to therapist (create collection of notification under Therapists collection)
firestore()
.collection('Therapists')
.doc(therapistId)
.collection('Notifications')
.add({
therapistId: therapistId,
title: 'Appointment Booked',
body: `You have a new appointment with ${name} on ${date} at ${time}`,
createdAt: firestore.FieldValue.serverTimestamp(),
read: false,
});
}
});
}
// function to upload the therapist details to firestore
export async function uploadTherapistDetailsToFirestore(
setLoading,
setErrorStatus,
setError,
name,
Location,
title,
workplace,
about,
value,
dayValue,
appointmentValue,
languageValue,
setUploadStatus,
) {
// set loading to true while uploading therapist details
setLoading(true);
// get current logged in user id
const uid = auth().currentUser.uid;
try {
// upload therapist details to firestore
await firestore()
.collection('Therapists')
.doc(uid)
.set({
name: name,
Location: Location,
title: title,
workplace: workplace,
about: about,
value: value,
image: `https://source.unsplash.com/collection/139386/160x160/?sig=${Math.floor(
Math.random() * 1000,
)}`,
dayValue: [...dayValue],
appointmentValue: [...appointmentValue],
languageValue: [...languageValue],
createdAt: firestore.FieldValue.serverTimestamp(),
});
// set upload status to true
setUploadStatus(true);
// set loading to false after uploading therapist details
setLoading(false);
// update error status
setErrorStatus('success');
// set error message
setError('Therapist details uploaded successfully!');
} catch (error) {
// set loading to false after uploading therapist details
setLoading(false);
// update error status
setErrorStatus('error');
// set error message
setError(error.message);
}
}
// function to edit the therapist details in firestore
export async function editTherapistDetailsInFirestore(
setLoading,
setErrorStatus,
setError,
values,
status,
availability,
appointmentTime,
) {
// get current logged in user id
const uid = auth().currentUser.uid;
try {
// set loading to true while uploading therapist details
setLoading(true);
// check that input fields are not empty
status === '' || availability === '' || appointmentTime === ''
? // set error status and message if any field is empty
(setErrorStatus('error'), setError('Please fill in all fields!'))
: // upload therapist details to firestore if all fields are filled
await firestore()
.collection('Therapists')
.doc(uid)
.update({
about: values.about,
value: status,
dayValue: [...availability],
appointmentValue: [...appointmentTime],
createdAt: firestore.FieldValue.serverTimestamp(),
});
} catch (error) {
// set error status and message if upload fails
setErrorStatus('error');
setError(error.message);
} finally {
// set loading to false after uploading therapist details
setLoading(false);
// recall fetchTherapist function to get updated therapist details
fetchTherapist(setLoading);
}
}
// function to fetch selected therapist details from firestore
export async function fetchSelectedTherapist(item) {
try {
// fetch therapist details
const res = await firestore().collection('Therapists').doc(item.key).get();
// check if the document exists
if (res.exists) {
// return the data as an object
return res.data();
} else {
// throw an error if no document found
throw new Error('No therapist found with this id');
}
} catch (error) {
// set error message
setError(error.message);
}
}
// function to check if therapist details exists in firestore
export async function checkIfTherapistDetailsExists(setTherapistDetailsExists) {
// get current logged in user id
const uid = auth().currentUser.uid;
// check if therapist details exists in firestore
await firestore()
.collection('Therapists')
.doc(uid)
.get()
.then(documentSnapshot => {
// check if document exists
if (documentSnapshot.exists) {
// set therapist details exists to true
setTherapistDetailsExists(true);
} else {
// set therapist details exists to false
setTherapistDetailsExists(false);
}
});
}
// function to send a private message to therapist
export async function sendPrivateMessage(
setErrorStatus,
setError,
text,
therapistId,
therapistName,
therapistImage,
userName,
userImage,
userUid,
) {
// get current logged in user id
const uid = auth().currentUser.uid;
try {
// send message to firestore
const RefDoc = firestore()
.collection('Therapists')
.doc(therapistId)
.collection('PrivateMessages')
.doc(`${userUid}-${therapistId}`);
// check if document exists
const doc = await RefDoc.get();
// if document exists, update the document
if (doc.exists) {
// update document
await RefDoc.update({
messages: firestore.FieldValue.arrayUnion({
message: text,
createdAt: new Date().getTime(),
user: {
_id: uid,
name: userName,
avatar: userImage,
},
}),
lastMessage: text, // denormalize last message for easy access
lastMessageTime: new Date().getTime(), // denormalize last message time for sorting
});
} else {
// create document
await RefDoc.set({
messages: firestore.FieldValue.arrayUnion({
message: text,
createdAt: new Date().getTime(),
user: {
_id: uid,
name: userName,
avatar: userImage,
},
}),
lastMessage: text, // denormalize last message for easy access
lastMessageTime: new Date().getTime(), // denormalize last message time for sorting
therapistId: therapistId,
therapistName: therapistName,
therapistImage: therapistImage,
userId: userUid,
userName: userName,
userImage: userImage,
createdAt: new Date().getTime(),
});
}
// update user's conversations collection with therapist info
await firestore()
.collection('Users')
.doc(userUid)
.collection('Conversations')
.doc(therapistId)
.set({
therapistId: therapistId,
therapistName: therapistName,
therapistImage: therapistImage,
lastMessage: text, // denormalize last message for easy access
lastMessageTime: new Date().getTime(), // denormalize last message time for sorting
});
} catch (error) {
// set error status and message if upload fails
setErrorStatus('error');
setError(error.message);
console.log(error.message);
}
}
// funtion to fetch the list of private chats for therapist to view
export async function fetchPrivateChats(setPrivateChats, setLoading) {
// use try-catch to handle errors
try {
setLoading(true);
// get current logged in user id
const uid = auth().currentUser.uid;
// fetch list of private chats
// use a variable to store the query
const query = firestore()
.collection('Therapists')
.doc(uid)
.collection('PrivateMessages')
.orderBy('lastMessageTime', 'desc');
// use await instead of onSnapshot to get the data once
const querySnapshot = await query.get();
// use map instead of forEach to create a new array
const chats = querySnapshot.docs.map(doc => ({
...doc.data(),
key: doc.id,
}));
setPrivateChats(chats);
} catch (error) {
// handle error here
console.error(error);
} finally {
setLoading(false);
}
}
// function to fetch the list of private chats for user to view
export async function fetchUserPrivateChats(setPrivateChats, setLoading) {
// use try-catch to handle errors
try {
setLoading(true);
// get current logged in user id
const uid = auth().currentUser.uid;
// fetch list of private chats
// use a variable to store the query
const query = firestore()
.collection('Users')
.doc(uid)
.collection('Conversations')
.orderBy('lastMessageTime', 'desc');
// use await instead of onSnapshot to get the data once
const querySnapshot = await query.get();
// use map instead of forEach to create a new array
const chats = querySnapshot.docs.map(doc => ({
...doc.data(),
key: doc.id,
}));
setPrivateChats(chats);
} catch (error) {
// handle error here
console.error(error);
} finally {
setLoading(false);
}
}
//-------------------------------------------------------------------------//
// PROFILE SCREEN FUNCTIONS
//-------------------------------------------------------------------------//
// Edit user profile
export async function editUserProfile(
setLoading,
setErrorStatus,
setError,
values,
resetForm,
) {
try {
// set loading to true
setLoading(true);
// get current user and user doc reference
const user = auth().currentUser;
const userDocRef = firestore().collection('Users').doc(user.uid);
// update display name, email and phone number in user doc
await userDocRef.update({
userName: values.username,
email: values.email,
phoneNumber: values.phone,
});
// update display name and email in auth
await user.updateProfile({
displayName: values.username,
email: values.email,
});
// reset form and set loading to false
resetForm();
setLoading(false);
} catch (error) {
// set error status and message
setErrorStatus(true);
setError(error.message);
console.log(error);
}
}
// Change user password
export async function changeUserPassword(
setLoading,
setErrorStatus,
setError,
values,
resetForm,
) {
// set loading to true while updating user password
setLoading(true);
// get current logged in user id
const uid = auth().currentUser.uid;
// re-authenticate user
const credential = auth.EmailAuthProvider.credential(
auth().currentUser.email,
values.currentPassword,
);
try {
// re-authenticate user
await auth().currentUser.reauthenticateWithCredential(credential);
// update user password
await auth().currentUser.updatePassword(values.newPassword);
// set loading to false after updating user password
setLoading(false);
// reset form
resetForm();
// update error status
setErrorStatus('success');
// set error message
setError('Password updated!');
} catch (error) {
// set loading to false after updating user password
setLoading(false);
// update error status
setErrorStatus('error');
// set error message based on error code
if (error.code === 'auth/wrong-password') {
setError('Current password is incorrect!');
} else {
setError('Something went wrong!');
}
}
}
// Delete user account
export async function deleteUserAccount(
setLoading,
setErrorStatus,
setError,
toggleModal2,
setUserToken,
) {
// set loading to true while deleting user account
setLoading(true);
// get current logged in user id
const uid = auth().currentUser.uid;
// get reference to user document in firestore collection
const userDocRef = firestore().collection('Users').doc(uid);
try {
// delete user account from authentication
await auth().currentUser.delete();
// delete user document from firestore collection
await userDocRef.delete();
// if user is a therapist, delete therapist document from firestore collection
await firestore().collection('Therapists').doc(uid).delete();
// set loading to false after deleting user account
setLoading(false);
// close modal
toggleModal2();
// clear user token
setUserToken(null);
// update error status
setErrorStatus('success');
// set error message
setError('Account deleted!');
} catch (error) {
// set loading to false after deleting user account
setLoading(false);
// update error status
setErrorStatus('error');
// set error message using error code and message from firebase
setError(`Something went wrong! (${error.code}: ${error.message})`);
}
}
//-------------------------------------------------------------------------//
// DISCUSSION BOARD FUNCTIONS
//-------------------------------------------------------------------------//
// fetch discussion board and check if current user is a member of that group
export async function fetchDiscussionBoard(setLoading) {
// set loading to true
setLoading(true);
// list to hold discussion board
const discussionBoard = [];
// get discussion board from firestore
await firestore()
.collection('Groups')
.orderBy('createdAt', 'desc')
.limit(5)
.get()
.then(querySnapshot => {
querySnapshot.forEach(documentSnapshot => {
discussionBoard.push({
...documentSnapshot.data(),
key: documentSnapshot.id,
});
});
});
// set loading to false
setLoading(false);
// return discussion board
return discussionBoard;
}
// search discussion board
export async function searchDiscussionBoard(
setLoading,
setGroup,
setError,
text,
) {
try {
// set loading to true
setLoading(true);
// list to hold discussion board
const discussionBoard = [];
// get discussion board from firestore
await firestore()
.collection('Groups')
.where('name', '>=', text.toUpperCase())
.where('name', '<=', text.toUpperCase() + '\uf8ff')
.orderBy('name')
.get()
.then(querySnapshot => {
querySnapshot.forEach(documentSnapshot => {
discussionBoard.push({
...documentSnapshot.data(),
key: documentSnapshot.id,
});
});
});
// set discussion board
setGroup(discussionBoard);
// set loading to false
setLoading(false);
} catch (error) {
// set loading to false
setLoading(false);
// set error
setError(error.message);
}
}
// create a new discussion board and send notification to user that created it
export async function createDiscussionBoard(
setLoading,
setLoading2,
setErrorStatus,
setError,
toggleModal2,
setGroup,
createGroup,
) {
try {
// set loading to true
setLoading2(true);
// get current user data
const currentUser = auth().currentUser;
// check createGroup is not empty
if (createGroup === '') {
// set loading to false
setLoading2(false);
// set error
setError('Please enter a group name');
} else {
// check if group name already exist
const groupRef = firestore()
.collection('Groups')
.where(
'name',
'==',
createGroup.charAt(0).toUpperCase() + createGroup.slice(1),
);