From 0b6199989b71c4e139f678cc763d5c498b086378 Mon Sep 17 00:00:00 2001 From: ivic00 <102467664+ivic00@users.noreply.github.com> Date: Fri, 9 Aug 2024 19:38:46 +0200 Subject: [PATCH] added profile hooks --- app/(auth)/main/index.tsx | 318 +++++++-------------------- hooks/firebase/index.ts | 1 + hooks/firebase/types/profileTypes.ts | 26 +++ hooks/firebase/useAuth.ts | 152 +++++++++++++ hooks/firebase/useCaregivers.ts | 59 +++++ hooks/firebase/useChildren.ts | 67 ++++++ 6 files changed, 390 insertions(+), 233 deletions(-) create mode 100644 hooks/firebase/types/profileTypes.ts create mode 100644 hooks/firebase/useAuth.ts create mode 100644 hooks/firebase/useCaregivers.ts create mode 100644 hooks/firebase/useChildren.ts diff --git a/app/(auth)/main/index.tsx b/app/(auth)/main/index.tsx index bb90537..9fae5f7 100644 --- a/app/(auth)/main/index.tsx +++ b/app/(auth)/main/index.tsx @@ -1,211 +1,58 @@ import React, { useEffect, useState } from "react"; import { Text, Button, TextInput } from "react-native"; -import firestore from "@react-native-firebase/firestore"; -import auth from "@react-native-firebase/auth"; -import {} from "@react-native-firebase/auth/"; -import { View, TextField, ListItem, Picker } from "react-native-ui-lib"; - -type ProfileType = "Parent" | "Child" | "Caregiver" | null; - -interface User { - uid: string; - email: string | null; -} - -interface UserProfile { - userType: "parent" | "child" | "caregiver"; - name: string; -} - -interface ParentProfile extends UserProfile { - userType: "parent"; -} - -interface ChildProfile extends UserProfile { - userType: "child"; - birthday: Date; - parentId: string; -} - -interface CaregiverProfile extends UserProfile { - userType: "caregiver"; - //assignedChildrenId: string[]; - contact: string; -} +import { View, TextField, Picker, Checkbox } from "react-native-ui-lib"; +import useAuth from "@/hooks/firebase/useAuth"; +import useChildren from "@/hooks/firebase/useChildren"; +import useCaregivers from "@/hooks/firebase/useCaregivers"; const Screen: React.FC = () => { - const [user, setUser] = useState(null); - const [profileType, setProfileType] = useState(null); - const [email, setEmail] = useState(""); - const [password, setPassword] = useState(""); - const [child, setChild] = useState({ - name: "", - birthday: new Date(), - userType: "child" as const, - parentId: "", - }); - const [caregiver, setCaregiver] = useState({ - name: "", - contact: "", - userType: "caregiver", - }); + const { + user, + profileType, + email, + setEmail, + password, + setPassword, + handleLogin, + handleSignOut, + handleProfileTypeSelection, + handleRegister, + } = useAuth(); - const [children, setChildren] = useState([]); - const [caregivers, setCaregivers] = useState([]); + const { + children, + child, + setChild, + fetchChildren, + handleNewChild, + } = useChildren(user); + + const { + caregivers, + caregiver, + setCaregiver, + fetchCaregivers, + handleNewCaregiver, + } = useCaregivers(); useEffect(() => { - const isDevelopment = __DEV__; - - if (isDevelopment) { - firestore().useEmulator("localhost", 8080); - auth().useEmulator("http://127.0.0.1:9099"); - } - - const unsubscribe = auth().onAuthStateChanged(async (firebaseUser) => { - if (firebaseUser) { - const userObj: User = { - uid: firebaseUser.uid, - email: firebaseUser.email, - }; - setUser(userObj); - - try { - const documentSnapshot = await firestore() - .collection("Users") - .doc(firebaseUser.uid) - .get(); - if (documentSnapshot.exists) { - setProfileType(documentSnapshot.data()?.profileType || null); - } - } catch (error) { - console.error("Error fetching user profile type:", error); - setProfileType(null); - } - } else { - setUser(null); - setProfileType(null); - } - }); - - return unsubscribe; - }, []); - - const fetchChildren = async () => { if (user) { - const childrenProfiles = await getChildrenByParentId(user.uid); - setChildren(childrenProfiles); + fetchChildren(); + fetchCaregivers(); } - }; - const fetchCaregivers = async () => { - const caregiverProfiles = await getCaregivers(); - setCaregivers(caregiverProfiles); - }; - - useEffect(() => { - fetchChildren(); - fetchCaregivers(); - }, []); - useEffect(() => { - fetchChildren(); - fetchCaregivers(); }, [user]); - const getCaregivers = async ( - ): Promise => { - try { - const snapshot = await firestore() - .collection("Profiles") - .where("userType", "==", "caregiver") - .get(); - - const caregivers: CaregiverProfile[] = snapshot.docs.map((doc) => { - const data = doc.data(); - return { - ...data, - } as CaregiverProfile; - }); - - return caregivers; - } catch (error) { - console.error("Error retrieving caregivers:", error); - return []; - } - }; - - const getChildrenByParentId = async ( - parentId: string - ): Promise => { - try { - const snapshot = await firestore() - .collection("Profiles") - .where("userType", "==", "child") - .where("parentId", "==", parentId) - .get(); - - const children: ChildProfile[] = snapshot.docs.map((doc) => { - const data = doc.data(); - return { - ...data, - birthday: data.birthday.toDate(), - } as ChildProfile; - }); - - return children; - } catch (error) { - console.error("Error retrieving child users:", error); - return []; - } - }; - - const handleLogin = async () => { - try { - await auth().signInWithEmailAndPassword(email, password); - console.log("User signed in!"); - } catch (error) { - console.error("Error during sign in:", error); - } - }; - - const handleSignOut = async () => { - try { - await auth().signOut(); - } catch (error) { - console.error("error during Sign out: ", error); - } - }; - - const handleProfileTypeSelection = (type: ProfileType) => { - if (user) { - setProfileType(type); - - firestore() - .collection("Users") - .doc(user.uid) - .set({ profileType: type }, { merge: true }); - } - }; - - const handleNewChild = async (newChild: ChildProfile) => { - try { - if (user) newChild.parentId = user.uid; - await firestore().collection("Profiles").add(newChild); - } catch (error) { - console.error(error); - } finally { - setChild((prev) => ({ ...prev, name: "" })); - } - }; - - const handleNewCaregiver = async (newCaregiver: CaregiverProfile) => { - try { - await firestore().collection("Profiles").add(newCaregiver); - } catch (error) { - console.error(error); - } finally { - setCaregiver((prev) => ({ ...prev, name: "", contact: "" })); - } - }; - + + const [isParent, setIsParent] = useState(false); + const [isChild, setIsChild] = useState(false); + const [isCaregiver, setIsCaregiver] = useState(false); + + useEffect(() => { + if (isParent) handleProfileTypeSelection("parent"); + if (isChild) handleProfileTypeSelection("child"); + if (isCaregiver) handleProfileTypeSelection("caregiver"); + }, [isParent, isChild, isCaregiver]); + const renderLogin = () => ( @@ -216,48 +63,52 @@ const Screen: React.FC = () => { secureTextEntry />