mirror of
https://github.com/urosran/cally.git
synced 2026-10-23 07:17:54 +00:00
additional hooks
This commit is contained in:
+40
-213
@@ -1,223 +1,50 @@
|
||||
import React, {useEffect, useState} from "react";
|
||||
import {Button, TextInput} from "react-native";
|
||||
import {Checkbox, Picker, TextField, View, Text} 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 {UserProfile} from "@/hooks/firebase/types/profileTypes";
|
||||
import {uuidv4} from "@firebase/util";
|
||||
import {useGetChildrenByParentId} from "@/hooks/firebase/useGetChildrenByParentId";
|
||||
import {ProfileType, useAuthContext} from "@/contexts/AuthContext";
|
||||
import React, { useEffect, useState } from "react";
|
||||
import { Button, TextInput } from "react-native";
|
||||
import { Checkbox, Picker, TextField, View, Text } from "react-native-ui-lib";
|
||||
import { useCreateSubUser } from "@/hooks/firebase/useCreateSubUser";
|
||||
import { UserProfile } from "@/hooks/firebase/types/profileTypes";
|
||||
import { uuidv4 } from "@firebase/util";
|
||||
import { useGetChildrenByParentId } from "@/hooks/firebase/useGetChildrenByParentId";
|
||||
import { ProfileType, useAuthContext } from "@/contexts/AuthContext";
|
||||
import SignInPage from "../../../components/pages/main/SignInPage";
|
||||
import Entry from "@/components/pages/main/Entry";
|
||||
import { useSignUp } from "@/hooks/firebase/useSignUp";
|
||||
import { useSignOut } from "@/hooks/firebase/useSignOut";
|
||||
|
||||
const Screen: React.FC = () => {
|
||||
const {user, profileType, profileData} = useAuthContext()
|
||||
const { user, profileType, profileData } = useAuthContext();
|
||||
|
||||
const {
|
||||
email,
|
||||
setEmail,
|
||||
password,
|
||||
setPassword,
|
||||
handleLogin,
|
||||
handleSignOut,
|
||||
handleProfileTypeSelection,
|
||||
handleRegister,
|
||||
} = useAuth();
|
||||
const { data: children } = useGetChildrenByParentId();
|
||||
|
||||
const {
|
||||
child,
|
||||
setChild,
|
||||
} = useChildren(user);
|
||||
const { mutateAsync: createSubUser } = useCreateSubUser();
|
||||
const { mutateAsync: signOut } = useSignOut();
|
||||
|
||||
const {data: children} = useGetChildrenByParentId()
|
||||
const createNewSubUser = async (userProfile: UserProfile) => {
|
||||
await createSubUser({ ...userProfile, email: `${uuidv4()}@test.com` });
|
||||
// createSubUser({
|
||||
// email,
|
||||
// password,
|
||||
// userType: profileType!,
|
||||
// name: "",
|
||||
// contact: "+381628334",
|
||||
// ...child
|
||||
// })
|
||||
};
|
||||
|
||||
const {
|
||||
caregivers,
|
||||
caregiver,
|
||||
setCaregiver,
|
||||
fetchCaregivers,
|
||||
} = useCaregivers();
|
||||
|
||||
const {mutateAsync: createSubUser} = useCreateSubUser()
|
||||
|
||||
const createNewSubUser = async (userProfile: UserProfile) => {
|
||||
await createSubUser({...userProfile, email: `${uuidv4()}@test.com`})
|
||||
// createSubUser({
|
||||
// email,
|
||||
// password,
|
||||
// userType: profileType!,
|
||||
// name: "",
|
||||
// contact: "+381628334",
|
||||
// ...child
|
||||
// })
|
||||
await fetchCaregivers();
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
if (user) {
|
||||
fetchCaregivers();
|
||||
}
|
||||
}, [user]);
|
||||
|
||||
|
||||
const [isParent, setIsParent] = useState<boolean>(false);
|
||||
const [isChild, setIsChild] = useState<boolean>(false);
|
||||
const [isCaregiver, setIsCaregiver] = useState<boolean>(false);
|
||||
|
||||
useEffect(() => {
|
||||
if (isParent) handleProfileTypeSelection("parent");
|
||||
if (isChild) handleProfileTypeSelection("child");
|
||||
if (isCaregiver) handleProfileTypeSelection("caregiver");
|
||||
}, [isParent, isChild, isCaregiver]);
|
||||
|
||||
const renderLogin = () => (
|
||||
<View marginH-20>
|
||||
<TextInput placeholder="Email" value={email} onChangeText={setEmail}/>
|
||||
<TextInput
|
||||
placeholder="Password"
|
||||
value={password}
|
||||
onChangeText={setPassword}
|
||||
secureTextEntry
|
||||
/>
|
||||
<Button title="Login" onPress={handleLogin}/>
|
||||
<Button title="Register" onPress={handleRegister}/>
|
||||
<Text>Choose Profile Type:</Text>
|
||||
<Checkbox
|
||||
label="Parent"
|
||||
value={isParent}
|
||||
onValueChange={(value) => {
|
||||
setIsParent(value);
|
||||
if (value) {
|
||||
setIsChild(false);
|
||||
setIsCaregiver(false);
|
||||
}
|
||||
}}
|
||||
/>
|
||||
<Checkbox
|
||||
label="Child"
|
||||
value={isChild}
|
||||
onValueChange={(value) => {
|
||||
setIsChild(value);
|
||||
if (value) {
|
||||
setIsParent(false);
|
||||
setIsCaregiver(false);
|
||||
}
|
||||
}}
|
||||
/>
|
||||
<Checkbox
|
||||
label="Caregiver"
|
||||
value={isCaregiver}
|
||||
onValueChange={(value) => {
|
||||
setIsCaregiver(value);
|
||||
if (value) {
|
||||
setIsParent(false);
|
||||
setIsChild(false);
|
||||
}
|
||||
}}
|
||||
/>
|
||||
return (
|
||||
<View>
|
||||
{user ? (
|
||||
<View paddingH-20 marginT-20>
|
||||
{profileType === ProfileType.PARENT && <Text>Parent</Text>}
|
||||
{profileType === ProfileType.CHILD && <Text>Child</Text>}
|
||||
{profileType === ProfileType.CAREGIVER && <Text>Caregiver</Text>}
|
||||
<Button title="Sign Out" onPress={async() => {await signOut()}} />
|
||||
</View>
|
||||
);
|
||||
|
||||
console.log(profileType, profileData)
|
||||
|
||||
return (
|
||||
<View>
|
||||
{user ? (
|
||||
<View paddingH-20 marginT-20>
|
||||
{profileType === ProfileType.PARENT && <Text>Parent</Text>}
|
||||
{profileType === ProfileType.CHILD && <Text>Child</Text>}
|
||||
{profileType === ProfileType.CAREGIVER && <Text>Caregiver</Text>}
|
||||
<Button title="Sign Out" onPress={handleSignOut}/>
|
||||
<TextField
|
||||
placeholder={"Child Name"}
|
||||
floatingPlaceholder
|
||||
value={child.name}
|
||||
onChangeText={(value) =>
|
||||
setChild((prevChild) => ({
|
||||
...prevChild,
|
||||
name: value,
|
||||
}))
|
||||
}
|
||||
enableErrors
|
||||
validate={["required", (value: string) => value.length > 6]}
|
||||
validationMessage={[
|
||||
"Field is required",
|
||||
"Name is too short",
|
||||
]}
|
||||
showCharCounter
|
||||
maxLength={30}
|
||||
/>
|
||||
<Button
|
||||
title="Add Child"
|
||||
onPress={() => {
|
||||
createNewSubUser(child);
|
||||
}}
|
||||
/>
|
||||
<TextField
|
||||
placeholder={"Caregiver Name"}
|
||||
floatingPlaceholder
|
||||
value={caregiver.name}
|
||||
onChangeText={(value) =>
|
||||
setCaregiver((prevCaregiver) => ({
|
||||
...prevCaregiver,
|
||||
name: value,
|
||||
}))
|
||||
}
|
||||
enableErrors
|
||||
validate={["required", (value: string) => value.length > 6]}
|
||||
validationMessage={["Field is required"]}
|
||||
showCharCounter
|
||||
maxLength={30}
|
||||
/>
|
||||
<TextField
|
||||
placeholder={"Caregiver Contact"}
|
||||
floatingPlaceholder
|
||||
value={caregiver.contact}
|
||||
onChangeText={(value) =>
|
||||
setCaregiver((prevCaregiver) => ({
|
||||
...prevCaregiver,
|
||||
contact: value,
|
||||
}))
|
||||
}
|
||||
enableErrors
|
||||
validate={[
|
||||
"number",
|
||||
"required",
|
||||
(value: string) => value.length > 9,
|
||||
]}
|
||||
validationMessage={["Field is required"]}
|
||||
showCharCounter
|
||||
maxLength={30}
|
||||
/>
|
||||
<Button
|
||||
title="Add Caregiver"
|
||||
onPress={() => {
|
||||
createNewSubUser(caregiver);
|
||||
}}
|
||||
/>
|
||||
<View margin-20>
|
||||
<Text>Children:</Text>
|
||||
{children?.map((child) => (
|
||||
<View key={child.name} row>
|
||||
<Text>Name: {child.name} </Text>
|
||||
<Picker label="Pick Caregiver">
|
||||
{caregivers.map((item) => (
|
||||
<Picker.Item
|
||||
key={item.name}
|
||||
label={item.name}
|
||||
value={item.name}
|
||||
/>
|
||||
))}
|
||||
</Picker>
|
||||
</View>
|
||||
))}
|
||||
</View>
|
||||
</View>
|
||||
) : (
|
||||
renderLogin()
|
||||
)}
|
||||
</View>
|
||||
);
|
||||
) : (
|
||||
<Entry />
|
||||
)}
|
||||
</View>
|
||||
);
|
||||
};
|
||||
|
||||
export default Screen;
|
||||
|
||||
Reference in New Issue
Block a user