This commit is contained in:
Kenneth Jannette
2024-03-23 02:23:16 -05:00
parent 9db9e03755
commit b51cdeb857
5 changed files with 216 additions and 124 deletions

View File

@@ -2,7 +2,13 @@ import React, { createContext, useContext, useEffect, useReducer } from "react";
import { useGroupReducer } from "../useReducer/reducers";
import { AuthContext } from "../../Context/AuthProvider";
import { db } from "../../firebase.js";
import { collection, getDocs, query, where, onSnapshot } from "firebase/firestore";
import {
collection,
getDocs,
query,
where,
onSnapshot,
} from "firebase/firestore";
const initialState = {
group: undefined,
@@ -16,9 +22,9 @@ const AppContext = createContext({
const AppProvider = ({ children }) => {
const { currentUserEmail } = useContext(AuthContext);
const [appState, appDispatch] = useReducer(useGroupReducer, initialState);
console.log("currentUserEmail in AppProvider", currentUserEmail);
useEffect(() => {
if (typeof currentUserEmail === 'undefined') {
if (typeof currentUserEmail === "undefined") {
return;
}
if (!currentUserEmail) {
@@ -26,13 +32,10 @@ const AppProvider = ({ children }) => {
return;
}
return onSnapshot(
query(
collection(db, "users"),
where("email", "==", currentUserEmail)
),
snapshot => {
query(collection(db, "users"), where("email", "==", currentUserEmail)),
(snapshot) => {
const doc = snapshot.docs[0];
if (typeof doc === 'undefined') {
if (typeof doc === "undefined") {
appDispatch({ type: "DELETE_GROUP" });
} else {
appDispatch({ type: "CREATE_GROUP", payload: doc.data() });