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

Fix/321 global system selection should store object #356

Merged
merged 2 commits into from
Jun 4, 2024
Merged
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
26 changes: 9 additions & 17 deletions src/components/appBar/AppBar.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import * as React from "react";
import { useEffect } from "react";
import useStyles from "@components/appBar/AppBar.styles";
import { AccountCircle } from "@mui/icons-material";
import {
Expand All @@ -23,10 +22,11 @@ import { ROUTES } from "@utils/constants";
import ArrowBackIcon from "@mui/icons-material/ArrowBack";
import { useTranslation } from "react-i18next";
import LanguageIcon from "@mui/icons-material/Language";
import { PRIMARY_LANGUAGE, SECONDARY_LANGUAGE, SELECTED_LANGUAGE_KEY, SELECTED_SYSTEM } from "@utils/constants";
import { PRIMARY_LANGUAGE, SECONDARY_LANGUAGE, SELECTED_LANGUAGE_KEY } from "@utils/constants";
import { useAppBar } from "../../contexts/AppBarContext";
import { useLocation } from "react-router-dom";
import CancelIcon from "@mui/icons-material/Cancel";
import {useSelectedSystem} from "@hooks/useSelectedSystem";

interface Props {
title: string;
Expand All @@ -43,16 +43,11 @@ const AppBar = ({ title, showBackButton = false, topPanelHeight }: Props) => {
const { appBarTitle, systemsList } = useAppBar();

const [anchorEl, setAnchorEl] = React.useState<null | HTMLElement>(null);
const [selectedSystem, setSelectedSystem] = useState<string>(() => sessionStorage.getItem(SELECTED_SYSTEM) || "");
const [selectedSystem, setSelectedSystem] = useSelectedSystem();
const [changePasswordDialogOpen, setChangePasswordDialogOpen] = useState(false);

const isMenuOpen = Boolean(anchorEl);

useEffect(() => {
const currentItem = sessionStorage.getItem(SELECTED_SYSTEM);
if (selectedSystem !== currentItem) setSelectedSystem(currentItem);
}, [location.pathname]);

const handleProfileMenuOpen = (event: React.MouseEvent<HTMLElement>) => {
setAnchorEl(event.currentTarget);
};
Expand Down Expand Up @@ -109,17 +104,14 @@ const AppBar = ({ title, showBackButton = false, topPanelHeight }: Props) => {
}
};

const handleSystemChange = (event: React.ChangeEvent<{ value: unknown }>) => {
const handleSystemChange = (event : React.ChangeEvent<{ value: unknown }>) => {
const value = event.target.value as string;
setSelectedSystem(value);
sessionStorage.setItem(SELECTED_SYSTEM, value);
window.dispatchEvent(new Event("storage"));
const selectedSystem = value ? systemsList.find((s) => s.iri == value) : null;
setSelectedSystem(selectedSystem);
};

const handleSystemDelete = () => {
setSelectedSystem("");
sessionStorage.setItem(SELECTED_SYSTEM, "");
window.dispatchEvent(new Event("storage"));
setSelectedSystem(null);
};

return (
Expand Down Expand Up @@ -147,12 +139,12 @@ const AppBar = ({ title, showBackButton = false, topPanelHeight }: Props) => {
select
InputLabelProps={{ shrink: false }}
className={classes.textfieldSelect}
value={selectedSystem || ""}
value={selectedSystem ? selectedSystem.iri : ""}
onChange={handleSystemChange}
>
{systemsList.map((s, i) => {
return (
<MenuItem key={`${s.name}-${i}`} value={s.name}>
<MenuItem key={`${s.iri}`} value={s.iri}>
{s.name}
</MenuItem>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import { useTranslation } from "react-i18next";
import useStyles from "./FaultTreeOverviewTable.styles";
import { FaultTree } from "@models/faultTreeModel";
import { useNavigate } from "react-router-dom";
import { ROUTES, SELECTED_SYSTEM } from "@utils/constants";
import {ROUTES} from "@utils/constants";
import { extractFragment } from "@services/utils/uriIdentifierUtils";
import { System } from "@models/systemModel";
import { getModifiedSystemsList, getModifiedFaultTreesList, formatDate } from "@utils/utils";
import {useSelectedSystem} from "@hooks/useSelectedSystem";

const faultTreeTableHeadCells = [
"faultTreeOverviewTable.name",
Expand Down Expand Up @@ -45,13 +46,12 @@ const FaultTreeAndSystemOverviewTable: FC<FaultTreeOverviewTableProps> = ({
const { t } = useTranslation();
const theme = useTheme();

const [, setSelectedSystem] = useSelectedSystem();
const modifiedSystemsList = getModifiedSystemsList(systems, selectedSystem);
const modifiedFaultTreesList = getModifiedFaultTreesList(faultTrees, selectedSystem);

const redirectToPath = (routePath: string, systemName?: string) => {
if (systemName) {
sessionStorage.setItem(SELECTED_SYSTEM, systemName);
}
const redirectToPath = (routePath: string, system ) => {
setSelectedSystem(system);
navigate(routePath);
};

Expand Down Expand Up @@ -103,7 +103,7 @@ const FaultTreeAndSystemOverviewTable: FC<FaultTreeOverviewTableProps> = ({
<Button
variant="contained"
className={classes.editButton}
onClick={() => redirectToPath(routePath, faultTree?.system?.name)}
onClick={() => redirectToPath(routePath, faultTree?.system)}
>
{t("faultTreeOverviewTable.edit")}
</Button>
Expand All @@ -128,7 +128,7 @@ const FaultTreeAndSystemOverviewTable: FC<FaultTreeOverviewTableProps> = ({
<Button
variant="contained"
className={classes.editButton}
onClick={() => redirectToPath(routePath)}
onClick={() => redirectToPath(routePath, system)}
>
{t("faultTreeOverviewTable.edit")}
</Button>
Expand Down
22 changes: 5 additions & 17 deletions src/components/dashboard/content/list/FaultTreeOverview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import FaultTreeEditDialog from "@components/dialog/faultTree/FaultTreeEditDialo
import FaultTreeDialog from "@components/dialog/faultTree/FaultTreeDialog";
import { Box, Button } from "@mui/material";
import { useTranslation } from "react-i18next";
import { SELECTED_SYSTEM, SELECTED_VIEW } from "@utils/constants";
import { FaultEventsReuseProvider } from "@hooks/useReusableFaultEvents";
import { SELECTED_VIEW } from "@utils/constants";
import {useSelectedSystem} from "@hooks/useSelectedSystem";

const FaultTreeOverview = () => {
const { t } = useTranslation();
Expand All @@ -25,25 +25,13 @@ const FaultTreeOverview = () => {
const [contextMenuSelectedTree, setContextMenuSelectedTree] = useState<FaultTree>(null);
const [contextMenuAnchor, setContextMenuAnchor] = useState<ElementContextMenuAnchor>(contextMenuDefaultAnchor);
const [createFaultTreeDialogOpen, setCreateFaultTreeDialogOpen] = useState<boolean>(false);
const [selectedSystem, setSelectedSystem] = useState<string | null>(sessionStorage.getItem(SELECTED_SYSTEM));
const [selectedSystem] = useSelectedSystem();

useEffect(() => {
const storedView = localStorage.getItem(SELECTED_VIEW) as ViewType;
if (storedView) {
setSelectedView(storedView);
}
const handleStorageChange = () => {
const system = sessionStorage.getItem(SELECTED_SYSTEM);
if (system) {
setSelectedSystem(system);
}
};

window.addEventListener("storage", handleStorageChange);

return () => {
window.removeEventListener("storage", handleStorageChange);
};
}, []);

const toggleView = (viewType: ViewType) => {
Expand Down Expand Up @@ -83,13 +71,13 @@ const FaultTreeOverview = () => {

{selectedView === "table" ? (
<FaultTreeAndSystemOverviewTable
selectedSystem={selectedSystem}
selectedSystem={selectedSystem?.iri}
faultTrees={faultTrees}
handleFaultTreeContextMenu={handleContextMenu}
/>
) : (
<FaultTreeAndSystemOverviewCardsList
selectedSystem={selectedSystem}
selectedSystem={selectedSystem?.iri}
faultTrees={faultTrees}
handleFaultTreeContextMenu={handleContextMenu}
/>
Expand Down
20 changes: 4 additions & 16 deletions src/components/dashboard/content/list/SystemOverview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import FaultTreeAndSystemOverviewCardsList from "./FaultTreeAndSystemOverviewCar
import { useTranslation } from "react-i18next";
import { Button, Box } from "@mui/material";
import SystemDialog from "@components/dialog/system/SystemDialog";
import { SELECTED_SYSTEM } from "@utils/constants";
import {useSelectedSystem} from "@hooks/useSelectedSystem";

const SystemOverview = () => {
const { t } = useTranslation();
Expand All @@ -25,25 +25,13 @@ const SystemOverview = () => {
const [contextMenuAnchor, setContextMenuAnchor] = useState<ElementContextMenuAnchor>(contextMenuDefaultAnchor);
const [editDialogOpen, setEditDialogOpen] = useState(false);
const [createSystemDialogOpen, setCreateSystemDialogOpen] = useState<boolean>(false);
const [selectedSystem, setSelectedSystem] = useState<string | null>(sessionStorage.getItem(SELECTED_SYSTEM));
const [selectedSystem, setSelectedSystem] = useSelectedSystem();

useEffect(() => {
const storedView = localStorage.getItem("selectedView") as ViewType;
if (storedView) {
setSelectedView(storedView);
}
const handleStorageChange = () => {
const system = sessionStorage.getItem(SELECTED_SYSTEM);
if (system) {
setSelectedSystem(system);
}
};

window.addEventListener("storage", handleStorageChange);

return () => {
window.removeEventListener("storage", handleStorageChange);
};
}, []);

const toggleView = (viewType: ViewType) => {
Expand Down Expand Up @@ -83,13 +71,13 @@ const SystemOverview = () => {

{selectedView === "table" ? (
<FaultTreeAndSystemOverviewTable
selectedSystem={selectedSystem}
selectedSystem={selectedSystem?.iri}
systems={systems}
handleSystemContextMenu={handleContextMenu}
/>
) : (
<FaultTreeAndSystemOverviewCardsList
selectedSystem={selectedSystem}
selectedSystem={selectedSystem?.iri}
systems={systems}
handleSystemContextMenu={handleContextMenu}
/>
Expand Down
10 changes: 3 additions & 7 deletions src/components/dialog/faultTree/FaultTreeDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,20 @@ import { yupResolver } from "@hookform/resolvers/yup";
import { rootEventSchema } from "@components/dialog/faultEvent/FaultEventCreation.schema";
import { FaultEventsReuseProvider } from "@hooks/useReusableFaultEvents";
import { useTranslation } from "react-i18next";
import { SELECTED_SYSTEM } from "@utils/constants";
import useStyles from "@components/dialog/faultTree/FaultTreeDialog.styles";
import {useSelectedSystem} from "@hooks/useSelectedSystem";

const FaultTreeDialog = ({ open, handleCloseDialog }) => {
const { t } = useTranslation();
const { classes } = useStyles();

const [, addFaultTree] = useFaultTrees();
const [processing, setIsProcessing] = useState(false);
const [systemName, setSystemName] = useState(sessionStorage.getItem(SELECTED_SYSTEM));
const [selectedSystem] = useSelectedSystem();

const useFormMethods = useForm({ resolver: yupResolver(schema.concat(rootEventSchema)) });
const { handleSubmit, register } = useFormMethods;

useEffect(() => {
setSystemName(sessionStorage.getItem(SELECTED_SYSTEM));
}, [sessionStorage.getItem(SELECTED_SYSTEM)]);

const handleCreateFaultTree = async (values: any) => {
setIsProcessing(true);

Expand All @@ -57,7 +53,7 @@ const FaultTreeDialog = ({ open, handleCloseDialog }) => {
<DialogContent dividers>
<TextField
className={classes.readOnly}
value={systemName}
value={selectedSystem?.name}
aria-readonly={true}
margin="dense"
label={t("newFtaModal.namePlaceholder")}
Expand Down
6 changes: 5 additions & 1 deletion src/hooks/DashboardContentProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@ import { ChildrenProps } from "../utils/hookUtils";
import { FaultTreesProvider } from "./useFaultTrees";
import { FailureModesTablesProvider } from "./useFailureModesTables";
import { SystemsProvider } from "@hooks/useSystems";
import {SelectedSystemProvider} from "@hooks/useSelectedSystem";

const DashboardContentProvider = ({ children }: ChildrenProps) => {
return (

<FaultTreesProvider>
<SystemsProvider>
<FailureModesTablesProvider>{children}</FailureModesTablesProvider>
<SelectedSystemProvider>
<FailureModesTablesProvider>{children}</FailureModesTablesProvider>
</SelectedSystemProvider>
</SystemsProvider>
</FaultTreesProvider>
);
Expand Down
36 changes: 36 additions & 0 deletions src/hooks/useSelectedSystem.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import * as React from "react";
import { createContext, useContext, useState } from "react";
import {SELECTED_SYSTEM} from "@utils/constants";
import { ChildrenProps } from "@utils/hookUtils";
import {System} from "../models/systemModel";

type systemContextType = [System, (system: System) => void];

export const selectedSystemContext = createContext<systemContextType>(null!);

export const useSelectedSystem = () => {
const [selectedSystem, setSelectedSystem] = useContext(selectedSystemContext);
return [selectedSystem, setSelectedSystem] as const;
};

export const getSelectedSystem = (): System => {
const item = sessionStorage.getItem(SELECTED_SYSTEM);

if (!item)
return null;

return JSON.parse(item);
};

export const SelectedSystemProvider = ({ children }: ChildrenProps) => {
const [_system, _setSelectedSystem] = useState<System>(getSelectedSystem());

const setSelectedSystem = async (system: System) => {
sessionStorage.removeItem(SELECTED_SYSTEM);
if(system)
sessionStorage.setItem(SELECTED_SYSTEM, JSON.stringify(system));
_setSelectedSystem(system);
};

return <selectedSystemContext.Provider value={[_system, setSelectedSystem]}>{children}</selectedSystemContext.Provider>;
};
Loading