Skip to content

Commit

Permalink
Day 24 Done ✨
Browse files Browse the repository at this point in the history
  • Loading branch information
seebham committed Jul 29, 2021
1 parent 62ecd9b commit c9b3c7e
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 26 deletions.
54 changes: 32 additions & 22 deletions Day_024/src/App.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,41 @@
import { BrowserRouter as Router, Switch, Route, Link } from "react-router-dom";
import { BrowserRouter as Router, Switch, Route } from "react-router-dom";
import Home from "./pages/Home";
import Secrets from "./pages/Secrets";
import LayoutWrapper from "./pages/Layout";
import { GlobalProvider } from "./store";
import ProtectedRoute from "./ProtectedRoute";
import { Login } from "./pages/Login";

function App() {
return (
<div className="App">
<Router>
<Switch>
<Route path="/" exact>
<LayoutWrapper>
<Home />
</LayoutWrapper>
</Route>
<Route path="/secrets" exact>
<LayoutWrapper>
<Secrets />
</LayoutWrapper>
</Route>
<Route path="*">
<LayoutWrapper>
<h1 style={{ textAlign: "center" }}>404: page not found :(</h1>
</LayoutWrapper>
</Route>
</Switch>
</Router>
</div>
<GlobalProvider>
<div className="App">
<Router>
<Switch>
<Route path="/" exact>
<LayoutWrapper>
<Home />
</LayoutWrapper>
</Route>
<Route path="/login">
<LayoutWrapper>
<Login />
</LayoutWrapper>
</Route>
<ProtectedRoute path="/secrets" exact>
<LayoutWrapper>
<Secrets />
</LayoutWrapper>
</ProtectedRoute>
<Route path="*">
<LayoutWrapper>
<h1 style={{ textAlign: "center" }}>404: page not found :(</h1>
</LayoutWrapper>
</Route>
</Switch>
</Router>
</div>
</GlobalProvider>
);
}

Expand Down
28 changes: 28 additions & 0 deletions Day_024/src/ProtectedRoute.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { useContext } from "react";
import { Redirect, Route } from "react-router";
import { GlobalContext } from "./store";

const ProtectedRoute = ({ children, ...rest }) => {
const { state } = useContext(GlobalContext);
const { auth } = state;
console.log("Protected Route was called");
return (
<Route
{...rest}
render={({ location }) =>
auth.isAuth ? (
children
) : (
<Redirect
to={{
pathname: "/login",
state: { from: location },
}}
/>
)
}
/>
);
};

export default ProtectedRoute;
12 changes: 10 additions & 2 deletions Day_024/src/pages/Layout.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from "react";
import { Link } from "react-router-dom";
import React, { useContext, useEffect } from "react";
import { Link, Redirect } from "react-router-dom";
import styled from "styled-components";
import { GlobalContext } from "../store";

//styled-components
const AppContainer = styled.div`
Expand Down Expand Up @@ -47,6 +48,13 @@ const Content = styled.main`
`;

const Layout = ({ children }) => {
const { state } = useContext(GlobalContext);
useEffect(() => {
if (state.auth.isAuth) {
console.log("Should Redirect to Secrets");
<Redirect to="/secrets" />;
}
}, [state]);
return (
<AppContainer>
<NavBarContainer>
Expand Down
8 changes: 8 additions & 0 deletions Day_024/src/pages/Login.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import React, { useContext, useEffect } from "react";
import { GlobalContext } from "../store";
import { Redirect } from "react-router";

export const Login = () => {
const { dispatch } = useContext(GlobalContext);
return <div onClick={() => dispatch({ type: "LOGIN" })}>Login</div>;
};
9 changes: 7 additions & 2 deletions Day_024/src/pages/Secrets.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import React from "react";
import React, { useContext } from "react";
import { GlobalContext } from "../store";

const Secrets = () => {
return <div>Secrets</div>;
const { state } = useContext(GlobalContext);
React.useEffect(() => {
console.log(state);
}, [state]);
return <div>{state.auth.isLoaded}</div>;
};

export default Secrets;
38 changes: 38 additions & 0 deletions Day_024/src/store.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import React, { useReducer } from "react";

//Gobal initialState
export const initialState = {
auth: {
isLoaded: false,
isAuth: false,
authObj: null,
},
theme: {
light: { background: "#fff" },
dark: { background: "#111" },
},
};

//Global Context
export const GlobalContext = React.createContext();

//Global Reducer
const GlobalReducer = (state = initialState, action) => {
switch (action.type) {
case "LOGIN":
console.log("logged in");
return { ...state, auth: { isAuth: true, isLoaded: true } };
default:
return state;
}
};

//GlobalContext Provider
export const GlobalProvider = ({ children }) => {
const [state, dispatch] = useReducer(GlobalReducer, initialState);
return (
<GlobalContext.Provider value={{ state, dispatch }}>
{children}
</GlobalContext.Provider>
);
};

0 comments on commit c9b3c7e

Please sign in to comment.