From 6b42476ef07a81b3eab4798d97fe5f364512ddc8 Mon Sep 17 00:00:00 2001 From: Milan Paunovic Date: Wed, 21 Aug 2024 20:25:15 +0200 Subject: [PATCH] Auth logic rework --- app/(auth)/main/index.tsx | 385 ++++++++++----------- contexts/AuthContext.tsx | 6 +- hooks/firebase/useAuth.ts | 10 +- hooks/firebase/useCreateSubUser.ts | 15 +- hooks/firebase/useGetChildrenByParentId.ts | 34 ++ hooks/firebase/useSignUp.ts | 4 + 6 files changed, 254 insertions(+), 200 deletions(-) create mode 100644 hooks/firebase/useGetChildrenByParentId.ts diff --git a/app/(auth)/main/index.tsx b/app/(auth)/main/index.tsx index 15e53c6..69bd22a 100644 --- a/app/(auth)/main/index.tsx +++ b/app/(auth)/main/index.tsx @@ -1,44 +1,45 @@ -import React, { useEffect, useState } from "react"; -import { Text, Button, TextInput } from "react-native"; -import { View, TextField, Picker, Checkbox } from "react-native-ui-lib"; +import React, {useEffect, useState} from "react"; +import {Button, Text, TextInput} from "react-native"; +import {Checkbox, Picker, TextField, View} from "react-native-ui-lib"; import useAuth from "@/hooks/firebase/useAuth"; import useChildren from "@/hooks/firebase/useChildren"; import useCaregivers from "@/hooks/firebase/useCaregivers"; import {useCreateSubUser} from "@/hooks/firebase/useCreateSubUser"; -import {email} from "@sideway/address"; import {UserProfile} from "@/hooks/firebase/types/profileTypes"; import {uuidv4} from "@firebase/util"; +import {useGetChildrenByParentId} from "@/hooks/firebase/useGetChildrenByParentId"; +import {ProfileType, useAuthContext} from "@/contexts/AuthContext"; const Screen: React.FC = () => { - const { - user, - profileType, - email, - setEmail, - password, - setPassword, - handleLogin, - handleSignOut, - handleProfileTypeSelection, - handleRegister, + const {user, profileType, profileData} = useAuthContext() - } = useAuth(); + const { + email, + setEmail, + password, + setPassword, + handleLogin, + handleSignOut, + handleProfileTypeSelection, + handleRegister, + } = useAuth(); - const { - children, - child, - setChild, - fetchChildren, - handleNewChild, - } = useChildren(user); + const { + children, + child, + setChild, + handleNewChild, + } = useChildren(user); - const { - caregivers, - caregiver, - setCaregiver, - fetchCaregivers, - handleNewCaregiver, - } = useCaregivers(); + const {data: childrenByParentId} = useGetChildrenByParentId() + + const { + caregivers, + caregiver, + setCaregiver, + fetchCaregivers, + handleNewCaregiver, + } = useCaregivers(); const {mutateAsync: createSubUser} = useCreateSubUser() @@ -52,174 +53,172 @@ const Screen: React.FC = () => { // contact: "+381628334", // ...child // }) - await fetchChildren(); await fetchCaregivers(); - } - - useEffect(() => { - if (user) { - fetchChildren(); - fetchCaregivers(); } - }, [user]); - - 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 = () => ( - - - -