Skip to content

Commit fb75231

Browse files
committed
[Mission] fix duplicate action and surveillance
1 parent 68d0c1d commit fb75231

File tree

5 files changed

+82
-17
lines changed

5 files changed

+82
-17
lines changed

frontend/cypress/e2e/side_window/mission_form/mission_actions.spec.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -673,4 +673,35 @@ context('Side Window > Mission Form > Mission actions', () => {
673673
cy.clickButton('Confirmer la suppression')
674674
})
675675
})
676+
677+
it('Should duplicate control and surveillance', () => {
678+
cy.getDataCy('edit-mission-34').scrollIntoView().click({ force: true })
679+
680+
cy.getDataCy('action-card').eq(1).click()
681+
cy.get('input[name="isControlAttachedToReporting"]').should('be.checked')
682+
683+
cy.clickButton('Dupliquer le contrôle')
684+
// The duplicate control should be the "active" action and attached reporting should be removed
685+
cy.get('input[name="isControlAttachedToReporting"]').should('be.not.checked')
686+
cy.clickButton('Supprimer le contrôle')
687+
688+
// Attach reprting to surveillance
689+
cy.wait(250)
690+
cy.getDataCy('action-card').eq(0).click()
691+
cy.getDataCy('surveillance-form-toggle-reporting').click({ force: true })
692+
cy.fill('Signalements', ['6'])
693+
cy.wait(250)
694+
// duplicate surveillance from timeline
695+
cy.clickButton("Dupliquer l'action")
696+
cy.wait(500)
697+
cy.getDataCy('action-card').eq(0).click()
698+
// The duplicate surveillance should be the "active" action and attached reporting should be removed
699+
cy.get('input[name="isSurveillanceAttachedToReporting"]').should('be.not.checked')
700+
// The duplicate control should be the "active" action and attached reporting should be removed
701+
cy.get('input[name="isSurveillanceAttachedToReporting"]').should('be.not.checked')
702+
cy.clickButton('Supprimer la surveillance')
703+
704+
cy.getDataCy('action-card').eq(0).click()
705+
cy.getDataCy('surveillance-form-toggle-reporting').click({ force: true })
706+
})
676707
})

