From 664d01367eb31356931d48072b8aa82d2aba2548 Mon Sep 17 00:00:00 2001 From: Harkirat Singh Date: Sat, 22 Jul 2023 03:30:46 +0530 Subject: [PATCH] Checkpoint #2 -> State at the top --- admin-client/src/App.jsx | 27 +++++++++++++++++++++---- admin-client/src/components/Appbar.jsx | 24 ++-------------------- admin-client/src/components/Courses.jsx | 1 + admin-client/src/components/Landing.jsx | 6 +++--- admin-client/src/components/Signin.jsx | 6 +++++- admin-client/src/components/Signup.jsx | 8 ++++++-- 6 files changed, 40 insertions(+), 32 deletions(-) diff --git a/admin-client/src/App.jsx b/admin-client/src/App.jsx index 503b4a5..dae99de 100644 --- a/admin-client/src/App.jsx +++ b/admin-client/src/App.jsx @@ -6,23 +6,42 @@ import AddCourse from "./components/AddCourse.jsx"; import Courses from "./components/Courses"; import Course from "./components/Course"; import {Landing} from "./components/Landing.jsx"; +import {useState, useEffect} from "react"; +import axios from "axios"; +import {BASE_URL} from "./config.js"; function App() { + const [userEmail, setUserEmail] = useState(null) + const init = async() => { + const response = await axios.get(`${BASE_URL}/admin/me`, { + headers: { + "Authorization": "Bearer " + localStorage.getItem("token") + } + }) + + if (response.data.username) { + setUserEmail(response.data.username) + } + }; + + useEffect(() => { + init(); + }, []); return (
- + } /> } /> } /> - } /> - } /> - } /> + } /> + } /> + } /> diff --git a/admin-client/src/components/Appbar.jsx b/admin-client/src/components/Appbar.jsx index 1a1ba67..e145d03 100644 --- a/admin-client/src/components/Appbar.jsx +++ b/admin-client/src/components/Appbar.jsx @@ -1,29 +1,9 @@ import {Typography} from "@mui/material"; import Button from "@mui/material/Button"; -import { useEffect, useState } from "react"; import {useNavigate} from "react-router-dom"; -import { BASE_URL } from "../config.js"; -import axios from "axios"; -function Appbar() { +function Appbar({userEmail, setUserEmail}) { const navigate = useNavigate() - const [userEmail, setUserEmail] = useState(null); - - const init = async() => { - const response = await axios.get(`${BASE_URL}/admin/me`, { - headers: { - "Authorization": "Bearer " + localStorage.getItem("token") - } - }) - - if (response.data.username) { - setUserEmail(response.data.username) - } - }; - - useEffect(() => { - init(); - }, []); if (userEmail) { return
{ localStorage.setItem("token", null); - window.location = "/"; + setUserEmail(null); }} >Logout
diff --git a/admin-client/src/components/Courses.jsx b/admin-client/src/components/Courses.jsx index deaa233..a99631d 100644 --- a/admin-client/src/components/Courses.jsx +++ b/admin-client/src/components/Courses.jsx @@ -15,6 +15,7 @@ function Courses() { }) setCourses(response.data.courses) } + useEffect(() => { init(); }, []); diff --git a/admin-client/src/components/Landing.jsx b/admin-client/src/components/Landing.jsx index f3320a3..8c62fab 100644 --- a/admin-client/src/components/Landing.jsx +++ b/admin-client/src/components/Landing.jsx @@ -3,7 +3,7 @@ import Button from "@mui/material/Button"; import {useNavigate} from "react-router-dom"; -export const Landing = () => { +export const Landing = ({userEmail}) => { const navigate = useNavigate() return
@@ -15,7 +15,7 @@ export const Landing = () => { A place to learn, earn and grow -
+ {!userEmail &&
-
+
}
diff --git a/admin-client/src/components/Signin.jsx b/admin-client/src/components/Signin.jsx index 98dc290..0090d45 100644 --- a/admin-client/src/components/Signin.jsx +++ b/admin-client/src/components/Signin.jsx @@ -3,10 +3,12 @@ import TextField from "@mui/material/TextField"; import {Card, Typography} from "@mui/material"; import {useState} from "react"; import axios from "axios"; +import {useNavigate} from "react-router-dom"; function Signin() { const [email, setEmail] = useState("") const [password, setPassword] = useState("") + const navigate = useNavigate() return
Signin diff --git a/admin-client/src/components/Signup.jsx b/admin-client/src/components/Signup.jsx index 15b4930..aa5df8d 100644 --- a/admin-client/src/components/Signup.jsx +++ b/admin-client/src/components/Signup.jsx @@ -4,10 +4,12 @@ import {Card, Typography} from "@mui/material"; import {useState} from "react"; import axios from "axios"; import { BASE_URL } from "../config.js"; +import {useNavigate} from "react-router-dom"; -function Signup() { +function Signup({setUserEmail}) { const [email, setEmail] = useState("") const [password, setPassword] = useState("") + const navigate = useNavigate() return
Signup