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/354 filter reusable events based on global system selection #359

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 3 additions & 1 deletion src/components/dialog/faultEvent/FaultEventDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -48,7 +50,7 @@ const FaultEventDialog = ({ open, eventIri, treeUri, onCreated, onClose }: Props
Create Event
</DialogTitle>
<DialogContent dividers>
<FaultEventsReuseProvider treeUri={treeUri}>
<FaultEventsReuseProvider treeUri={treeUri} systemUri={selectedSystem?.iri}>
<FaultEventCreation useFormMethods={useFormMethods} eventReusing={true} />
</FaultEventsReuseProvider>
</DialogContent>
Expand Down
5 changes: 3 additions & 2 deletions src/components/dialog/faultTree/FaultTreeDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -64,7 +65,7 @@ const FaultTreeDialog = ({ open, handleCloseDialog }) => {
{...register("faultTreeName")}
helperText={!!useFormMethods.formState.errors.faultTreeName && t("newFtaModal.noSystemError")}
/>
<FaultEventsReuseProvider>
<FaultEventsReuseProvider systemUri={selectedSystem?.iri}>
<FaultEventCreation useFormMethods={useFormMethods} isRootEvent={true} />
</FaultEventsReuseProvider>
</DialogContent>
Expand Down
8 changes: 5 additions & 3 deletions src/hooks/useReusableFaultEvents.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
kostobog marked this conversation as resolved.
Show resolved Hide resolved
const [_faultEvents, _setFaultEvents] = useState<FaultEvent[]>([]);
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));
};

Expand Down
23 changes: 19 additions & 4 deletions src/services/faultTreeService.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -88,16 +88,31 @@ export const remove = async (faultTreeIri: string): Promise<void> => {
}
};

export const getReusableEvents = async (faultTreeIri: string): Promise<FaultEvent[]> => {
export const getRootReusableEvents = async (systemIri: string): Promise<FaultEvent[]> => {
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<FaultEvent>(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<FaultEvent[]> => {
try {
const fragment = extractFragment(systemIri);
const response = await axiosClient.get(`/faultEvents/all-fault-events/${fragment}`, {
headers: authHeaders(),
});

return JsonLdUtils.compactAndResolveReferencesAsArray<FaultEvent>(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)));
}
Expand Down