Skip to content

Commit

Permalink
Checkpoint 100xDevs-hkirat#2 -> State at the top
Browse files Browse the repository at this point in the history
  • Loading branch information
hkirat committed Jul 21, 2023
1 parent 29a8ca3 commit 664d013
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 32 deletions.
27 changes: 23 additions & 4 deletions admin-client/src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
<div style={{width: "100vw",
height: "100vh",
backgroundColor: "#eeeeee"}}
>
<Router>
<Appbar />
<Appbar userEmail={userEmail} setUserEmail={setUserEmail}/>
<Routes>
<Route path={"/addcourse"} element={<AddCourse />} />
<Route path={"/course/:courseId"} element={<Course />} />
<Route path={"/courses"} element={<Courses />} />
<Route path={"/signin"} element={<Signin />} />
<Route path={"/signup"} element={<Signup />} />
<Route path={"/"} element={<Landing />} />
<Route path={"/signin"} element={<Signin setUserEmail={setUserEmail} />} />
<Route path={"/signup"} element={<Signup setUserEmail={setUserEmail} />} />
<Route path={"/"} element={<Landing userEmail={userEmail} />} />
</Routes>
</Router>

Expand Down
24 changes: 2 additions & 22 deletions admin-client/src/components/Appbar.jsx
Original file line number Diff line number Diff line change
@@ -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 <div style={{
Expand Down Expand Up @@ -60,7 +40,7 @@ function Appbar() {
variant={"contained"}
onClick={() => {
localStorage.setItem("token", null);
window.location = "/";
setUserEmail(null);
}}
>Logout</Button>
</div>
Expand Down
1 change: 1 addition & 0 deletions admin-client/src/components/Courses.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ function Courses() {
})
setCourses(response.data.courses)
}

useEffect(() => {
init();
}, []);
Expand Down
6 changes: 3 additions & 3 deletions admin-client/src/components/Landing.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 <div>
<Grid container style={{padding: "5vw"}}>
Expand All @@ -15,7 +15,7 @@ export const Landing = () => {
<Typography variant={"h5"}>
A place to learn, earn and grow
</Typography>
<div style={{display: "flex", marginTop: 20}}>
{!userEmail && <div style={{display: "flex", marginTop: 20}}>
<div style={{marginRight: 10}}>
<Button
size={"large"}
Expand All @@ -34,7 +34,7 @@ export const Landing = () => {
}}
>Signin</Button>
</div>
</div>
</div>}
</div>
<div>
</div>
Expand Down
6 changes: 5 additions & 1 deletion admin-client/src/components/Signin.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 <div>
<div style={{
Expand Down Expand Up @@ -57,7 +59,9 @@ function Signin() {
const data = res.data;

localStorage.setItem("token", data.token);
window.location = "/"
// window.location = "/"
setUserEmail(email)
navigate("/courses")
}}

> Signin</Button>
Expand Down
8 changes: 6 additions & 2 deletions admin-client/src/components/Signup.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 <div>
<div style={{
Expand Down Expand Up @@ -52,7 +54,9 @@ function Signup() {
})
let data = response.data;
localStorage.setItem("token", data.token);
window.location = "/"
// window.location = "/"
setUserEmail(email)
navigate("/courses")
}}

> Signup</Button>
Expand Down

0 comments on commit 664d013

Please sign in to comment.