frontend/src/features/Mission/components/MissionForm/ActionForm/ControlForm/index.tsx

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { actionFactory } from '@features/Mission/Missions.helpers'
2+
import { useAppDispatch } from '@hooks/useAppDispatch'
23
import { useAppSelector } from '@hooks/useAppSelector'
34
import { useGetControlPlans } from '@hooks/useGetControlPlans'
45
import {
@@ -42,7 +43,7 @@ import { VehicleTypeSelector } from '../../../../../commonComponents/VehicleType
4243
import { getFormattedReportingId } from '../../../../../Reportings/utils'
4344
import { HIDDEN_ERROR } from '../../constants'
4445
import { useMissionAndActionsCompletion } from '../../hooks/useMissionAndActionsCompletion'
45-
import { getNumberOfInfractionTarget } from '../../slice'
46+
import { getNumberOfInfractionTarget, missionFormsActions } from '../../slice'
4647
import { Separator } from '../../style'
4748
import { MissingFieldsText } from '../MissingFieldsText'
4849
import {
@@ -67,6 +68,7 @@ export function ControlForm({
6768
removeControlAction: () => void
6869
}) {
6970
const { newWindowContainerRef } = useNewWindow()
71+
const dispatch = useAppDispatch()
7072
const {
7173
errors,
7274
setFieldValue,
@@ -195,9 +197,13 @@ export function ControlForm({
195197
if (!currentAction) {
196198
return
197199
}
198-
const duplicatedAction = actionFactory(currentAction)
200+
201+
const newControl = { ...currentAction, reportingIds: [] }
202+
const duplicatedAction = actionFactory(newControl)
203+
199204
setFieldValue('envActions', [duplicatedAction, ...(envActions || [])])
200-
}, [currentAction, setFieldValue, envActions])
205+
dispatch(missionFormsActions.setActiveActionId(duplicatedAction.id))
206+
}, [currentAction, setFieldValue, envActions, dispatch])
201207

202208
const updateIsControlAttachedToReporting = (checked: boolean | undefined) => {
203209
setIsReportingListVisible(checked ?? false)
@@ -339,7 +345,13 @@ export function ControlForm({
339345
<ActionThemes>{themesAsText}</ActionThemes>
340346
</TitleWithIcon>
341347
<HeaderButtons>
342-
<Button accent={Accent.SECONDARY} Icon={Icon.Duplicate} onClick={duplicateControl} size={Size.SMALL}>
348+
<Button
349+
accent={Accent.SECONDARY}
350+
Icon={Icon.Duplicate}
351+
onClick={duplicateControl}
352+
size={Size.SMALL}
353+
title="Dupliquer le contrôle"
354+
>
343355
Dupliquer
344356
</Button>
345357

@@ -348,7 +360,7 @@ export function ControlForm({
348360
Icon={Icon.Delete}
349361
onClick={handleRemoveAction}
350362
size={Size.SMALL}
351-
title="supprimer"
363+
title="Supprimer le contrôle"
352364
/>
353365
</HeaderButtons>
354366
</Header>

frontend/src/features/Mission/components/MissionForm/ActionForm/SurveillanceForm/index.tsx

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { actionFactory } from '@features/Mission/Missions.helpers'
2+
import { useAppDispatch } from '@hooks/useAppDispatch'
23
import { useGetControlPlans } from '@hooks/useGetControlPlans'
34
import { useGetControlPlansByYear } from '@hooks/useGetControlPlansByYear'
45
import {
@@ -38,6 +39,7 @@ import { dateDifferenceInHours } from '../../../../../../utils/dateDifferenceInH
3839
import { getFormattedReportingId } from '../../../../../Reportings/utils'
3940
import { HIDDEN_ERROR } from '../../constants'
4041
import { useMissionAndActionsCompletion } from '../../hooks/useMissionAndActionsCompletion'
42+
import { missionFormsActions } from '../../slice'
4143
import { Separator } from '../../style'
4244
import { MissingFieldsText } from '../MissingFieldsText'
4345
import {
@@ -55,6 +57,8 @@ import { SurveillanceThemes } from '../Themes/SurveillanceThemes'
5557
export function SurveillanceForm({ currentActionId, remove }) {
5658
const { newWindowContainerRef } = useNewWindow()
5759

60+
const dispatch = useAppDispatch()
61+
5862
const {
5963
errors,
6064
setFieldValue,
@@ -165,9 +169,13 @@ export function SurveillanceForm({ currentActionId, remove }) {
165169
if (!currentAction) {
166170
return
167171
}
168-
const duplicatedAction = actionFactory(currentAction)
172+
173+
const newSurveillance = { ...currentAction, reportingIds: [] }
174+
const duplicatedAction = actionFactory(newSurveillance)
175+
169176
setFieldValue('envActions', [duplicatedAction, ...(envActions || [])])
170-
}, [currentAction, setFieldValue, envActions])
177+
dispatch(missionFormsActions.setActiveActionId(duplicatedAction.id))
178+
}, [currentAction, setFieldValue, envActions, dispatch])
171179

172180
const updateStartDateTime = (date: string | undefined) => {
173181
const newSurveillanceDateYear = date ? customDayjs(date).year() : undefined
@@ -205,7 +213,13 @@ export function SurveillanceForm({ currentActionId, remove }) {
205213
<ActionThemes>{themeIds && themeIds?.length > 1 ? themesAsText?.join(', ') : themesAsText}</ActionThemes>
206214
</TitleWithIcon>
207215
<HeaderButtons>
208-
<Button accent={Accent.SECONDARY} Icon={Icon.Duplicate} onClick={duplicateSurveillance} size={Size.SMALL}>
216+
<Button
217+
accent={Accent.SECONDARY}
218+
Icon={Icon.Duplicate}
219+
onClick={duplicateSurveillance}
220+
size={Size.SMALL}
221+
title="Dupliquer la surveillance"
222+
>
209223
Dupliquer
210224
</Button>
211225

@@ -214,7 +228,7 @@ export function SurveillanceForm({ currentActionId, remove }) {
214228
Icon={Icon.Delete}
215229
onClick={handleRemoveAction}
216230
size={Size.SMALL}
217-
title="supprimer"
231+
title="Supprimer la surveillance"
218232
/>
219233
</HeaderButtons>
220234
</Header>

frontend/src/features/Mission/components/MissionForm/ActionsTimeLine/ActionCard/EnvActions/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,15 @@ export function EnvActions({
6767
accent={Accent.TERTIARY}
6868
Icon={Icon.Duplicate}
6969
onClick={duplicateAction}
70-
title="dupliquer"
70+
title="Dupliquer l'action"
7171
/>
7272
<IconButton
7373
accent={Accent.TERTIARY}
7474
color={THEME.color.maximumRed}
7575
data-cy={`actioncard-delete-button-${action.id}`}
7676
Icon={Icon.Delete}
7777
onClick={removeAction}
78-
title="supprimer"
78+
title="Supprimer l'action"
7979
/>
8080
</ActionButtons>
8181
{action.actionType === ActionTypeEnum.CONTROL && action.formattedReportingId && (

frontend/src/features/Mission/components/MissionForm/ActionsTimeLine/index.tsx

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -120,14 +120,22 @@ export function ActionsTimeLine({ currentActionId, setCurrentActionId }) {
120120
)
121121

122122
const handleDuplicateAction = useCallback(
123-
id => {
123+
(e, id) => {
124+
e.stopPropagation()
124125
const envAction = envActions && envActions.find(action => action.id === id)
125126

126-
if (envAction) {
127-
const duplicatedAction = actionFactory(envAction)
128-
setFieldValue('envActions', [duplicatedAction, ...(envActions || [])])
129-
setCurrentActionId(undefined)
127+
if (!envAction) {
128+
return
130129
}
130+
131+
let newAction = { ...envAction }
132+
if ('reportingIds' in newAction) {
133+
newAction = { ...newAction, reportingIds: [] }
134+
}
135+
136+
const duplicatedAction = actionFactory(newAction)
137+
setFieldValue('envActions', [duplicatedAction, ...envActions])
138+
setCurrentActionId(duplicatedAction.id)
131139
},
132140
[envActions, setFieldValue, setCurrentActionId]
133141
)
@@ -174,7 +182,7 @@ export function ActionsTimeLine({ currentActionId, setCurrentActionId }) {
174182
<ActionCard
175183
key={action.id}
176184
action={action}
177-
duplicateAction={() => handleDuplicateAction(action.id)}
185+
duplicateAction={e => handleDuplicateAction(e, action.id)}
178186
hasError={!!envActionsErrors}
179187
removeAction={event => handleRemoveAction(action.id, event)}
180188
selectAction={() => handleSelectAction(action.id)}

0 commit comments

Comments
 (0)