From 138224edb51fa932a0a14453a548990caaacca60 Mon Sep 17 00:00:00 2001 From: coder_023 Date: Sun, 13 Jun 2021 00:54:26 +0530 Subject: [PATCH 01/13] Done with the functionality of adding and deleting of posts from firebase --- src/App.js | 51 +++++++++++++++-- src/Components/HomePage.js | 40 ++++++------- src/Components/LandingBody.js | 2 +- src/Components/PostSection.js | 102 ++++++++++++++++++++++++++++------ src/Components/Posts.js | 46 ++++++++++++--- src/Components/SignIn.js | 1 + src/Components/SignInBody.js | 7 ++- src/Context/PostContext.js | 5 ++ src/Context/actions.types.js | 3 + src/Context/reducer.js | 25 +++++++++ 10 files changed, 230 insertions(+), 52 deletions(-) create mode 100644 src/Context/PostContext.js create mode 100644 src/Context/actions.types.js create mode 100644 src/Context/reducer.js diff --git a/src/App.js b/src/App.js index 38da740..c752e77 100644 --- a/src/App.js +++ b/src/App.js @@ -1,4 +1,4 @@ -import React, { useState } from "react"; +import React, { useEffect,useReducer,useState } from "react"; import './css/App.css'; //react router @@ -10,32 +10,73 @@ import "react-toastify/dist/ReactToastify.min.css"; //firebase +import FirebaseConfig from './Config/FirebaseConfig'; import firebase from "firebase/app"; import "firebase/auth"; +import "firebase/database" -//Context +//ContextAPI STUFF +import reducer from "./Context/reducer"; import UserContext from "./Context/UserContext"; +import PostContext from "./Context/PostContext"; +import { SET_POST,SET_LOADING } from "./Context/actions.types"; import NavigationBar from './Components/NavigationBar'; import Footer from './Components/Footer'; import LandingBody from './Components/LandingBody'; import SignIn from './Components/SignIn'; import SignUp from './Components/SignUp'; -import FirebaseConfig from './Config/FirebaseConfig'; import NotFound from './Components/NotFound'; import HomePage from "./Components/HomePage"; - +//TODO: create useeffect for calling objects from the database //initializing Firebase at root page if (firebase.apps.length === 0) { firebase.initializeApp(FirebaseConfig); } +//first state to be provided in react reducer +const initialState={ + posts:[], + post:{}, + postToUpdate:null, + postToUpdateKey:null, + isLoading:false +}; function App() { + const [state,dispatch] = useReducer(reducer,initialState); const [user,setUser] = useState(null) + const getPosts = async () => { + dispatch({ + type:SET_LOADING, + payload:true + }); + + const postsRef= await firebase.database().ref("/posts"); + postsRef.on("value",snapshot => { + dispatch({ + type:SET_POST, + payload:snapshot.val() + }); + dispatch({ + type:SET_LOADING, + payload:false + }); + }); + + + + };//till here we declared a function which will call the firebase db and fetch a object.The fetched object is then set in dispatch +useEffect(()=>{ + getPosts(); + +},[]); + + return ( + @@ -44,7 +85,7 @@ function App() { - +