Skip to content

Commit

Permalink
[Mission] fix mission validation
Browse files Browse the repository at this point in the history
  • Loading branch information
claire2212 committed Oct 23, 2023
1 parent a060f1e commit 9a00741
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ VALUES
( 40, 10003),
( 41, 10002),
( 42, 10003),
( 43, 10018),
( 44, 10003),
( 45, 10014),
( 46, 10018),
Expand All @@ -118,7 +117,8 @@ VALUES
( 34, 10016, 'M. Capitaine Flame'),
( 34, 10017, 'Popeye 06789012345'),
( 47, 10002, 'A Team - Gimme your number'),
( 25, 10002, 'Full contact');
( 25, 10002, 'Full contact'),
( 43, 10018, 'Full contact');

INSERT INTO missions_control_resources
(mission_id, control_resource_id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ context('Mission', () => {

// delete theme to test error
cy.fill('Thématique de surveillance', '')
cy.clickButton(' Enregistrer et clôturer')
cy.clickButton('Enregistrer et clôturer')
cy.wait(100)
cy.get('*[data-cy="mission-errors"]').should('exist')

Expand Down
13 changes: 6 additions & 7 deletions frontend/cypress/e2e/side_window/mission/create_mission.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ context('Mission', () => {
it('A mission should be created', () => {
// Given
cy.intercept('GET', '/bff/v1/missions*').as('getMissions')
cy.wait('@getMissions')
cy.wait(400)
cy.get('*[data-cy="Missions-numberOfDisplayedMissions"]').then($el => {
const numberOfMissions = parseInt($el.text(), 10)
Expand Down Expand Up @@ -44,7 +43,7 @@ context('Mission', () => {
cy.get('[name="openBy"]').scrollIntoView().type('PCF')

cy.intercept('PUT', '/bff/v1/missions').as('createMission')
cy.get('form').submit()
cy.clickButton('Enregistrer et quitter')

// Then
cy.wait('@createMission').then(({ request, response }) => {
Expand Down Expand Up @@ -72,7 +71,6 @@ context('Mission', () => {
it('A mission should be deleted', () => {
// Given
cy.intercept('GET', '/bff/v1/missions*').as('getMissions')
cy.wait('@getMissions')
cy.wait(400)
cy.get('*[data-cy="Missions-numberOfDisplayedMissions"]').then($el => {
const numberOfMissions = parseInt($el.text(), 10)
Expand Down Expand Up @@ -111,7 +109,7 @@ context('Mission', () => {

cy.get('*[data-cy="reopen-mission"]').click()
cy.get('*[data-cy="control-unit-contact"]').type('Contact 012345')
cy.get('form').submit()
cy.clickButton('Enregistrer et quitter')

// Then
cy.wait('@updateMission').then(({ response }) => {
Expand All @@ -125,14 +123,15 @@ context('Mission', () => {
// Given
cy.wait(200)
cy.get('*[data-cy="edit-mission-43"]').click({ force: true })
cy.intercept('PUT', `/bff/v1/missions/43`).as('updateMission')
cy.intercept('PUT', '/bff/v1/missions/43').as('updateMission')

cy.fill("Contact de l'unité 1", '')
cy.wait(300)
cy.clickButton('Enregistrer et clôturer')

// Then
cy.wait('@updateMission').then(({ request, response }) => {
expect(response && response.statusCode).equal(200)
expect(request.body.controlUnits[0].contact).equal(null)
expect(request.body.controlUnits[0].contact).equal(undefined)
})
cy.get('*[data-cy="SideWindowHeader-title"]').contains('Missions et contrôles')
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ context('Mission actions', () => {
cy.get('*[data-cy="duplicate-infraction"]').eq(1).should('be.disabled')

cy.intercept('PUT', `/bff/v1/missions/34`).as('updateMission')
cy.get('form').submit()
cy.clickButton('Enregistrer et quitter')

// Then
cy.wait('@updateMission').then(({ request, response }) => {
Expand Down Expand Up @@ -70,7 +70,7 @@ context('Mission actions', () => {
cy.get('*[data-cy="envaction-add-theme"]').should('not.exist')

cy.intercept('PUT', `/bff/v1/missions/34`).as('updateMission')
cy.get('form').submit()
cy.clickButton('Enregistrer et quitter')

// Then
cy.wait('@updateMission').then(({ request, response }) => {
Expand Down Expand Up @@ -100,7 +100,7 @@ context('Mission actions', () => {
})

cy.intercept('PUT', `/bff/v1/missions/34`).as('updateMission')
cy.get('form').submit()
cy.clickButton('Enregistrer et quitter')

// Then
cy.wait('@updateMission').then(({ request, response }) => {
Expand Down Expand Up @@ -138,7 +138,7 @@ context('Mission actions', () => {
cy.get('*[data-cy="envaction-protected-species-selector"]').should('have.length', 0)

cy.intercept('PUT', `/bff/v1/missions/34`).as('updateMission')
cy.get('form').submit()
cy.clickButton('Enregistrer et quitter')

// Then
cy.wait('@updateMission').then(({ response }) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ context('Mission dates', () => {
cy.fill('Equipement de sécurité et respect des normes', false)

cy.intercept('PUT', `/bff/v1/missions/41`).as('updateMission')
cy.get('form').submit()
cy.clickButton('Enregistrer et quitter')

// Then
cy.wait('@updateMission').then(({ request, response }) => {
Expand All @@ -249,6 +249,6 @@ context('Mission dates', () => {
cy.fill('Respect du code de la navigation sur le plan d’eau', true)
cy.fill('Gens de mer', true)
cy.fill('Equipement de sécurité et respect des normes', true)
cy.get('form').submit()
cy.clickButton('Enregistrer et quitter')
})
})
32 changes: 12 additions & 20 deletions frontend/src/features/missions/MissionForm/MissionForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,10 @@ import { useAppSelector } from '../../../hooks/useAppSelector'
import { sideWindowActions } from '../../SideWindow/slice'
import { missionFactory } from '../Missions.helpers'

export function MissionForm({ id, isAlreadyClosed, isNewMission, selectedMission, setShouldValidateOnChange }) {
export function MissionForm({ id, isNewMission, selectedMission, setShouldValidateOnChange }) {
const dispatch = useAppDispatch()
const { sideWindow } = useAppSelector(state => state)
const { dirty, handleSubmit, setFieldValue, setValues, validateForm, values } =
useFormikContext<Partial<Mission | NewMission>>()
const { dirty, setFieldValue, setValues, validateForm, values } = useFormikContext<Partial<Mission | NewMission>>()

useSyncFormValuesWithRedux()
useUpdateSurveillance()
Expand Down Expand Up @@ -75,41 +74,34 @@ export function MissionForm({ id, isAlreadyClosed, isNewMission, selectedMission
dispatch(sideWindowActions.setCurrentPath(generatePath(sideWindowPaths.MISSIONS)))
}

const submitMission = async () => {
const submitMission = () => {
setShouldValidateOnChange(false)

// If the mission is not already closed (from the API), we want the `isClosed` field to be set to false.
// if the user has already tried to close the mission and the `isClosed` field is set to true.
if (!isAlreadyClosed) {
await setFieldValue('isClosed', false)
}
validateForm().then(errors => {
validateForm({ ...values, isClosed: false }).then(errors => {
if (_.isEmpty(errors)) {
handleSubmit()
dispatch(saveMission({ ...values, isClosed: true }))

return
}
setShouldValidateOnChange(true)
})
}

const closeMission = async () => {
await setFieldValue('isClosed', false)
validateForm({ ...values, isClosed: true }).then(async errors => {
const closeMission = () => {
validateForm({ ...values, isClosed: true }).then(errors => {
if (_.isEmpty(errors)) {
await setFieldValue('isClosed', true)
dispatch(saveMission({ ...values, isClosed: true }))

return handleSubmit()
return
}

return setShouldValidateOnChange(true)
setShouldValidateOnChange(true)
})
}

const reopenMission = () => {
validateForm({ ...values, isClosed: false }).then(async errors => {
validateForm({ ...values, isClosed: false }).then(errors => {
if (_.isEmpty(errors)) {
await setFieldValue('isClosed', false)
setFieldValue('isClosed', false)
if (dirty) {
return setIsReopenModalOpen(true)
}
Expand Down
11 changes: 2 additions & 9 deletions frontend/src/features/missions/MissionForm/index.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { skipToken } from '@reduxjs/toolkit/dist/query'
import { Formik } from 'formik'
import { noop } from 'lodash'
import { useMemo, useState } from 'react'
import styled from 'styled-components'

import { MissionForm } from './MissionForm'
import { MissionSchema } from './Schemas'
import { useGetMissionQuery } from '../../../api/missionsAPI'
import { saveMission } from '../../../domain/use_cases/missions/saveMission'
import { useAppDispatch } from '../../../hooks/useAppDispatch'
import { useAppSelector } from '../../../hooks/useAppSelector'
import { FormikForm } from '../../../uiMonitor/CustomFormikFields/FormikForm'
import { getIdTyped } from '../../../utils/getIdTyped'
Expand All @@ -20,7 +19,6 @@ export function Mission() {
multiMissions: { selectedMissions },
sideWindow
} = useAppSelector(state => state)
const dispatch = useAppDispatch()
const [shouldValidateOnChange, setShouldValidateOnChange] = useState(false)

const routeParams = getMissionPageRoute(sideWindow.currentPath)
Expand All @@ -44,10 +42,6 @@ export function Mission() {
return missionFactory(missionToEdit)
}, [idTyped, missionIsNewMission, missionToEdit])

const handleSubmitForm = values => {
dispatch(saveMission(values))
}

if (isLoading) {
return <div>Chargement en cours</div>
}
Expand All @@ -58,7 +52,7 @@ export function Mission() {
key={idTyped}
enableReinitialize
initialValues={missionFormikValues}
onSubmit={handleSubmitForm}
onSubmit={noop}
validateOnBlur={false}
validateOnChange={shouldValidateOnChange}
validateOnMount={false}
Expand All @@ -67,7 +61,6 @@ export function Mission() {
<FormikForm>
<MissionForm
id={idTyped}
isAlreadyClosed={missionToEdit?.isClosed}
isNewMission={missionIsNewMission}
selectedMission={selectedMission?.mission}
setShouldValidateOnChange={setShouldValidateOnChange}
Expand Down

0 comments on commit 9a00741

Please sign in to comment.