Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cwn-dashboard): added a preview of the customizable navbar #1342

Open
wants to merge 1 commit into
base: beta
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions src/components/Navbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ import {
Typography,
} from '@mui/material';
import Image from 'next/image';
import { useState } from 'react';
import { useState, useEffect } from 'react';
import { initialState } from 'context/configContext';
import { getStorageValue } from 'hooks/globalHooks/useLocalStorage';
import MenuBar from 'images/MenuBar';
import { makeStyles } from 'models/makeStyles';
import ChangeThemeButton from './ChangeThemeButton';
Expand Down Expand Up @@ -60,11 +61,11 @@ const useStyles = makeStyles()((theme) => ({
},
}));

function Navbar() {
function Navbar({ preview }) {
const [anchorEl, setAnchorEl] = useState(null);
const isMobile = useMobile();
const [webMapConfig] = useLocalStorage('config', initialState);
const { items: navItems } = webMapConfig.navbar;
const [navItems, setNavItems] = useState(webMapConfig.navbar.items);

const open = Boolean(anchorEl);
const handleMenuClick = (event) => {
Expand All @@ -74,10 +75,24 @@ function Navbar() {
setAnchorEl(null);
};
const { classes } = useStyles();

// Detect webMapConfig changes. Reactively update the nav-items;
useEffect(() => {
const updateFunction = () => {
const localWebMapConfig = getStorageValue('config', initialState);
setNavItems(localWebMapConfig.navbar.items);
};
window.addEventListener('storage', updateFunction);
return () => {
window.removeEventListener('storage', updateFunction);
};
}, []);

return (
<AppBar
elevation={4}
className={classes.navContainer}
sx={preview ? { width: '100%!important' } : null}
color="default"
position="static"
>
Expand Down
3 changes: 2 additions & 1 deletion src/hooks/globalHooks/useLocalStorage.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useState, useEffect } from 'react';
const KEY_PREFIX = 'greenstand-web-map-client-';

// Get value from localStorage if possible, otherwise return provided default
function getStorageValue(key, defaultValue) {
export function getStorageValue(key, defaultValue) {
if (typeof window !== 'undefined') {
const value = localStorage.getItem(KEY_PREFIX + key);
let saved;
Expand All @@ -23,6 +23,7 @@ const useLocalStorage = (key, defaultValue) => {

useEffect(() => {
localStorage.setItem(KEY_PREFIX + key, JSON.stringify(value));
window.dispatchEvent(new Event('storage'));
}, [key, value]);

return [value, setValue];
Expand Down
22 changes: 19 additions & 3 deletions src/pages/admin/global.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ import { Box, Typography, Divider, List } from '@mui/material';
import dynamic from 'next/dynamic';
import { useEffect, useState } from 'react';
import HeadTag from 'components/HeadTag';
import Navbar from 'components/Navbar';
import ChangeLogoSection from 'components/dashboard/ChangeLogoSection';
import ChangeNavSection from 'components/dashboard/ChangeNavSection';
import { Tab, TabPanel } from 'components/dashboard/Tabs';
import { ConfigProvider, useConfigContext } from 'context/configContext';
import { CustomThemeProvider } from 'context/themeContext';
import { getOrganizationById } from 'models/api';
import { updateLogoUrl } from 'models/config.reducer';
import { wrapper } from 'models/utils';
Expand Down Expand Up @@ -47,7 +49,7 @@ function Global({ organization }) {
}, [mapContext, organization, state.map.initialLocation]);

return (
<>
<CustomThemeProvider>
<HeadTag title="Admin Dashboard" />
<Box
sx={{
Expand Down Expand Up @@ -98,7 +100,21 @@ function Global({ organization }) {
}}
>
<TabPanel value={currentTab} index={0}>
<Typography variant="h5">Navbar View</Typography>
<Typography variant="h4">Navbar Preview</Typography>
<Box
sx={{
display: 'flex',
flexDirection: 'column',
justifyContent: 'center',
paddingY: '50px;',
backgroundColor: 'background.paperDark',
borderRadius: '8px',
marginBottom: '24px',
}}
>
<Navbar preview />
</Box>

<Box
sx={{
display: 'flex',
Expand All @@ -119,7 +135,7 @@ function Global({ organization }) {
</TabPanel>
</Box>
</Box>
</>
</CustomThemeProvider>
);
}

Expand Down