diff --git a/openbas-front/src/admin/components/atomic_testings/atomic_testing/TargetResultsDetail.tsx b/openbas-front/src/admin/components/atomic_testings/atomic_testing/TargetResultsDetail.tsx index 57dfdabe58..28e2419f4c 100644 --- a/openbas-front/src/admin/components/atomic_testings/atomic_testing/TargetResultsDetail.tsx +++ b/openbas-front/src/admin/components/atomic_testings/atomic_testing/TargetResultsDetail.tsx @@ -101,7 +101,7 @@ const TargetResultsDetailFlow: FunctionComponent = ({ const theme = useTheme(); const { nsdt, t } = useFormatter(); const [anchorEls, setAnchorEls] = useState>({}); - const [selectedExpectationForCreation, setSelectedExpectationForCreation] = useState(null); + const [selectedExpectationForCreation, setSelectedExpectationForCreation] = useState<{ injectExpectation: InjectExpectationsStore, sourceIds: string[] } | null>(null); const [selectedResultEdition, setSelectedResultEdition] = useState<{ injectExpectation: InjectExpectationsStore, expectationResult: InjectExpectationResult } | null>(null); const [selectedResultDeletion, setSelectedResultDeletion] = useState<{ injectExpectation: InjectExpectationsStore, expectationResult: InjectExpectationResult } | null>(null); const [initialized, setInitialized] = useState(false); @@ -174,6 +174,17 @@ const TargetResultsDetailFlow: FunctionComponent = ({ return { ...step, status: lastExecutionEndDate ? 'SUCCESS' : 'PENDING' }; }); }; + + const computeExistingSourceIds = (results: InjectExpectationResult[]) => { + const sourceIds: string[] = []; + results.forEach((result) => { + if (result.sourceId) { + sourceIds.push(result.sourceId); + } + }); + return sourceIds; + }; + // Fetching data const { injectResultDto, updateInjectResultDto } = useContext(InjectResultDtoContext); useEffect(() => { @@ -461,7 +472,7 @@ const TargetResultsDetailFlow: FunctionComponent = ({ {injectExpectation.inject_expectation_results && injectExpectation.inject_expectation_results.map((expectationResult, index) => ( - + = ({ ))} - {(['DETECTION', 'PREVENTION'].includes(injectExpectation.inject_expectation_type) || (injectExpectation.inject_expectation_type === 'MANUAL' && injectExpectation.inject_expectation_results && injectExpectation.inject_expectation_results.length === 0)) && ( - - - setSelectedExpectationForCreation(injectExpectation)}> - - - - - )} + {(['DETECTION', 'PREVENTION'].includes(injectExpectation.inject_expectation_type) + || (injectExpectation.inject_expectation_type === 'MANUAL' && injectExpectation.inject_expectation_results && injectExpectation.inject_expectation_results.length === 0)) + && ( + + + setSelectedExpectationForCreation( + { injectExpectation, sourceIds: computeExistingSourceIds(injectExpectation.inject_expectation_results ?? []) }, + )} + > + + + + + )} @@ -526,8 +544,14 @@ const TargetResultsDetailFlow: FunctionComponent = ({ {selectedExpectationForCreation && ( <> - {selectedExpectationForCreation.inject_expectation_type === 'MANUAL' && } - {['DETECTION', 'PREVENTION'].includes(selectedExpectationForCreation.inject_expectation_type) && } + {selectedExpectationForCreation.injectExpectation.inject_expectation_type === 'MANUAL' + && } + {['DETECTION', 'PREVENTION'].includes(selectedExpectationForCreation.injectExpectation.inject_expectation_type) + && } )} @@ -543,8 +567,19 @@ const TargetResultsDetailFlow: FunctionComponent = ({ {selectedResultEdition && selectedResultEdition.injectExpectation && ( <> - {selectedResultEdition.injectExpectation.inject_expectation_type === 'MANUAL' && } - {['DETECTION', 'PREVENTION'].includes(selectedResultEdition.injectExpectation.inject_expectation_type) && } + {selectedResultEdition.injectExpectation.inject_expectation_type === 'MANUAL' + && + } + {['DETECTION', 'PREVENTION'].includes(selectedResultEdition.injectExpectation.inject_expectation_type) + && + } )} diff --git a/openbas-front/src/admin/components/simulations/simulation/validation/expectations/DetectionPreventionExpectationsValidationForm.tsx b/openbas-front/src/admin/components/simulations/simulation/validation/expectations/DetectionPreventionExpectationsValidationForm.tsx index ca65982b5d..f0397cc027 100644 --- a/openbas-front/src/admin/components/simulations/simulation/validation/expectations/DetectionPreventionExpectationsValidationForm.tsx +++ b/openbas-front/src/admin/components/simulations/simulation/validation/expectations/DetectionPreventionExpectationsValidationForm.tsx @@ -34,10 +34,11 @@ const useStyles = makeStyles((theme: Theme) => ({ interface FormProps { expectation: InjectExpectationsStore; result?: InjectExpectationResult; + sourceIds?: string[]; onUpdate?: () => void; } -const DetectionPreventionExpectationsValidationForm: FunctionComponent = ({ expectation, result, onUpdate }) => { +const DetectionPreventionExpectationsValidationForm: FunctionComponent = ({ expectation, result, sourceIds = [], onUpdate }) => { const classes = useStyles(); const { t } = useFormatter(); const dispatch = useAppDispatch(); @@ -69,10 +70,14 @@ const DetectionPreventionExpectationsValidationForm: FunctionComponent (n.asset_external_reference === null && !sourceIds.includes(n.asset_id)); + return (
{result && ( @@ -96,8 +101,9 @@ const DetectionPreventionExpectationsValidationForm: FunctionComponent )} /> diff --git a/openbas-front/src/components/fields/SecurityPlatformField.tsx b/openbas-front/src/components/fields/SecurityPlatformField.tsx index f493d653c7..f343d9fc86 100644 --- a/openbas-front/src/components/fields/SecurityPlatformField.tsx +++ b/openbas-front/src/components/fields/SecurityPlatformField.tsx @@ -31,51 +31,54 @@ interface Props { fieldValue: string; fieldOnChange: (value: string) => void; errors: FieldErrors; + filterOptions: (securityPlatform: SecurityPlatform) => boolean; style: CSSProperties; - onlyManual?: boolean; + editing: boolean; } +const securityPlatformsToOptions = (securityPlatforms: SecurityPlatform[], filterOptions: (securityPlatform: SecurityPlatform) => boolean) => { + return securityPlatforms + .filter(filterOptions) + .map((n) => ({ + id: n.asset_id, + label: n.asset_name, + logo_dark: n.security_platform_logo_dark, + logo_light: n.security_platform_logo_light, + })); +}; + const SecurityPlatformField: FunctionComponent = ({ name, label, fieldValue, fieldOnChange, errors, + filterOptions, style, - onlyManual, + editing, }) => { // Standard hooks const theme = useTheme(); const classes = useStyles(); + const dispatch = useAppDispatch(); // Fetching data - const { securityPlatforms }: { securityPlatforms: [SecurityPlatform]; } = useHelper((helper: SecurityPlatformHelper) => ({ + const { securityPlatforms }: { securityPlatforms: SecurityPlatform[]; } = useHelper((helper: SecurityPlatformHelper) => ({ securityPlatforms: helper.getSecurityPlatforms(), })); - const dispatch = useAppDispatch(); useDataLoader(() => { dispatch(fetchSecurityPlatforms()); }); // Form - const securityPlatformsOptions = securityPlatforms - .filter((n) => (onlyManual ? n.asset_external_reference === null : true)) - .map( - (n) => ({ - id: n.asset_id, - label: n.asset_name, - logo_dark: n.security_platform_logo_dark, - logo_light: n.security_platform_logo_light, - }), - ); - const valueResolver = () => { - return securityPlatformsOptions.filter((securityPlatform) => fieldValue === securityPlatform.id).at(0); - }; + const securityPlatformsOptions = securityPlatformsToOptions(securityPlatforms, filterOptions); + + const selectedValue = securityPlatformsOptions.find((option) => option.id === fieldValue) || null; return (
= ({ clearOnBlur={false} clearOnEscape={false} options={securityPlatformsOptions} + disabled={editing} onChange={(_, value) => { fieldOnChange(value?.id ?? ''); }}