Skip to content

Commit

Permalink
fix: simplify code and check login status on home page load
Browse files Browse the repository at this point in the history
  • Loading branch information
9p4 committed Jun 1, 2022
1 parent 036e577 commit ddb3f44
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 68 deletions.
139 changes: 71 additions & 68 deletions frontend/src/components/Bar/Bar.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,15 @@ function Bar() {
// name: "Settings",
// to: "/settings",
// },
!disabledAuth && {
name: "Log out",
divide: true,
onClick: onLogOutClick,
},
...(!disabledAuth
? [
{
name: "Log out",
divide: true,
onClick: onLogOutClick,
},
]
: []),
];

return (
Expand All @@ -74,69 +78,68 @@ function Bar() {
</Typography>
</Box>
{/* The filter removes all elements that are "true" or "false" */}
{loggedIn &&
menuItems.filter((e) => typeof e !== "boolean").length > 0 && (
<>
<Button color="inherit" onClick={openMenu}>
<MenuIcon></MenuIcon>
</Button>

<Menu
anchorEl={anchorEl}
open={Boolean(anchorEl)}
onClose={closeMenu}
>
{menuItems.map((menuItem, index) => {
if (
menuItem.hasOwnProperty("condition") &&
!menuItem.condition
) {
return null;
}

let component = null;

if (menuItem.to) {
component = (
<MenuItem
key={index}
component={RouterLink}
to={menuItem.to}
onClick={closeMenu}
>
{menuItem.name}
</MenuItem>
);
} else {
component = (
<MenuItem
key={index}
onClick={() => {
closeMenu();

menuItem.onClick();
}}
>
{menuItem.name}
</MenuItem>
);
}

if (menuItem.divide) {
return (
<span key={index}>
<Divider />

{component}
</span>
);
}

return component;
})}
</Menu>
</>
)}
{loggedIn && menuItems.length > 0 && (
<>
<Button color="inherit" onClick={openMenu}>
<MenuIcon></MenuIcon>
</Button>

<Menu
anchorEl={anchorEl}
open={Boolean(anchorEl)}
onClose={closeMenu}
>
{menuItems.map((menuItem, index) => {
if (
menuItem.hasOwnProperty("condition") &&
!menuItem.condition
) {
return null;
}

let component = null;

if (menuItem.to) {
component = (
<MenuItem
key={index}
component={RouterLink}
to={menuItem.to}
onClick={closeMenu}
>
{menuItem.name}
</MenuItem>
);
} else {
component = (
<MenuItem
key={index}
onClick={() => {
closeMenu();

menuItem.onClick();
}}
>
{menuItem.name}
</MenuItem>
);
}

if (menuItem.divide) {
return (
<span key={index}>
<Divider />

{component}
</span>
);
}

return component;
})}
</Menu>
</>
)}
{!loggedIn && LogIn()}
</Toolbar>
</AppBar>
Expand Down
14 changes: 14 additions & 0 deletions frontend/src/components/HomeLoggedIn/HomeLoggedIn.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { useState, useEffect } from "react";
import { useHistory } from "react-router-dom";
import { useLocalStorage } from "react-use";
import axios from "axios";

import { Divider, Button, Grid, Typography, Box } from "@material-ui/core";
import useStyles from "./HomeLoggedIn.styles";
Expand All @@ -11,10 +13,22 @@ import { generateNetworkConfig } from "utils/NetworkConfig";

function HomeLoggedIn() {
const [networks, setNetworks] = useState([]);
const [, setLoggedIn] = useLocalStorage("loggedIn", false);
const [, setDisableAuth] = useLocalStorage("disableAuth", false);
const [token, setToken] = useLocalStorage("token", null);

const classes = useStyles();
const history = useHistory();

axios.get("/auth/login").then(function (response) {
if (response.data.enabled) {
setDisableAuth(false);
if (!token || token.length === 0) {
setLoggedIn(false);
}
}
});

const createNetwork = async () => {
const network = await API.post("network", generateNetworkConfig());
console.log(network);
Expand Down

0 comments on commit ddb3f44

Please sign in to comment.