Skip to content

Commit

Permalink
Merge pull request #586 from kbss-cvut/fix/fix-reset-of-fault-event-m…
Browse files Browse the repository at this point in the history
…enu-form-

Fix fault event menu form reset
  • Loading branch information
kostobog authored Sep 11, 2024
2 parents f622bd0 + 0fa866c commit 1655531
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 49 deletions.
3 changes: 2 additions & 1 deletion public/locales/cs/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@
"calculatedFailureRate": "Vypočtená intenzita poruch",
"operationalFailureRate": "Provozní intenzita poruch",
"manuallyDefinedFailureRate": "Manuálně definovaná intenzita poruch",
"unsavedChanges": "Máte neuložené změny. Chcete před pokračováním uložit změny?"
"unsavedChanges": "Máte neuložené změny. Chcete před pokračováním uložit změny?",
"notSelected": "Není vybrána žádná událost"
},
"appBar": {
"selectSystemPlaceholder": "Vyberte systém"
Expand Down
3 changes: 2 additions & 1 deletion public/locales/en/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@
"calculatedFailureRate": "Calculated failure rate",
"operationalFailureRate": "Operational failure rate",
"manuallyDefinedFailureRate": "Manually defined failure rate",
"unsavedChanges": "You have unsaved changes. Do you want to save your changes before proceeding?"
"unsavedChanges": "You have unsaved changes. Do you want to save your changes before proceeding?",
"notSelected": "No Event selected"
},
"appBar": {
"selectSystemPlaceholder": "Select system"
Expand Down
1 change: 1 addition & 0 deletions src/components/dialog/faultEvent/FaultEventCreation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ const FaultEventCreation = ({
name: name,
};
}}
defaultValue={_eventVal ? _eventVal : selectedEvent}
onChangeCallback={handleEventSelect}
onInputChangeCallback={handleFilterOptions}
onCreateEventClick={handleOnCreateEventClick}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -323,11 +323,10 @@ const FaultEventMenu = ({ selectedShapeToolData, onEventUpdated, refreshTree, ro

return (
<Box paddingLeft={2} marginRight={2}>
{shapeToolData && (
<ReusableFaultEventsProvider systemUri={selectedSystem?.iri}>
<FaultEventShapeToolPane data={shapeToolData} refreshTree={refreshTree} formMethods={formMethods} />
</ReusableFaultEventsProvider>
)}
<ReusableFaultEventsProvider systemUri={selectedSystem?.iri}>
<FaultEventShapeToolPane data={shapeToolData} refreshTree={refreshTree} formMethods={formMethods} />
</ReusableFaultEventsProvider>

{/* TODO: Finish for other nodes. Will be refactored. */}

{/* ROOT NODE */}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ import { makeStyles } from "tss-react/mui";

const useStyles = makeStyles()((theme: Theme) => ({
emptyTitle: {
padding: theme.spacing(0, 2),
textAlign: "center",
fontStyle: "italic",
fontWeight: "bold",
color: theme.main.grey,
padding: theme.spacing(10, 2),
},
}));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import useStyles from "@components/editor/faultTree/menu/faultEvent/FaultEventSh
import { ReusableFaultEventsProvider } from "@hooks/useReusableFaultEvents";
import { useCurrentFaultTree } from "@hooks/useCurrentFaultTree";
import { asArray } from "@utils/utils";
import { useTranslation } from "react-i18next";

interface Props {
data?: FaultEvent;
Expand All @@ -18,55 +19,60 @@ interface Props {
const FaultEventShapeToolPane = ({ data, refreshTree, formMethods }: Props) => {
const { classes } = useStyles();
const [faultTree] = useCurrentFaultTree();
const { t } = useTranslation();

let editorPane;
let defaultValues;
const getFormValues = (data) => {
if (data) {
const supertypes = asArray(data.supertypes);
const safeExistingEvent = (supertypes.length ? supertypes : [data]).map((t) => ({
name: t.name,
iri: t.iri,
types: t.types,
}))?.[0];
return {
eventType: data.eventType,
name: data.name,
description: data.description,
probability: data.probability ? data.probability : 0.01,
gateType: data.gateType ? data.gateType : null,
sequenceProbability: data.sequenceProbability,
existingEvent: safeExistingEvent,
};
} else {
return {};
}
};

if (data) {
const safeSupertype = asArray(data.supertypes).map((t) => ({ name: t.name, iri: t.iri, types: t.types }))?.[0];
defaultValues = {
eventType: data.eventType,
name: data.name,
description: data.description,
probability: data.probability ? data.probability : 0.01,
gateType: data.gateType ? data.gateType : null,
sequenceProbability: data.sequenceProbability,
existingEvent: safeSupertype,
};
useEffect(() => {
formMethods.reset(getFormValues(data));
}, [data]);

useEffect(() => {
formMethods.reset(defaultValues);
}, [data]);
const isDisabled =
data &&
([EventType.INTERMEDIATE, EventType.BASIC, EventType.EXTERNAL].includes(data.eventType) || data.isReference);

const isDisabled =
data &&
([EventType.INTERMEDIATE, EventType.BASIC, EventType.EXTERNAL].includes(data.eventType) || data.isReference);
const editorPane = (isDisabled) => (
<ReusableFaultEventsProvider treeUri={faultTree?.iri}>
<FaultEventCreation
useFormMethods={formMethods}
isRootEvent={false}
eventValue={data}
isEditedEvent={true}
isEditMode={true}
disabled={isDisabled}
/>
</ReusableFaultEventsProvider>
);

editorPane = (
<ReusableFaultEventsProvider treeUri={faultTree?.iri}>
<FaultEventCreation
useFormMethods={formMethods}
isRootEvent={false}
eventValue={data}
isEditedEvent={true}
isEditMode={true}
disabled={isDisabled}
/>
</ReusableFaultEventsProvider>
);
} else {
defaultValues = {};
formMethods.reset(defaultValues);
editorPane = (
<Typography className={classes.emptyTitle} variant="subtitle1" align="left">
No Event selected
</Typography>
);
}
const editorPaneNoSelection = () => (
<Typography className={classes.emptyTitle} variant="subtitle1" align="left">
{t("faultEventMenu.notSelected")}
</Typography>
);

return (
<React.Fragment>
{editorPane}
{data ? editorPane(isDisabled) : editorPaneNoSelection()}
{data?.gateType === GateType.PRIORITY_AND && (
<React.Fragment>
<Divider />
Expand Down

0 comments on commit 1655531

Please sign in to comment.