From 4010bdb7bf13318d8743f054903816644b97f66a Mon Sep 17 00:00:00 2001 From: Bogdan Kostov Date: Tue, 4 Jun 2024 11:48:07 +0200 Subject: [PATCH 1/4] [Fix #354] Filter reusable fault events based on selected global system and fault tree --- .../dialog/faultEvent/FaultEventDialog.tsx | 4 +++- .../dialog/faultTree/FaultTreeDialog.tsx | 5 ++-- src/hooks/useReusableFaultEvents.tsx | 8 ++++--- src/services/faultTreeService.tsx | 23 +++++++++++++++---- 4 files changed, 30 insertions(+), 10 deletions(-) diff --git a/src/components/dialog/faultEvent/FaultEventDialog.tsx b/src/components/dialog/faultEvent/FaultEventDialog.tsx index 096b90ce..3ecfc45d 100644 --- a/src/components/dialog/faultEvent/FaultEventDialog.tsx +++ b/src/components/dialog/faultEvent/FaultEventDialog.tsx @@ -13,6 +13,7 @@ import { yupResolver } from "@hookform/resolvers/yup"; import { eventFromHookFormValues } from "@services/faultEventService"; import { FaultEventsReuseProvider } from "@hooks/useReusableFaultEvents"; import { FaultEvent } from "@models/eventModel"; +import {useSelectedSystem} from "@hooks/useSelectedSystem"; interface Props { open: boolean; @@ -25,6 +26,7 @@ interface Props { const FaultEventDialog = ({ open, eventIri, treeUri, onCreated, onClose }: Props) => { const [showSnackbar] = useSnackbar(); + const [selectedSystem] = useSelectedSystem(); const useFormMethods = useForm({ resolver: yupResolver(schema) }); const { handleSubmit, formState } = useFormMethods; const { isSubmitting } = formState; @@ -48,7 +50,7 @@ const FaultEventDialog = ({ open, eventIri, treeUri, onCreated, onClose }: Props Create Event - + diff --git a/src/components/dialog/faultTree/FaultTreeDialog.tsx b/src/components/dialog/faultTree/FaultTreeDialog.tsx index 3dfc26b8..84e81653 100644 --- a/src/components/dialog/faultTree/FaultTreeDialog.tsx +++ b/src/components/dialog/faultTree/FaultTreeDialog.tsx @@ -35,7 +35,8 @@ const FaultTreeDialog = ({ open, handleCloseDialog }) => { // TODO: Add full system from System Context Provider const faultTree = { - name: values.faultTreeName, + name: rootEvent.name, + system: selectedSystem, manifestingEvent: rootEvent, } as FaultTree; @@ -64,7 +65,7 @@ const FaultTreeDialog = ({ open, handleCloseDialog }) => { {...register("faultTreeName")} helperText={!!useFormMethods.formState.errors.faultTreeName && t("newFtaModal.noSystemError")} /> - + diff --git a/src/hooks/useReusableFaultEvents.tsx b/src/hooks/useReusableFaultEvents.tsx index 51f73bbb..719dbbe0 100644 --- a/src/hooks/useReusableFaultEvents.tsx +++ b/src/hooks/useReusableFaultEvents.tsx @@ -18,16 +18,18 @@ export const useReusableFaultEvents = () => { interface Props extends ChildrenProps { treeUri?: string; + systemUri?: string; } -export const FaultEventsReuseProvider = ({ children, treeUri }: Props) => { +export const FaultEventsReuseProvider = ({ children, treeUri, systemUri }: Props) => { const [_faultEvents, _setFaultEvents] = useState([]); const [showSnackbar] = useSnackbar(); useEffect(() => { const fetchFaultEvents = async () => { - const eventsPromise = treeUri ? faultTreeService.getReusableEvents(treeUri) : faultEventService.findAll(); - + const eventsPromise = treeUri + ? faultTreeService.getAllReusableEvents(systemUri, treeUri) + : faultTreeService.getRootReusableEvents(systemUri); eventsPromise.then((value) => _setFaultEvents(value)).catch((reason) => showSnackbar(reason, SnackbarType.ERROR)); }; diff --git a/src/services/faultTreeService.tsx b/src/services/faultTreeService.tsx index 74aa101e..6f2f1dc5 100644 --- a/src/services/faultTreeService.tsx +++ b/src/services/faultTreeService.tsx @@ -88,16 +88,31 @@ export const remove = async (faultTreeIri: string): Promise => { } }; -export const getReusableEvents = async (faultTreeIri: string): Promise => { +export const getRootReusableEvents = async (systemIri: string): Promise => { try { - const fragment = extractFragment(faultTreeIri); - const response = await axiosClient.get(`/faultTrees/${fragment}/reusableEvents`, { + const fragment = extractFragment(systemIri); + const response = await axiosClient.get(`/faultEvents/top-fault-events/${fragment}`, { + headers: authHeaders(), + }); + + return JsonLdUtils.compactAndResolveReferencesAsArray(response.data, EVENT_CONTEXT); + } catch (e) { + console.log("Fault Tree Service - Failed to call /getRootReusableEvents"); + const defaultMessage = "Failed to find reusable fault events"; + return new Promise((resolve, reject) => reject(handleServerError(e, defaultMessage))); + } +}; + +export const getAllReusableEvents = async (systemIri: string, faultTreeIri: string): Promise => { + try { + const fragment = extractFragment(systemIri); + const response = await axiosClient.get(`/faultEvents/all-fault-events/${fragment}`, { headers: authHeaders(), }); return JsonLdUtils.compactAndResolveReferencesAsArray(response.data, EVENT_CONTEXT); } catch (e) { - console.log("Fault Tree Service - Failed to call /getReusableEvents"); + console.log("Fault Tree Service - Failed to call /getAllReusableEvents"); const defaultMessage = "Failed to find reusable fault events"; return new Promise((resolve, reject) => reject(handleServerError(e, defaultMessage))); } From 7ea6cade3ab26e46f4c0126456deffde371bbcb2 Mon Sep 17 00:00:00 2001 From: Bogdan Kostov Date: Tue, 4 Jun 2024 12:11:10 +0200 Subject: [PATCH 2/4] [Fix #354] Don't fetch reusable fault events if system is not selected --- src/hooks/useReusableFaultEvents.tsx | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/hooks/useReusableFaultEvents.tsx b/src/hooks/useReusableFaultEvents.tsx index 719dbbe0..5bdeb900 100644 --- a/src/hooks/useReusableFaultEvents.tsx +++ b/src/hooks/useReusableFaultEvents.tsx @@ -27,6 +27,9 @@ export const FaultEventsReuseProvider = ({ children, treeUri, systemUri }: Props useEffect(() => { const fetchFaultEvents = async () => { + if(!systemUri) + return; + const eventsPromise = treeUri ? faultTreeService.getAllReusableEvents(systemUri, treeUri) : faultTreeService.getRootReusableEvents(systemUri); From 2357246addec62eca938a46ec1cc17ef7f18a773 Mon Sep 17 00:00:00 2001 From: Bogdan Kostov Date: Tue, 4 Jun 2024 16:08:53 +0200 Subject: [PATCH 3/4] Rename FaultEventsReuseProvider to ReusableFaultEventsProvider --- src/components/dashboard/content/list/FaultTreeOverview.tsx | 5 +++-- src/components/dialog/faultEvent/FaultEventDialog.tsx | 6 +++--- src/components/dialog/faultTree/FaultTreeDialog.tsx | 6 +++--- .../faultTree/menu/faultEvent/FaultEventShapeToolPane.tsx | 6 +++--- src/hooks/useReusableFaultEvents.tsx | 4 ++-- 5 files changed, 14 insertions(+), 13 deletions(-) diff --git a/src/components/dashboard/content/list/FaultTreeOverview.tsx b/src/components/dashboard/content/list/FaultTreeOverview.tsx index 0d23b260..fd5d7552 100644 --- a/src/components/dashboard/content/list/FaultTreeOverview.tsx +++ b/src/components/dashboard/content/list/FaultTreeOverview.tsx @@ -14,6 +14,7 @@ import { Box, Button } from "@mui/material"; import { useTranslation } from "react-i18next"; import { SELECTED_VIEW } from "@utils/constants"; import {useSelectedSystem} from "@hooks/useSelectedSystem"; +import {ReusableFaultEventsProvider} from "@hooks/useReusableFaultEvents"; const FaultTreeOverview = () => { const { t } = useTranslation(); @@ -94,12 +95,12 @@ const FaultTreeOverview = () => { handleCloseDialog={() => setEditDialogOpen(false)} faultTree={contextMenuSelectedTree} /> - + setCreateFaultTreeDialogOpen(false)} /> - + ); }; diff --git a/src/components/dialog/faultEvent/FaultEventDialog.tsx b/src/components/dialog/faultEvent/FaultEventDialog.tsx index 3ecfc45d..765a7f29 100644 --- a/src/components/dialog/faultEvent/FaultEventDialog.tsx +++ b/src/components/dialog/faultEvent/FaultEventDialog.tsx @@ -11,7 +11,7 @@ import { useForm } from "react-hook-form"; import { schema } from "./FaultEventCreation.schema"; import { yupResolver } from "@hookform/resolvers/yup"; import { eventFromHookFormValues } from "@services/faultEventService"; -import { FaultEventsReuseProvider } from "@hooks/useReusableFaultEvents"; +import { ReusableFaultEventsProvider } from "@hooks/useReusableFaultEvents"; import { FaultEvent } from "@models/eventModel"; import {useSelectedSystem} from "@hooks/useSelectedSystem"; @@ -50,9 +50,9 @@ const FaultEventDialog = ({ open, eventIri, treeUri, onCreated, onClose }: Props Create Event - + - +