18.12 Updating the UI Depending on Auth State
Layout.js
src\components\Layout\Layout.js
import React, { Component, Fragment } from "react";
import { connect } from "react-redux";
import SideDrawer from "../Navigation/SideDrawer/SideDrawer";
import Toolbar from "../Navigation/Tollbar/Toolbar";
import classes from "./Layout.module.css";
class Layout extends Component {
state = {
showSideDrawer: false,
};
sideDrawerClosedHandler = () => {
this.setState({ showSideDrawer: false });
};
sideDrawerToggleHandler = () => {
this.setState((prevState) => {
return { showSideDrawer: !prevState.showSideDrawer };
});
};
render() {
return (
<Fragment>
<Toolbar
isAuth={this.props.isAuthenticated}
drawerToggleClicked={this.sideDrawerToggleHandler}
/>
<SideDrawer
isAuth={this.props.isAuthenticated}
closed={this.sideDrawerClosedHandler}
open={this.state.showSideDrawer}
/>
<main className={classes.Content}>{this.props.children}</main>
</Fragment>
);
}
}
const mapStateToProps = (state) => {
return {
isAuthenticated: state.auth.token !== null,
};
};
export default connect(mapStateToProps)(Layout);
NavigationItems.js
src\components\Navigation\NavigationItems\NavigationItems.js
import React from "react";
import NavigationItem from "./NavigationItem/NavigationItem";
import classes from "./NavigationItems.module.css";
const navigationItems = (props) => (
<ul className={classes.NavigationItems}>
<NavigationItem link="/" exact>
Burger Builder
</NavigationItem>
<NavigationItem link="/orders">Orders</NavigationItem>
{!props.isAuthenticated ? (
<NavigationItem link="/auth">Authenticate</NavigationItem>
) : (
<NavigationItem link="/logout">Logout</NavigationItem>
)}
</ul>
);
export default navigationItems;
SideDrawer.js
src\components\Navigation\SideDrawer\SideDrawer.js
import React, { Fragment } from "react";
import Logo from "../../Logo/Logo";
import Backdrop from "../../UI/Backdrop/Backdrop";
import NavigationItems from "../NavigationItems/NavigationItems";
import classes from "./SideDrawer.module.css";
const sideDrawer = (props) => {
let attachedClasses = [classes.SideDrawer, classes.Close];
if (props.open) {
attachedClasses = [classes.SideDrawer, classes.Open];
}
return (
<Fragment>
<Backdrop show={props.open} clicked={props.closed} />
<div className={attachedClasses.join(" ")}>
<div className={classes.Logo}>
<Logo />
</div>
<nav>
<NavigationItems isAuthenticated={props.isAuth} />
</nav>
</div>
</Fragment>
);
};
export default sideDrawer;
Toolbar.js
src\components\Navigation\Tollbar\Toolbar.js
import React from "react";
import Logo from "../../Logo/Logo";
import NavigationItems from "../NavigationItems/NavigationItems";
import DrawerToggle from "../SideDrawer/DrawerToggle/DrawerToggle";
import classes from "./Toolbar.module.css";
const toolbar = (props) => (
<header className={classes.Toolbar}>
<DrawerToggle clicked={props.drawerToggleClicked} />
<div className={classes.Logo}>
<Logo />
</div>
<nav className={classes.DesktopOnly}>
<NavigationItems isAuthenticated={props.isAuth} />
</nav>
</header>
);
export default toolbar;