From fb8d0db00c929ee6543fc1668f5a6a09fcef5293 Mon Sep 17 00:00:00 2001 From: Naimish Date: Tue, 23 Aug 2022 19:01:35 +0530 Subject: [PATCH] used react hooks, changed many classes to function based components, redirect dashboard problem --- src/App.js | 114 +++++++++++--------- src/components/EmailPassword/index.js | 89 ++++++---------- src/components/Header/index.js | 6 ++ src/components/SignIn/index.js | 111 ++++++++----------- src/components/Signup/index.js | 147 ++++++++++++-------------- src/customHooks/index.js | 3 + src/customHooks/useAuth.js | 19 ++++ src/hoc/withAuth.js | 6 ++ src/pages/Dashboard/index.js | 8 ++ src/pages/Dashboard/styles.scss | 0 10 files changed, 250 insertions(+), 253 deletions(-) create mode 100644 src/customHooks/index.js create mode 100644 src/customHooks/useAuth.js create mode 100644 src/hoc/withAuth.js create mode 100644 src/pages/Dashboard/index.js create mode 100644 src/pages/Dashboard/styles.scss diff --git a/src/App.js b/src/App.js index 7d8c2b6..6b40bbd 100644 --- a/src/App.js +++ b/src/App.js @@ -1,9 +1,12 @@ -import React, { Component } from "react"; +import React, { useEffect } from "react"; import { connect } from "react-redux/es/exports"; import { Route, Routes, Navigate } from "react-router-dom"; import { auth, handleUserProfile } from "./firebase/utils"; import { setCurrentUser } from "./redux/User/user.actions"; +//hoc +import WithAuth from "./hoc/withAuth"; + //layouts import MainLayout from "./layouts/MainLayout"; import HomepageLayout from "./layouts/HomepageLayout"; @@ -13,13 +16,13 @@ import HomePage from "./pages/Homepage"; import Login from "./pages/Login"; import Recovery from "./pages/Recovery"; import Registration from "./pages/Registration"; +import Dashboard from "./pages/Dashboard"; -class App extends Component { - authListener = null; - componentDidMount() { - const { setCurrentUser } = this.props; +const App = (props) => { + const { setCurrentUser, currentUser } = props; - this.authListener = auth.onAuthStateChanged(async (userAuth) => { + useEffect(() => { + const authListener = auth.onAuthStateChanged(async (userAuth) => { if (userAuth) { const userRef = await handleUserProfile(userAuth); userRef.onSnapshot((snapshot) => { @@ -31,55 +34,62 @@ class App extends Component { } setCurrentUser(userAuth); }); - } - componentWillUnmount() { - this.authListener(); - } + return () => { + authListener(); + }; + }, []); - render() { - const { currentUser } = this.props; - return ( -
- - - - - } - /> - - - {currentUser ? : null} - - } - /> - - - {currentUser ? : null} - - } - /> - + + + + + } + /> + + + {currentUser ? : null} + + } + /> + + + {currentUser ? : null} + + } + /> + + + + } + /> + - + - } - /> - -
- ); - } -} + + } + /> + + + ); +}; const mapStateToProps = ({ user }) => ({ currentUser: user.currentUser, diff --git a/src/components/EmailPassword/index.js b/src/components/EmailPassword/index.js index 31c46c2..02466b0 100644 --- a/src/components/EmailPassword/index.js +++ b/src/components/EmailPassword/index.js @@ -1,4 +1,4 @@ -import React, { Component } from "react"; +import React, { useState } from "react"; import "./styles.scss"; import { withRouter } from "../../withRouter"; @@ -8,79 +8,58 @@ import Button from "../forms/Button"; import { auth } from "../../firebase/utils"; -const initialState = { - email: "", - errors: [], -}; +const EmailPassword = (props) => { + const [email, setEmail] = useState(""); + const [errors, setErrors] = useState([]); -class EmailPassword extends Component { - constructor(props) { - super(props); - this.state = { - ...initialState, - }; - this.handleChange = this.handleChange.bind(this); - } - handleChange(e) { - const { name, value } = e.target; - this.setState({ - [name]: value, - }); - } - handleSubmit = async (e) => { + const handleSubmit = async (e) => { e.preventDefault(); try { - const { email } = this.state; const config = { url: "http://localhost:3000/login", }; await auth .sendPasswordResetEmail(email, config) .then(() => { - this.props.navigate("/login"); + props.navigate("/login"); }) .catch((e) => { console.log(e); const err = ["Email not found, please try again"]; - this.setState({ - errors: err, - }); + setErrors(err); }); } catch (err) { console.log(err); } }; - render() { - const { email, errors } = this.state; - const configAuthWrapper = { - headline: "Email Password", - }; + const configAuthWrapper = { + headline: "Email Password", + }; - return ( - -
- {errors.length > 0 && ( -
    - {errors.map((e, index) => { - return
  • {e}
  • ; - })} -
- )} -
- - - -
-
- ); - } -} + return ( + +
+ {errors.length > 0 && ( +
    + {errors.map((e, index) => { + return
  • {e}
  • ; + })} +
+ )} +
+ setEmail(e.target.value)} + /> + + +
+
+ ); +}; export default withRouter(EmailPassword); diff --git a/src/components/Header/index.js b/src/components/Header/index.js index a52130c..5281850 100644 --- a/src/components/Header/index.js +++ b/src/components/Header/index.js @@ -21,6 +21,9 @@ const Header = (props) => {
{currentUser && (