Skip to content

Commit

Permalink
rework Modify team dialog in inject definition
Browse files Browse the repository at this point in the history
Signed-off-by: Antoine MAZEAS <[email protected]>
  • Loading branch information
antoinemzs committed Dec 24, 2024
1 parent b6df3e4 commit aa23d8a
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
}));
Expand Down Expand Up @@ -346,6 +347,7 @@ const CreateInjectDetails = ({
inject_asset_groups: [],
inject_documents: [],
}}
availableTeamIds={availableTeamIds}
injectorContract={{ ...contractContent }}
handleClose={handleClose}
tagsMap={tagsMap}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -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<Props> = ({
handleAddTeams,
handleModifyTeams,
injectTeamsIds,
availableTeamIds,
}) => {
// Standard hooks
const dispatch = useAppDispatch()
const { t } = useFormatter();
const classes = useStyles();
const { permissions } = useContext(PermissionsContext);
Expand All @@ -56,6 +64,13 @@ const InjectAddTeams: FunctionComponent<Props> = ({
const [teamValues, setTeamValues] = useState<TeamOutput[]>([]);
const [selectedTeamValues, setSelectedTeamValues] = useState<TeamOutput[]>([]);

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);

Expand All @@ -65,13 +80,14 @@ const InjectAddTeams: FunctionComponent<Props> = ({
};

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]);

Expand Down Expand Up @@ -127,7 +143,7 @@ const InjectAddTeams: FunctionComponent<Props> = ({
<ControlPointOutlined color="primary" />
</ListItemIcon>
<ListItemText
primary={t('Add target teams')}
primary={t('Modify target teams')}
classes={{ primary: classes.text }}
/>
</ListItemButton>
Expand All @@ -145,7 +161,7 @@ const InjectAddTeams: FunctionComponent<Props> = ({
},
}}
>
<DialogTitle>{t('Add target teams in this inject')}</DialogTitle>
<DialogTitle>{t('Modify target teams in this inject')}</DialogTitle>
<DialogContent>
<Box sx={{ marginTop: 2 }}>
<SelectList
Expand All @@ -171,7 +187,7 @@ const InjectAddTeams: FunctionComponent<Props> = ({
<DialogActions>
<Button onClick={handleClose}>{t('Cancel')}</Button>
<Button color="secondary" onClick={submitAddTeams}>
{t('Add')}
{t('Update')}
</Button>
</DialogActions>
</Dialog>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 || [],
Expand Down Expand Up @@ -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));
}

Expand Down Expand Up @@ -987,6 +988,7 @@ class InjectDefinition extends Component {
const {
allTeams,
teamsIds,
availableTeamIds,
assetIds,
assetGroupIds,
documents,
Expand Down Expand Up @@ -1163,7 +1165,8 @@ class InjectDefinition extends Component {
/>
<InjectAddTeams
injectTeamsIds={teamsIds}
handleAddTeams={this.handleAddTeams.bind(this)}
availableTeamIds={availableTeamIds}
handleAddTeams={this.handleModifyTeams.bind(this)}
/>
</>
)}
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -57,10 +59,16 @@ const UpdateInject: React.FC<Props> = ({ 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
Expand Down Expand Up @@ -100,6 +108,7 @@ const UpdateInject: React.FC<Props> = ({ open, handleClose, onUpdateInject, mass
contractContent={injectorContract}
injectId={injectId}
inject={inject}
availableTeamIds={availableTeamIds}
handleClose={handleClose}
onUpdateInject={onUpdateInject}
isAtomic={isAtomic}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ const useStyles = makeStyles(theme => ({
const UpdateInjectDetails = ({
contractContent,
inject,
availableTeamIds,
handleClose,
onUpdateInject,
isAtomic = false,
Expand Down Expand Up @@ -317,6 +318,7 @@ const UpdateInjectDetails = ({
values={values}
submitting={submitting}
inject={initialValues}
availableTeamIds={availableTeamIds}
injectorContract={contractContent}
handleClose={handleClose}
tagsMap={tagsMap}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ class QuickInject extends Component {
this.state = {
allTeams: false,
teamsIds: [],
availableTeamIds: this.props.exercise?.exercise_teams || [],
documents: [],
expectations: [],
teamsSortBy: 'team_name',
Expand Down Expand Up @@ -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],
});
}

Expand Down Expand Up @@ -809,6 +810,7 @@ class QuickInject extends Component {
const {
allTeams,
teamsIds,
availableTeamIds,
documents,
expectations,
teamsSortBy,
Expand Down Expand Up @@ -1146,7 +1148,8 @@ class QuickInject extends Component {
))}
<InjectAddTeams
injectTeamsIds={teamsIds}
handleAddTeams={this.handleAddTeams.bind(
availableTeamIds={availableTeamIds}
handleModifyTeams={this.handleModifyTeams.bind(
this,
)}
/>
Expand Down
8 changes: 7 additions & 1 deletion openbas-front/src/utils/Localization.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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': '从团队移除',
Expand Down

0 comments on commit aa23d8a

Please sign in to comment.