mirror of
https://github.com/urosran/cally.git
synced 2025-07-10 23:27:18 +00:00
29 lines
1.2 KiB
TypeScript
29 lines
1.2 KiB
TypeScript
import {useMutation, useQueryClient} from "@tanstack/react-query";
|
|
import {UserProfile} from "@/hooks/firebase/types/profileTypes";
|
|
import functions from '@react-native-firebase/functions';
|
|
import {ProfileType, useAuthContext} from "@/contexts/AuthContext";
|
|
import {HttpsCallableResult} from "@firebase/functions";
|
|
|
|
export const useCreateSubUser = () => {
|
|
const queryClient = useQueryClient()
|
|
const {profileType, profileData} = useAuthContext()
|
|
|
|
return useMutation({
|
|
mutationKey: ["createSubUser"],
|
|
mutationFn: async ({email, ...userProfile}: { email?: string } & UserProfile) => {
|
|
if (profileType === ProfileType.PARENT) {
|
|
return await functions().httpsCallable("createSubUser")({
|
|
...userProfile,
|
|
email,
|
|
familyId: profileData?.familyId!
|
|
}) as HttpsCallableResult<{ userId: string }>
|
|
} else {
|
|
throw Error("Can't create sub-users as a non-parent.")
|
|
}
|
|
},
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries({queryKey: ["getChildrenByParentId"]})
|
|
queryClient.invalidateQueries({queryKey: ["familyMembers"]})
|
|
}
|
|
});
|
|
} |