From db3d0ce4d3525359dee091d6013c391c796a14f5 Mon Sep 17 00:00:00 2001 From: Daniel Cortes Date: Tue, 23 Jun 2020 02:07:01 -0400 Subject: [PATCH] Stupid basic auth --- src/components/Nav.jsx | 49 ++++++++++++++++++++++++++++++++++-------- src/index.jsx | 10 +-------- src/services/State.jsx | 33 +++++++++++++++++++++++----- 3 files changed, 69 insertions(+), 23 deletions(-) diff --git a/src/components/Nav.jsx b/src/components/Nav.jsx index 283b0e0..58f63cb 100644 --- a/src/components/Nav.jsx +++ b/src/components/Nav.jsx @@ -1,13 +1,44 @@ import React from "react"; import {Link} from "react-router-dom"; import './Nav.scss'; +import {useStateValue} from '../services/State' -export const Nav = () => ( - -) +export const Nav = () => { + const [context, dispatch] = useStateValue(); + + const handleLogin = () => dispatch({type: 'login', user: {auth: true}}) + const handleLogout = () => dispatch({type: 'logout', user: {auth: false}}) + + const showLogin = () => { + return context.user.auth === false; + } + + const buttons = () => { + if (showLogin()) { + return + }else { + return + } + } + + return ( + + ) +} diff --git a/src/index.jsx b/src/index.jsx index f8297f3..2d8d1ef 100644 --- a/src/index.jsx +++ b/src/index.jsx @@ -62,16 +62,8 @@ const App = () => ( ); const AppWithState = () => { - const initialState = {theme: {primary: 'green'}}; - - const reducer = (state, action) => { - switch (action.type) { - default: return state; - } - }; - return ( - + ); diff --git a/src/services/State.jsx b/src/services/State.jsx index 76febae..7a6b60b 100644 --- a/src/services/State.jsx +++ b/src/services/State.jsx @@ -1,11 +1,34 @@ import React, {createContext, useContext, useReducer} from 'react'; +const initialState = {user: {auth: false}}; + +const reducer = (state, action) => { + console.log(state, action); + + switch (action.type) { + case 'login': + return { + ...state, + user: action.user + } + case 'logout': + return { + ...state, + user: action.user + } + default: return state; + } +}; + export const StateContext = createContext(null); -export const StateProvider = ({reducer, initialState, children}) => ( - - {children} - -); +export const StateProvider = ({children}) => { + + return ( + + {children} + + ); +} export const useStateValue = () => useContext(StateContext);