additional hooks

This commit is contained in:
ivic00
2024-08-22 22:02:43 +02:00
parent 55a3091023
commit 0a862e7a8c
14 changed files with 3376 additions and 2999 deletions
-136
View File
@@ -1,136 +0,0 @@
import {useState} from "react";
import {useMutation} from "react-query";
import auth from "@react-native-firebase/auth";
import firestore from "@react-native-firebase/firestore";
import {User,} from "./types/profileTypes";
import {useSignUp} from "@/hooks/firebase/useSignUp";
type ProfileType = "parent" | "child" | "caregiver" | null;
const useAuth = () => {
const createUserMutation = useMutation(() =>
auth().createUserWithEmailAndPassword(email, password)
);
const signInMutation = useMutation(() =>
auth().signInWithEmailAndPassword(email, password)
);
const loginMutation = useMutation(() =>
auth().signInWithEmailAndPassword(email, password)
);
const signOutMutation = useMutation(() => auth().signOut());
const {mutateAsync: signUp } = useSignUp()
/*const setProfileDataMutation = useMutation((profileData) => {
const currentUser = auth().currentUser;
if (currentUser) {
return firestore()
.collection("Profiles")
.doc(currentUser.uid)
.set(profileData);
}
});*/
const [user, setUser] = useState<User | null>(null);
const [profileType, setProfileType] = useState<ProfileType>(null);
const [email, setEmail] = useState<string>("");
const [password, setPassword] = useState<string>("");
const handleRegister = async () => {
try {
await signUp({email, password})
// await createUserMutation.mutateAsync();
console.log("User registered!");
// await signInMutation.mutateAsync();
// 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 loginMutation.mutateAsync();
console.log("User signed in!");
} catch (error) {
console.error("Error during sign in:", error);
}
};
const handleSignOut = async () => {
try {
await signOutMutation.mutateAsync();
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("Profiles")
.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;
-61
View File
@@ -1,61 +0,0 @@
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",
email: "[email protected]",
password: "[email protected]"
});
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;
-69
View File
@@ -1,69 +0,0 @@
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: "",
password: "",
email: ""
});
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;
+3 -3
View File
@@ -9,9 +9,9 @@ export const useCreateSubUser = () => {
return useMutation({
mutationKey: ["createSubUser"],
mutationFn: async ({email, password, ...userProfile}: { email: string, password: string } & UserProfile) => {
if(profileType === ProfileType.parent) {
return await functions().httpsCallable("createSubUser")({email, password, ...userProfile})
mutationFn: async ({email, ...userProfile}: { email: string } & UserProfile) => {
if(profileType === ProfileType.PARENT) {
return await functions().httpsCallable("createSubUser")({email, ...userProfile})
} else {
throw Error("Can't create sub-users as a non-parent.")
}
+14
View File
@@ -0,0 +1,14 @@
import { useMutation } from "react-query";
import firestore from "@react-native-firebase/firestore";
export const useSignUp = () => {
return useMutation({
mutationKey: ["getCaregivers"],
mutationFn: async () => {
const snapshot = await firestore()
.collection("Profiles")
.where("userType", "==", "caregiver")
.get();
},
});
};
+20
View File
@@ -0,0 +1,20 @@
import { useMutation } from "react-query";
import auth from "@react-native-firebase/auth";
import { useUpdateUserData } from "@/hooks/firebase/useUpdateUserData";
import { ProfileType } from "@/contexts/AuthContext";
export const useSignIn = () => {
return useMutation({
mutationKey: ["signIn"],
mutationFn: async ({
email,
password,
}: {
email: string;
password: string;
}) => {
await auth().signInWithEmailAndPassword(email, password);
},
});
};
+2 -2
View File
@@ -9,8 +9,8 @@ export const useSignUp = () => {
return useMutation({
mutationKey: ["signUp"],
mutationFn: async ({email, password}: { email: string, password: string }) => {
await auth().createUserWithEmailAndPassword(email, password)
await updateUserData({userType: ProfileType.PARENT, email, password})
await auth().createUserWithEmailAndPassword(email, password);
await updateUserData({userType: ProfileType.PARENT, email, password});
}
});
}
+2 -1
View File
@@ -11,6 +11,7 @@ export const useUpdateUserData = () => {
mutationFn: async (newProfileData: Partial<UserProfile>) => {
if (user) {
try {
console.log("yall don't even");
await firestore()
.collection("Profiles")
.doc(user.uid)
@@ -20,7 +21,7 @@ export const useUpdateUserData = () => {
setProfileData(profileData.data() as UserProfile)
} catch (e) {
console.log(e)
console.error(e)
}
}
}