added profile hooks

This commit is contained in:
ivic00
2024-08-09 19:38:46 +02:00
parent f6bbc94285
commit 0b6199989b
6 changed files with 390 additions and 233 deletions
+1
View File
@@ -0,0 +1 @@
+26
View File
@@ -0,0 +1,26 @@
export interface User {
uid: string;
email: string | null;
}
export interface UserProfile {
userType: "parent" | "child" | "caregiver";
name: string;
}
export interface ParentProfile extends UserProfile {
userType: "parent";
childrenIds: string[];
}
export interface ChildProfile extends UserProfile {
userType: "child";
birthday: Date;
parentId: string;
}
export interface CaregiverProfile extends UserProfile {
userType: "caregiver";
//assignedChildrenId: string[];
contact: string;
}
+152
View File
@@ -0,0 +1,152 @@
import { useState, useEffect } from "react";
import auth from "@react-native-firebase/auth";
import firestore from "@react-native-firebase/firestore";
import {
User,
CaregiverProfile,
ChildProfile,
ParentProfile,
} from "./types/profileTypes";
type ProfileType = "parent" | "child" | "caregiver" | null;
const useAuth = () => {
const [user, setUser] = useState<User | null>(null);
const [profileType, setProfileType] = useState<ProfileType>(null);
const [email, setEmail] = useState<string>("");
const [password, setPassword] = useState<string>("");
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 handleRegister = async () => {
try {
await auth().createUserWithEmailAndPassword(email, password);
console.log("user registered!");
await auth().signInWithEmailAndPassword(email, password);
console.log("user signed in!");
let profileData: ParentProfile | ChildProfile | CaregiverProfile;
switch (profileType) {
case "parent":
profileData = {
userType: "parent",
name: "",
childrenIds: [],
} as ParentProfile;
break;
case "child":
profileData = {
userType: "child",
name: "",
birthday: new Date(),
parentId: "X",
} as ChildProfile;
break;
case "caregiver":
profileData = {
userType: "caregiver",
name: "Nanny Nannersen",
contact: "5523123",
} as CaregiverProfile;
break;
default:
throw new Error("Invalid profile type");
}
const currentUser = auth().currentUser;
if (currentUser) {
await firestore()
.collection("Profiles")
.doc(currentUser.uid)
.set(profileData);
console.log("Profile document added!");
}
} catch (error) {
console.error("Error during registration: ", error);
}
};
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();
console.log("User signed out!");
} catch (error) {
console.error("Error during sign out:", error);
}
};
const handleProfileTypeSelection = async (type: ProfileType) => {
if (user) {
setProfileType(type);
try {
await firestore()
.collection("Users")
.doc(user.uid)
.set({ profileType: type }, { merge: true });
} catch (error) {
console.error("Error saving profile type:", error);
}
}
};
return {
user,
profileType,
email,
setEmail,
password,
setPassword,
handleLogin,
handleSignOut,
handleProfileTypeSelection,
handleRegister,
};
};
export default useAuth;
+59
View File
@@ -0,0 +1,59 @@
import { useState } from "react";
import firestore from "@react-native-firebase/firestore";
import { CaregiverProfile } from "./types/profileTypes";
const useCaregivers = () => {
const [caregivers, setCaregivers] = useState<CaregiverProfile[]>([]);
const [caregiver, setCaregiver] = useState<CaregiverProfile>({
name: "",
contact: "",
userType: "caregiver",
});
const fetchCaregivers = async () => {
const caregiverProfiles = await getCaregivers();
setCaregivers(caregiverProfiles);
};
const getCaregivers = async (): Promise<CaregiverProfile[]> => {
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 handleNewCaregiver = async (newCaregiver: CaregiverProfile) => {
try {
await firestore().collection("Profiles").add(newCaregiver);
} catch (error) {
console.error(error);
} finally {
setCaregiver((prev) => ({ ...prev, name: "", contact: "" }));
fetchCaregivers();
}
};
return {
caregivers,
caregiver,
setCaregiver,
fetchCaregivers,
handleNewCaregiver,
};
};
export default useCaregivers;
+67
View File
@@ -0,0 +1,67 @@
import { useState } from "react";
import firestore from "@react-native-firebase/firestore";
import { ChildProfile } from "./types/profileTypes";
const useChildren = (user: any) => {
const [children, setChildren] = useState<ChildProfile[]>([]);
const [child, setChild] = useState<ChildProfile>({
name: "",
birthday: new Date(),
userType: "child",
parentId: "",
});
const fetchChildren = async () => {
if (user) {
const childrenProfiles = await getChildrenByParentId(user.uid);
setChildren(childrenProfiles);
}
};
const getChildrenByParentId = async (
parentId: string
): Promise<ChildProfile[]> => {
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 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: "" }));
fetchChildren();
}
};
return {
children,
child,
setChild,
fetchChildren,
handleNewChild,
};
};
export default useChildren;