Skip to content

Commit

Permalink
[frontend] empty predefined expectations on inject update
Browse files Browse the repository at this point in the history
  • Loading branch information
isselparra committed Nov 26, 2024
1 parent 994a7c5 commit e8b5edf
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions openbas-front/src/admin/components/common/injects/UpdateInject.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Tab, Tabs } from '@mui/material';
import { useEffect, useRef, useState } from 'react';
import * as React from 'react';
import { useEffect, useRef, useState } from 'react';

import { fetchInject } from '../../../../actions/Inject';
import type { InjectOutputType } from '../../../../actions/injects/Inject';
Expand All @@ -24,6 +24,28 @@ interface Props {
injects?: InjectOutputType[];
}

function getInjectorContractWithEmptyPredefinedExpectations(injectorContractContent: string) {
const injectorContract = JSON.parse(injectorContractContent);
const { fields } = injectorContract;
const fieldsOnlyExpectations = fields.filter(
(f: { key: string }) => f.key === 'expectations',
);
if (fieldsOnlyExpectations.length === 0 || (fieldsOnlyExpectations.length > 0 && !fieldsOnlyExpectations[0].hasAttribute('predefinedExpectations'))) {
return injectorContract;
}
const fieldsWithoutExpectations = fields.filter(
(f: { key: string }) => f.key !== 'expectations',
);
const fieldsWithEmptyPredefinedExpectations = [
...fieldsWithoutExpectations,
{
...fieldsOnlyExpectations[0],
predefinedExpectations: [],
},
];
return { ...injectorContract, fields: fieldsWithEmptyPredefinedExpectations };
}

const UpdateInject: React.FC<Props> = ({ open, handleClose, onUpdateInject, massUpdateInject, injectId, isAtomic = false, injects, ...props }) => {
const { t } = useFormatter();
const dispatch = useAppDispatch();
Expand All @@ -49,7 +71,7 @@ const UpdateInject: React.FC<Props> = ({ open, handleClose, onUpdateInject, mass
const [injectorContract, setInjectorContract] = useState(null);
useEffect(() => {
if (inject?.inject_injector_contract?.injector_contract_content) {
setInjectorContract(JSON.parse(inject.inject_injector_contract?.injector_contract_content));
setInjectorContract(getInjectorContractWithEmptyPredefinedExpectations(inject.inject_injector_contract?.injector_contract_content));
}
}, [inject]);
return (
Expand Down

0 comments on commit e8b5edf

Please sign in to comment.