Skip to content
Snippets Groups Projects

Draft: Master

Merged Thi Nguyen requested to merge master into feature/update-08.01
Files
5
+ 0
53
import { View, Text } from 'react-native'
import React, { useState, useLayoutEffect } from "react";
import { auth, database } from "../config/firebase";
import { collection, query, onSnapshot, where, and, addDoc } from "firebase/firestore";
import CatsitterHome from './cat-sitter/CatsitterHome';
import CatOwnerHome from './tabs/CatownerHome';
import CatsitterSetting from './cat-sitter/CatsitterSetting';
const Home = () => {
//Get current profile
const [userProfile, setUserProfile] = useState({});
useLayoutEffect(() => {
const id = auth?.currentUser?.uid;
if (!id) {
navigate("/")
return
};
const collectionRef = collection(database, "profiles");
const q = query(collectionRef, and(where("userId", "==", id)),);
const unsubscribe = onSnapshot(q, (querySnapshot) => {
if (querySnapshot.docs.length > 0) {
setUserProfile(querySnapshot.docs[0].data())
} else {
createUserProfile(auth?.currentUser?.uid)
}
});
return unsubscribe;
}, []);
const createUserProfile = (userId) => {
addDoc(collection(database, "profiles"), {
createdAt: new Date(),
userId: userId,
role: "cat-owner"
});
}
if (userProfile.role == "cat-owner") {
return <CatOwnerHome />
} else if (userProfile.role == "cat-sitter" && userProfile.updatedAt) {
return <CatsitterHome />
} else if (userProfile.role == "cat-sitter" && !userProfile.updatedAt) {
return <CatsitterSetting />
}
return <View>
<Text>Loading screen</Text>
</View>
}
export default Home
\ No newline at end of file
Loading