first commit
This commit is contained in:
51
src/Hooks/useContext/appContext.js
Executable file
51
src/Hooks/useContext/appContext.js
Executable file
@@ -0,0 +1,51 @@
|
||||
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";
|
||||
|
||||
const initialState = {
|
||||
group: undefined,
|
||||
};
|
||||
|
||||
const AppContext = createContext({
|
||||
state: initialState,
|
||||
dispatch: () => null,
|
||||
});
|
||||
|
||||
const AppProvider = ({ children }) => {
|
||||
const { currentUserEmail } = useContext(AuthContext);
|
||||
const [appState, appDispatch] = useReducer(useGroupReducer, initialState);
|
||||
|
||||
useEffect(() => {
|
||||
if (typeof currentUserEmail === 'undefined') {
|
||||
return;
|
||||
}
|
||||
if (!currentUserEmail) {
|
||||
appDispatch({ type: "DELETE_GROUP" });
|
||||
return;
|
||||
}
|
||||
return onSnapshot(
|
||||
query(
|
||||
collection(db, "users"),
|
||||
where("email", "==", currentUserEmail)
|
||||
),
|
||||
snapshot => {
|
||||
const doc = snapshot.docs[0];
|
||||
if (typeof doc === 'undefined') {
|
||||
appDispatch({ type: "DELETE_GROUP" });
|
||||
} else {
|
||||
appDispatch({ type: "CREATE_GROUP", payload: doc.data() });
|
||||
}
|
||||
}
|
||||
);
|
||||
}, [currentUserEmail]);
|
||||
|
||||
return (
|
||||
<AppContext.Provider value={{ appState, appDispatch }}>
|
||||
{children}
|
||||
</AppContext.Provider>
|
||||
);
|
||||
};
|
||||
|
||||
export { AppContext, AppProvider };
|
||||
22
src/Hooks/useContext/dataContext.js
Normal file
22
src/Hooks/useContext/dataContext.js
Normal file
@@ -0,0 +1,22 @@
|
||||
import React, { createContext, useReducer } from "react";
|
||||
import { useDataReducer } from "../useReducer/reducers";
|
||||
|
||||
const initialState = {
|
||||
cases: [{}],
|
||||
};
|
||||
|
||||
const DataContext = createContext({
|
||||
state: initialState,
|
||||
dispatch: () => null,
|
||||
});
|
||||
|
||||
const DataProvider = ({ children }) => {
|
||||
const [dataState, dataDispatch] = useReducer(useDataReducer, initialState);
|
||||
return (
|
||||
<DataContext.Provider value={{ dataState, dataDispatch }}>
|
||||
{children}
|
||||
</DataContext.Provider>
|
||||
);
|
||||
};
|
||||
|
||||
export { DataContext, DataProvider };
|
||||
15
src/Hooks/useQueries/queries.js
Normal file
15
src/Hooks/useQueries/queries.js
Normal file
@@ -0,0 +1,15 @@
|
||||
import { useQuery } from "react-query";
|
||||
import { db } from "../../firebase";
|
||||
import { collection } from "firebase/firestore";
|
||||
|
||||
const casesRef = collection(db, "cases");
|
||||
|
||||
export const useCases = (userId) => {
|
||||
return useQuery(userId, async () => {
|
||||
const queryParams = {
|
||||
userId: userId,
|
||||
};
|
||||
const response = await someEndpoint;
|
||||
//return response
|
||||
});
|
||||
};
|
||||
25
src/Hooks/useReducer/reducers.js
Executable file
25
src/Hooks/useReducer/reducers.js
Executable file
@@ -0,0 +1,25 @@
|
||||
export const useGroupReducer = (state, action) => {
|
||||
switch (action.type) {
|
||||
case "CREATE_GROUP":
|
||||
return { ...state, group: action.payload };
|
||||
case "UPDATE_GROUP_NAME":
|
||||
console.log("state, action.payload", state, action.payload);
|
||||
return [...state, { name: action.payload }];
|
||||
case "DELETE_GROUP":
|
||||
return { ...state, group: null };
|
||||
default:
|
||||
return state;
|
||||
}
|
||||
};
|
||||
|
||||
export const useDataReducer = (state, action) => {
|
||||
switch (action.type) {
|
||||
case "CREATE_CASES":
|
||||
return { ...state, cases: action.payload };
|
||||
case "UPDATE_GROUP_NAME":
|
||||
console.log("state, action.payload", state, action.payload);
|
||||
return [...state, { cases: action.payload }];
|
||||
default:
|
||||
return state;
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user