Skip to content

Commit

Permalink
frontend Detail Drawer: Add detail drawer mode
Browse files Browse the repository at this point in the history
Signed-off-by: Vincent T <[email protected]>
  • Loading branch information
vyncent-t committed Nov 29, 2023
1 parent fe7f2ed commit b49bdef
Show file tree
Hide file tree
Showing 13 changed files with 230 additions and 5 deletions.
12 changes: 12 additions & 0 deletions frontend/src/components/App/Layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { useTypedSelector } from '../../redux/reducers/reducers';
import store from '../../redux/stores/store';
import ActionsNotifier from '../common/ActionsNotifier';
import AlertNotification from '../common/AlertNotification';
import DetailDrawer from '../common/DetailDrawer/DetailDrawer';
import Sidebar, { NavigationTabs } from '../Sidebar';
import RouteSwitcher from './RouteSwitcher';
import TopBar from './TopBar';
Expand Down Expand Up @@ -93,6 +94,16 @@ export default function Layout({}: LayoutProps) {
const { t } = useTranslation();
const clusterInURL = getCluster();

// DETAIL DRAWER MODE
const isDetailDrawerEnabled = localStorage.getItem('detailDrawerEnabled')
? Boolean(localStorage.getItem('detailDrawerEnabled'))
: useTypedSelector(state => state.drawerMode.isDetailDrawerEnabled);
console.log('A - isDetailDrawerEnabled', isDetailDrawerEnabled);
const isDetailDrawerOpen = localStorage.getItem('detailDrawerOpen')
? Boolean(localStorage.getItem('detailDrawerOpen'))
: useTypedSelector(state => state.drawerMode.isDetailDrawerOpen);
console.log('A - isDetailDrawerOpen', isDetailDrawerOpen);

useEffect(() => {
window.clusterConfigFetchHandler = setInterval(
() => {
Expand Down Expand Up @@ -175,6 +186,7 @@ export default function Layout({}: LayoutProps) {
<CssBaseline />
<TopBar />
<Sidebar />
{isDetailDrawerEnabled && <DetailDrawer open={isDetailDrawerOpen} />}
<main id="main" className={classes.content}>
{clusters && !!clusterInURL && !Object.keys(clusters).includes(getCluster() || '') ? (
<ClusterNotFoundPopup />
Expand Down
55 changes: 55 additions & 0 deletions frontend/src/components/App/Settings/DrawerModeButton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { FormControlLabel, Switch } from '@material-ui/core';
import React from 'react';
import { useEffect, useState } from 'react';
import { useTranslation } from 'react-i18next';
import { useDispatch } from 'react-redux';
import { setDetailDrawerEnabled } from '../../../redux/drawerModeSlice';
import { useTypedSelector } from '../../../redux/reducers/reducers';

export default function DrawerModeButton() {
// This will fix the problem of the project refreshing the state away from needed position

// we initate a const that sets the state for the drawerMode, it will read whatever state is on and then set it to local storage through the setDetailDrawerEnabled function later
const isDetailDrawerEnabled = useTypedSelector(state => state.drawerMode.isDetailDrawerEnabled);

// localStorage.getItem('detailDrawerEnabled') will return a string, so we need to convert it to a boolean
// ex. localStorage.getItem('detailDrawerEnabled') = 'true' or 'false'
const localDetailDrawerEnabled = Boolean(localStorage.getItem('detailDrawerEnabled'));

// we need to create a state for the current changes to the drawerMode, so we create a new state called newDrawerEnabled and set it to the localDetailDrawerEnabled
// we use newDrawerEnabled to take in a change of state, send it to the setDetailDrawerEnabled function which the function then sets sets the state to the new change and ALSO sets it to local storage

const [newDrawerEnabled, changeDetailDrawerEnabled] = useState(localDetailDrawerEnabled);

const dispatch = useDispatch();
const { t } = useTranslation('translation');

// the useEffect will run everytime the newDrawerEnabled state changes, which is everytime the user clicks the switch button because the switch button changes the state of newDrawerEnabled
useEffect(() => {
dispatch(setDetailDrawerEnabled(newDrawerEnabled));
console.log('ON SETTINGS');
console.log(localStorage.getItem('detailDrawerEnabled'));
}, [newDrawerEnabled]);

// this function takes in the current changes and updates it, this kicks off the useEffect that is listening for changes to newDrawerEnabled
function drawerModeToggle() {
console.log('drawerModeToggle');
changeDetailDrawerEnabled(!newDrawerEnabled);
}

// NOTICE THAT WE DO NOT USE NewDrawerEnabled TO DETERMINE HOW THE SWITCH IS RENDERED UNDER THE CHECKED PROP, THIS IS BECAUSE THE USEEFFECT WILL RERENDER THE COMPONENT WITH THE NEW STATE
return (
<FormControlLabel
control={
<Switch
checked={isDetailDrawerEnabled}
onClick={() => drawerModeToggle()}
name="drawerMode"
color="primary"
/>
}
// will need to replace label
label={t('translation|Drawer Mode')}
/>
);
}
5 changes: 5 additions & 0 deletions frontend/src/components/App/Settings/Settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { setAppSettings } from '../../../redux/configSlice';
import { defaultTableRowsPerPageOptions } from '../../../redux/configSlice';
import { ActionButton, NameValueTable, SectionBox } from '../../common';
import TimezoneSelect from '../../common/TimezoneSelect';
import DrawerModeButton from './DrawerModeButton';
import { useSettings } from './hook';
import NumRowsInput from './NumRowsInput';
import ThemeChangeButton from './ThemeChangeButton';
Expand Down Expand Up @@ -69,6 +70,10 @@ export default function Settings() {
name: t('translation|Theme'),
value: <ThemeChangeButton showBothIcons />,
},
{
name: t('translation|Drawer Mode'),
value: <DrawerModeButton />,
},
{
name: t('translation|Number of rows for tables'),
value: (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,52 @@ exports[`Storyshots Settings General 1`] = `
</button>
</div>
</dd>
<dt
class="MuiGrid-root MuiGrid-item MuiGrid-grid-xs-12 MuiGrid-grid-sm-4 makeStyles-metadataNameCell css-1r6indk-MuiGrid-root"
>
Drawer Mode
</dt>
<dd
class="MuiGrid-root MuiGrid-item MuiGrid-grid-xs-12 MuiGrid-grid-sm-8 makeStyles-metadataCell makeStyles-valueCol css-r0umuq-MuiGrid-root"
>
<label
class="MuiFormControlLabel-root"
>
<span
class="MuiSwitch-root"
>
<span
aria-disabled="false"
class="MuiButtonBase-root MuiIconButton-root PrivateSwitchBase-root-1 MuiSwitch-switchBase MuiSwitch-colorPrimary"
>
<span
class="MuiIconButton-label"
>
<input
class="PrivateSwitchBase-input-4 MuiSwitch-input"
name="drawerMode"
type="checkbox"
value=""
/>
<span
class="MuiSwitch-thumb"
/>
</span>
<span
class="MuiTouchRipple-root"
/>
</span>
<span
class="MuiSwitch-track"
/>
</span>
<span
class="MuiTypography-root MuiFormControlLabel-label MuiTypography-body1"
>
Drawer Mode
</span>
</label>
</dd>
<dt
class="MuiGrid-root MuiGrid-item MuiGrid-grid-xs-12 MuiGrid-grid-sm-4 makeStyles-metadataNameCell css-1r6indk-MuiGrid-root"
>
Expand Down
71 changes: 71 additions & 0 deletions frontend/src/components/common/DetailDrawer/DetailDrawer.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { Button } from '@material-ui/core';
import Box from '@material-ui/core/Box';
import Drawer from '@material-ui/core/Drawer';
import React from 'react';
import { useState } from 'react';
import { useEffect } from 'react';
import { useDispatch } from 'react-redux';
import { setDetailDrawerOpen } from '../../../redux/drawerModeSlice';
import { useTypedSelector } from '../../../redux/reducers/reducers';

export interface DetailDrawerProps {
open: boolean;
onClose?: () => void;
children?: React.ReactNode;
}

export default function DetailDrawer({ open, onClose, children }: DetailDrawerProps) {
// localDetailDrawerEnabled is a string of either 'true' or 'false', if it does not exist yet it needs to be created
const localDetailDrawerEnabled = Boolean(localStorage.getItem('detailDrawerEnabled'));
console.log('LOCAL B OFF SETTINGS BEFORE localDetailDrawerEnabled', localDetailDrawerEnabled);

if (!localDetailDrawerEnabled) {
useTypedSelector(state => state.drawerMode.isDetailDrawerEnabled);
}
console.log('LOCAL B OFF SETTINGS localDetailDrawerEnabled', localDetailDrawerEnabled);

// const isDetailDrawerOpen = localStorage.getItem('detialDrawerOpen') ? Boolean(localStorage.getItem('detailDrawerOpen')) : useTypedSelector(state => state.drawerMode.isDetailDrawerOpen);
const isDetailDrawerOpen = open;
console.log('OFF SETTINGS isDetailDrawerOpen', isDetailDrawerOpen);
// localDetailDrawerOpen is a string of either 'true' or 'false', if it does not exist yet it needs to be created
// const localDetailDrawerOpen = Boolean(localStorage.getItem('detailDrawerOpen'))

const [openDetailDrawer, changeOpenDetailDrawer] = useState(isDetailDrawerOpen);
console.log('openDetailDrawer', openDetailDrawer);
const dispatch = useDispatch();

useEffect(() => {
console.log('Toggle Open Drawer', openDetailDrawer);
dispatch(setDetailDrawerOpen(openDetailDrawer));
}, [openDetailDrawer]);

function toggleOpenDrawer() {
changeOpenDetailDrawer(!openDetailDrawer);
}

return (
<>
{!openDetailDrawer && (
<>
<Drawer anchor="right" open onClose={onClose}>
<Box width={200} p={2}>
<Button onClick={() => toggleOpenDrawer()}>Open</Button>
</Box>
</Drawer>
</>
)}
{openDetailDrawer && (
<>
<Drawer anchor="right" open onClose={() => toggleOpenDrawer()}>
<Box width={600} p={2}>
<Button onClick={() => toggleOpenDrawer()}>Close</Button>
{children}
</Box>
</Drawer>
</>
)}
</>
);
}

// * the drawer is not opening in minimized mode? persistent drawer fix maybe - https://mui.com/material-ui/react-drawer/#persistent-drawer
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ exports[`Storyshots ActionsNotifier None 1`] = `<div />`;
exports[`Storyshots ActionsNotifier Some 1`] = `
<div>
<div
class="makeStyles-bottom-4 makeStyles-left-5 makeStyles-root-1"
class="makeStyles-bottom-8 makeStyles-left-9 makeStyles-root-5"
>
<div
class="MuiCollapse-root MuiCollapse-entered"
Expand All @@ -18,22 +18,22 @@ exports[`Storyshots ActionsNotifier Some 1`] = `
class="MuiCollapse-wrapperInner"
>
<div
class="SnackbarItem-root-8 SnackbarItem-wrappedRoot-23 SnackbarItem-anchorOriginBottomLeft-14"
class="SnackbarItem-root-12 SnackbarItem-wrappedRoot-27 SnackbarItem-anchorOriginBottomLeft-18"
>
<div
aria-describedby="notistack-snackbar"
class="ForwardRef-root-24 SnackbarItem-contentRoot-15"
class="ForwardRef-root-28 SnackbarItem-contentRoot-19"
role="alert"
style="webkit-transform: none; transform: none; webkit-transition: -webkit-transform 225ms cubic-bezier(0.0, 0, 0.2, 1) 0ms; transition: transform 225ms cubic-bezier(0.0, 0, 0.2, 1) 0ms;"
>
<div
class="SnackbarItem-message-21"
class="SnackbarItem-message-25"
id="notistack-snackbar"
>
Some message
</div>
<div
class="SnackbarItem-action-22"
class="SnackbarItem-action-26"
>
<button
class="MuiButtonBase-root MuiButton-root MuiButton-text MuiButton-textSecondary MuiButton-sizeSmall MuiButton-textSizeSmall MuiButton-root MuiButton-text MuiButton-textSecondary MuiButton-sizeSmall MuiButton-textSizeSmall css-u3zvl7-MuiButtonBase-root-MuiButton-root"
Expand Down
1 change: 1 addition & 0 deletions frontend/src/i18n/locales/de/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"Visible": "Sichtbar",
"Plugins": "Plugins",
"Save & Apply": "Speichern & Anwenden",
"Drawer Mode": "",
"Enter a value between {{ minRows }} and {{ maxRows }}.": "Geben Sie einen Wert zwischen {{ minRows }} und {{ maxRows }} ein.",
"Custom row value": "Benutzerdefinierter Zeilenwert",
"Apply": "Anwenden",
Expand Down
1 change: 1 addition & 0 deletions frontend/src/i18n/locales/en/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"Visible": "Visible",
"Plugins": "Plugins",
"Save & Apply": "Save & Apply",
"Drawer Mode": "Drawer Mode",
"Enter a value between {{ minRows }} and {{ maxRows }}.": "Enter a value between {{ minRows }} and {{ maxRows }}.",
"Custom row value": "Custom row value",
"Apply": "Apply",
Expand Down
1 change: 1 addition & 0 deletions frontend/src/i18n/locales/es/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"Visible": "Visible",
"Plugins": "Plugins",
"Save & Apply": "Guardar & Aplicar",
"Drawer Mode": "",
"Enter a value between {{ minRows }} and {{ maxRows }}.": "Introduzca un valor entre {{ minRows }} y {{ maxRows }}.",
"Custom row value": "Núm. de líneas personalizado",
"Apply": "Aplicar",
Expand Down
1 change: 1 addition & 0 deletions frontend/src/i18n/locales/fr/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"Visible": "Visible",
"Plugins": "Plugins",
"Save & Apply": "Sauvegarder et appliquer",
"Drawer Mode": "",
"Enter a value between {{ minRows }} and {{ maxRows }}.": "Entrez une valeur entre {{ minRows }} et {{ maxRows }}.",
"Custom row value": "Valeur de ligne personnalisée",
"Apply": "Appliquer",
Expand Down
1 change: 1 addition & 0 deletions frontend/src/i18n/locales/pt/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"Visible": "Visível",
"Plugins": "Plugins",
"Save & Apply": "Guardar & Aplicar",
"Drawer Mode": "",
"Enter a value between {{ minRows }} and {{ maxRows }}.": "Introduza um valor entre {{ minRows }} e {{ maxRows }}.",
"Custom row value": "Núm. de linhas personalizado",
"Apply": "Aplicar",
Expand Down
29 changes: 29 additions & 0 deletions frontend/src/redux/drawerModeSlice.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { createSlice, PayloadAction } from '@reduxjs/toolkit';

interface DrawerModeState {
isDetailDrawerEnabled: boolean;
isDetailDrawerOpen: boolean;
}

const initialState: DrawerModeState = {
isDetailDrawerEnabled: false,
isDetailDrawerOpen: false,
};

const drawerModeSlice = createSlice({
name: 'drawerMode',
initialState,
reducers: {
setDetailDrawerEnabled: (state, action: PayloadAction<boolean>) => {
state.isDetailDrawerEnabled = action.payload;
localStorage.setItem('detailDrawerEnabled', `${action.payload}`);
},
setDetailDrawerOpen: (state, action: PayloadAction<boolean>) => {
state.isDetailDrawerOpen = action.payload;
localStorage.setItem('detailDrawerOpen', `${action.payload}`);
},
},
});

export const { setDetailDrawerEnabled, setDetailDrawerOpen } = drawerModeSlice.actions;
export default drawerModeSlice.reducer;
2 changes: 2 additions & 0 deletions frontend/src/redux/reducers/reducers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import pluginsReducer from '../../plugin/pluginsSlice';
import actionButtons from '../actionButtonsSlice';
import clusterAction from '../clusterActionSlice';
import configReducer from '../configSlice';
import drawerModeSlice from '../drawerModeSlice';
import filterReducer from '../filterSlice';
import routesReducer from '../routesSlice';
import resourceTableReducer from './../../components/common/Resource/resourceTableSlice';
Expand All @@ -26,6 +27,7 @@ const reducers = combineReducers({
detailsViewSection: detailsViewSectionReducer,
routes: routesReducer,
sidebar: sidebarReducer,
drawerMode: drawerModeSlice,
});

export type RootState = ReturnType<typeof reducers>;
Expand Down

0 comments on commit b49bdef

Please sign in to comment.