Adding and displaying family members

This commit is contained in:
Milan Paunovic
2024-09-28 19:08:09 +02:00
parent 594e54b11b
commit 92f335543f
7 changed files with 189 additions and 149 deletions
+1
View File
@@ -15,6 +15,7 @@ export interface User {
contact?: string;
email: string
password: string
familyId?: string
}
export interface ParentProfile extends UserProfile {
+6 -3
View File
@@ -5,13 +5,16 @@ import {ProfileType, useAuthContext} from "@/contexts/AuthContext";
export const useCreateSubUser = () => {
const queryClient = useQueryClient()
const { profileType } = useAuthContext()
const {profileType, profileData} = useAuthContext()
return useMutation({
mutationKey: ["createSubUser"],
mutationFn: async ({email, ...userProfile}: { email: string } & UserProfile) => {
if(profileType === ProfileType.PARENT) {
return await functions().httpsCallable("createSubUser")({email, ...userProfile})
if (profileType === ProfileType.PARENT) {
return await functions().httpsCallable("createSubUser")({
email,
userProfile: {...userProfile, familyId: profileData?.familyId}
})
} else {
throw Error("Can't create sub-users as a non-parent.")
}
+30
View File
@@ -0,0 +1,30 @@
import {useAuthContext} from "@/contexts/AuthContext";
import {useMutation} from "react-query";
import firestore from "@react-native-firebase/firestore";
import {UserProfile} from "@/hooks/firebase/types/profileTypes";
import {FirebaseAuthTypes} from "@react-native-firebase/auth";
export const useEditFamily = () => {
const {user: currentUser, setProfileData} = useAuthContext()
return useMutation({
mutationKey: ["editFamily"],
mutationFn: async ({newUserData, customUser}: {newUserData: Partial<UserProfile>, customUser?: FirebaseAuthTypes.User }) => {
const user = currentUser ?? customUser
if (user) {
try {
await firestore()
.collection("Families")
.doc(user.uid)
.set(newUserData);
const profileData = await firestore().collection("Profiles").doc(user?.uid!).get()
setProfileData(profileData.data() as UserProfile)
} catch (e) {
console.error(e)
}
}
}
})
}
+24
View File
@@ -0,0 +1,24 @@
import {useQuery} from "react-query";
import firestore from "@react-native-firebase/firestore";
import {useAuthContext} from "@/contexts/AuthContext";
import {UserProfile} from "@/hooks/firebase/types/profileTypes";
export const useGetFamilyMembers = (excludeSelf?: boolean) => {
const {profileData, user} = useAuthContext()
return useQuery({
queryKey: ["familyMembers", user?.uid],
queryFn: async (): Promise<undefined | UserProfile[]> => {
const snapshot = await firestore()
.collection("Profiles")
.where("familyId", "==", profileData?.familyId)
.get();
if (excludeSelf) {
return snapshot.docs.map((doc) => doc.data()).filter((doc) => doc.id !== user?.uid) as UserProfile[];
}
return snapshot.docs.map((doc) => doc.data()) as UserProfile[];
}
})
}
+2
View File
@@ -2,6 +2,7 @@ import { useMutation } from "react-query";
import auth from "@react-native-firebase/auth";
import { ProfileType } from "@/contexts/AuthContext";
import { useSetUserData } from "./useSetUserData";
import {uuidv4} from "@firebase/util";
export const useSignUp = () => {
const { mutateAsync: setUserData } = useSetUserData();
@@ -28,6 +29,7 @@ export const useSignUp = () => {
userType: ProfileType.PARENT,
firstName: firstName,
lastName: lastName,
familyId: uuidv4(),
},
customUser: res.user,
});