From aa23d8a3f5d0b545a11271b8042031b36e2bd01c Mon Sep 17 00:00:00 2001 From: Antoine MAZEAS Date: Tue, 24 Dec 2024 16:34:25 +0100 Subject: [PATCH] rework Modify team dialog in inject definition Signed-off-by: Antoine MAZEAS --- .../common/injects/CreateInjectDetails.js | 2 ++ .../common/injects/InjectAddTeams.tsx | 28 +++++++++++++++---- .../common/injects/InjectDefinition.js | 10 +++++-- .../common/injects/UpdateInject.tsx | 11 +++++++- .../common/injects/UpdateInjectDetails.js | 2 ++ .../simulation/injects/QuickInject.js | 9 ++++-- openbas-front/src/utils/Localization.js | 8 +++++- 7 files changed, 56 insertions(+), 14 deletions(-) diff --git a/openbas-front/src/admin/components/common/injects/CreateInjectDetails.js b/openbas-front/src/admin/components/common/injects/CreateInjectDetails.js index c5490e0335..309e895680 100644 --- a/openbas-front/src/admin/components/common/injects/CreateInjectDetails.js +++ b/openbas-front/src/admin/components/common/injects/CreateInjectDetails.js @@ -81,6 +81,7 @@ const CreateInjectDetails = ({ const [openDetails, setOpenDetails] = useState(false); const [injectDetailsState, setInjectDetailsState] = useState({}); + const availableTeamIds = props.teamsFromExerciseOrScenario ? props.teamsFromExerciseOrScenario.map(t => t.team_id) : []; const { tagsMap } = useHelper(helper => ({ tagsMap: helper.getTagsMap(), })); @@ -346,6 +347,7 @@ const CreateInjectDetails = ({ inject_asset_groups: [], inject_documents: [], }} + availableTeamIds={availableTeamIds} injectorContract={{ ...contractContent }} handleClose={handleClose} tagsMap={tagsMap} diff --git a/openbas-front/src/admin/components/common/injects/InjectAddTeams.tsx b/openbas-front/src/admin/components/common/injects/InjectAddTeams.tsx index a332909c32..8d86342fa1 100644 --- a/openbas-front/src/admin/components/common/injects/InjectAddTeams.tsx +++ b/openbas-front/src/admin/components/common/injects/InjectAddTeams.tsx @@ -25,6 +25,11 @@ import type { Theme } from '../../../../components/Theme'; import type { TeamOutput } from '../../../../utils/api-types'; import CreateTeam from '../../components/teams/CreateTeam'; import { PermissionsContext, TeamContext } from '../Context'; +import { useAppDispatch } from '../../../../utils/hooks'; +import { fetchTeams } from '../../../../actions/teams/team-actions'; +import {useHelper} from "../../../../store"; +import {TeamsHelper} from "../../../../actions/teams/team-helper"; +import useDataLoader from "../../../../utils/hooks/useDataLoader"; const useStyles = makeStyles((theme: Theme) => ({ item: { @@ -39,15 +44,18 @@ const useStyles = makeStyles((theme: Theme) => ({ })); interface Props { - handleAddTeams: (teamIds: string[]) => void; + handleModifyTeams: (teamIds: string[]) => void; injectTeamsIds: string[]; + availableTeamIds: string[]; } const InjectAddTeams: FunctionComponent = ({ - handleAddTeams, + handleModifyTeams, injectTeamsIds, + availableTeamIds, }) => { // Standard hooks + const dispatch = useAppDispatch() const { t } = useFormatter(); const classes = useStyles(); const { permissions } = useContext(PermissionsContext); @@ -56,6 +64,13 @@ const InjectAddTeams: FunctionComponent = ({ const [teamValues, setTeamValues] = useState([]); const [selectedTeamValues, setSelectedTeamValues] = useState([]); + console.log("availableTeamIds = {}", availableTeamIds) + const allTeams = useHelper((helper: TeamsHelper) => helper.getTeams().filter(t => availableTeamIds.length > 0 ? availableTeamIds.includes(t.team_id) : true)); + useDataLoader(() => { + dispatch(fetchTeams()).finally(() => { + }); + }); + // Dialog const [open, setOpen] = useState(false); @@ -65,13 +80,14 @@ const InjectAddTeams: FunctionComponent = ({ }; const submitAddTeams = () => { - handleAddTeams(selectedTeamValues.map(v => v.team_id).filter(id => !injectTeamsIds.includes(id))); + handleModifyTeams(selectedTeamValues.map(v => v.team_id)); handleClose(); }; useEffect(() => { if (open) { findTeams(injectTeamsIds).then(result => setSelectedTeamValues(result.data)); + setTeamValues(allTeams); } }, [open, injectTeamsIds]); @@ -127,7 +143,7 @@ const InjectAddTeams: FunctionComponent = ({ @@ -145,7 +161,7 @@ const InjectAddTeams: FunctionComponent = ({ }, }} > - {t('Add target teams in this inject')} + {t('Modify target teams in this inject')} = ({ diff --git a/openbas-front/src/admin/components/common/injects/InjectDefinition.js b/openbas-front/src/admin/components/common/injects/InjectDefinition.js index 0708373f41..7db009bdf9 100644 --- a/openbas-front/src/admin/components/common/injects/InjectDefinition.js +++ b/openbas-front/src/admin/components/common/injects/InjectDefinition.js @@ -352,6 +352,7 @@ class InjectDefinition extends Component { super(props); this.state = { allTeams: props.inject.inject_all_teams, + availableTeamIds: props.availableTeamIds || [], teamsIds: props.inject.inject_teams || [], assetIds: props.inject.inject_assets || [], assetGroupIds: props.inject.inject_asset_groups || [], @@ -412,9 +413,9 @@ class InjectDefinition extends Component { } // Teams - handleAddTeams(teamsIds) { + handleModifyTeams(teamsIds) { this.setState({ - teamsIds: [...this.state.teamsIds, ...teamsIds], + teamsIds: [...teamsIds], }, () => this.props.setInjectDetailsState(this.state)); } @@ -987,6 +988,7 @@ class InjectDefinition extends Component { const { allTeams, teamsIds, + availableTeamIds, assetIds, assetGroupIds, documents, @@ -1163,7 +1165,8 @@ class InjectDefinition extends Component { /> )} @@ -1538,6 +1541,7 @@ InjectDefinition.propTypes = { nsdt: PropTypes.func, injectId: PropTypes.string, inject: PropTypes.object, + availableTeamIds: PropTypes.array, fetchInjectTeams: PropTypes.func, fetchChannels: PropTypes.func, fetchChallenges: PropTypes.func, diff --git a/openbas-front/src/admin/components/common/injects/UpdateInject.tsx b/openbas-front/src/admin/components/common/injects/UpdateInject.tsx index 3e70271e68..bb2294286e 100644 --- a/openbas-front/src/admin/components/common/injects/UpdateInject.tsx +++ b/openbas-front/src/admin/components/common/injects/UpdateInject.tsx @@ -13,6 +13,8 @@ import { useAppDispatch } from '../../../../utils/hooks'; import useDataLoader from '../../../../utils/hooks/useDataLoader'; import UpdateInjectDetails from './UpdateInjectDetails'; import UpdateInjectLogicalChains from './UpdateInjectLogicalChains'; +import {ExercisesHelper} from "../../../../actions/exercises/exercise-helper"; +import {fetchExercise} from "../../../../actions/Exercise"; interface Props { open: boolean; @@ -57,10 +59,16 @@ const UpdateInject: React.FC = ({ open, handleClose, onUpdateInject, mass const { inject } = useHelper((helper: InjectHelper) => ({ inject: helper.getInject(injectId), })); + const { simulation } = useHelper((helper: ExercisesHelper) => ({ + simulation: helper.getExercise(inject.inject_exercise) + })); + const availableTeamIds = simulation?.exercise_teams || [] useDataLoader(() => { setIsInjectLoading(true); - dispatch(fetchInject(injectId)).finally(() => setIsInjectLoading(false)); + dispatch(fetchInject(injectId)); + dispatch(fetchExercise(inject.inject_exercise)); + setIsInjectLoading(false); }); // Selection @@ -100,6 +108,7 @@ const UpdateInject: React.FC = ({ open, handleClose, onUpdateInject, mass contractContent={injectorContract} injectId={injectId} inject={inject} + availableTeamIds={availableTeamIds} handleClose={handleClose} onUpdateInject={onUpdateInject} isAtomic={isAtomic} diff --git a/openbas-front/src/admin/components/common/injects/UpdateInjectDetails.js b/openbas-front/src/admin/components/common/injects/UpdateInjectDetails.js index 5e582fdea2..33e215c1dd 100644 --- a/openbas-front/src/admin/components/common/injects/UpdateInjectDetails.js +++ b/openbas-front/src/admin/components/common/injects/UpdateInjectDetails.js @@ -51,6 +51,7 @@ const useStyles = makeStyles(theme => ({ const UpdateInjectDetails = ({ contractContent, inject, + availableTeamIds, handleClose, onUpdateInject, isAtomic = false, @@ -317,6 +318,7 @@ const UpdateInjectDetails = ({ values={values} submitting={submitting} inject={initialValues} + availableTeamIds={availableTeamIds} injectorContract={contractContent} handleClose={handleClose} tagsMap={tagsMap} diff --git a/openbas-front/src/admin/components/simulations/simulation/injects/QuickInject.js b/openbas-front/src/admin/components/simulations/simulation/injects/QuickInject.js index 93a9721753..5c4c63e4f4 100644 --- a/openbas-front/src/admin/components/simulations/simulation/injects/QuickInject.js +++ b/openbas-front/src/admin/components/simulations/simulation/injects/QuickInject.js @@ -235,6 +235,7 @@ class QuickInject extends Component { this.state = { allTeams: false, teamsIds: [], + availableTeamIds: this.props.exercise?.exercise_teams || [], documents: [], expectations: [], teamsSortBy: 'team_name', @@ -263,9 +264,9 @@ class QuickInject extends Component { this.setState({ openVariables: false }); } - handleAddTeams(teamsIds) { + handleModifyTeams(teamsIds) { this.setState({ - teamsIds: [...this.state.teamsIds, ...teamsIds], + teamsIds: [...teamsIds], }); } @@ -809,6 +810,7 @@ class QuickInject extends Component { const { allTeams, teamsIds, + availableTeamIds, documents, expectations, teamsSortBy, @@ -1146,7 +1148,8 @@ class QuickInject extends Component { ))} diff --git a/openbas-front/src/utils/Localization.js b/openbas-front/src/utils/Localization.js index 9621c90879..0dd7a6853b 100644 --- a/openbas-front/src/utils/Localization.js +++ b/openbas-front/src/utils/Localization.js @@ -261,13 +261,16 @@ const i18n = { 'Souhaitez-vous supprimer ce document ?', 'Update the document': 'Modifier le document', 'Inject data': 'Données du stimuli', - 'Update teams': 'Modifier les équipe', + 'Update teams': 'Modifier les équipes', 'Add target teams in this inject': 'Ajouter des équipes cibles dans ce stimuli', + 'Modify target teams in this inject': + 'Modifier les équipes cibles dans ce stimuli', 'Targeted teams': 'Equipes ciblées', 'Test the inject': 'Tester le stimuli', 'All teams': 'Toutes les équipes', 'Add target teams': 'Ajouter des équipes cibles', + 'Modify target teams': 'Modifier les équipes cibles', 'Trigger after': 'Se déclenche après', 'Create a new inject': 'Créer un nouveau stimuli', 'Remove from the team': 'Retirer de l\'équipe', @@ -1699,10 +1702,13 @@ const i18n = { 'Update teams': '编辑团队', 'Add target teams in this inject': '在这个注入里添加目标团队', + 'Modify target teams in this inject': + '在此注入中修改目标团队', 'Targeted teams': '目标团队', 'Test the inject': '测试注入', 'All teams': '所有团队', 'Add target teams': '添加目标团队', + 'Modify target teams': '修改目标团队', 'Trigger after': '在之后触发', 'Create a new inject': '创建新注入', 'Remove from the team': '从团队移除',