Update logic for profile creation

This commit is contained in:
Milan Paunovic
2024-08-21 20:03:40 +02:00
parent 4a20f7a53e
commit 957e0a582e
16 changed files with 1670 additions and 2228 deletions

View File

@ -1,172 +1,132 @@
import { useState, useEffect } from "react";
import { useMutation } from "react-query";
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,
CaregiverProfile,
ChildProfile,
ParentProfile,
} from "./types/profileTypes";
import {User,} from "./types/profileTypes";
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 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>("");
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 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 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 setProfileDataMutation = useMutation((profileData) => {
const currentUser = auth().currentUser;
if (currentUser) {
await firestore()
return 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("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,
};
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 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;