Skip to content

Commit

Permalink
[Mission] fix closeSchema
Browse files Browse the repository at this point in the history
  • Loading branch information
claire2212 committed Oct 11, 2023
1 parent b8e5d9c commit c59a56a
Show file tree
Hide file tree
Showing 11 changed files with 50 additions and 18 deletions.
2 changes: 1 addition & 1 deletion frontend/src/domain/entities/missions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ export type EnvActionCommonProperties = {

export type EnvActionTheme = {
protectedSpecies?: string[]
subThemes?: string[]
subThemes: string[]
theme: string
}
export type NewEnvActionControl = EnvActionCommonProperties & {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ export function SubThemesSelector({ actionIndex, label, theme, themeIndex }) {
const { data: controlThemes, isError, isLoading } = useGetControlThemesQuery()
const { newWindowContainerRef } = useNewWindow()
const { setFieldValue } = useFormikContext<Mission>()
const [currentSubThemesField] = useField<string[]>(`envActions[${actionIndex}].themes[${themeIndex}].subThemes`)
const [currentSubThemesField, currentSubThemesProps] = useField<string[]>(
`envActions[${actionIndex}].themes[${themeIndex}].subThemes`
)

const availableThemes = useMemo(
() =>
Expand All @@ -41,7 +43,7 @@ export function SubThemesSelector({ actionIndex, label, theme, themeIndex }) {
baseContainer={newWindowContainerRef.current}
data-cy="envaction-subtheme-selector"
disabled={!theme}
isErrorMessageHidden
error={currentSubThemesProps.error}
isLight
label={label}
name={`${actionIndex}-${themeIndex}`}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ import type { Mission } from '../../../../../../domain/entities/missions'
export function ThemeSelector({ actionIndex, label, themeIndex }) {
const { data: controlThemes, isError, isLoading } = useGetControlThemesQuery()
const { newWindowContainerRef } = useNewWindow()
const [currentThemeField] = useField<string>(`envActions[${actionIndex}].themes[${themeIndex}].theme`)
const [currentThemeField, currentThemeProps] = useField<string>(
`envActions[${actionIndex}].themes[${themeIndex}].theme`
)
const { setFieldValue, values } = useFormikContext<Mission>()

const availableThemes = useMemo(
Expand All @@ -37,7 +39,7 @@ export function ThemeSelector({ actionIndex, label, themeIndex }) {
key={`${actionIndex}-${themeIndex}`}
baseContainer={newWindowContainerRef.current}
data-cy="envaction-theme-selector"
isErrorMessageHidden
error={currentThemeProps.error}
isLight
label={label}
name={`${actionIndex}-${themeIndex}`}
Expand Down
5 changes: 2 additions & 3 deletions frontend/src/features/missions/MissionForm/MissionForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,9 @@ export function MissionForm({ id, isAlreadyClosed, isNewMission, selectedMission
validateForm().then(errors => {
if (_.isEmpty(errors)) {
handleSubmit()

return
} else {
setShouldValidateOnChange(true)
}
setShouldValidateOnChange(true)
})
}

Expand Down
11 changes: 9 additions & 2 deletions frontend/src/features/missions/MissionForm/Schemas/Control.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import _ from 'lodash'
import * as Yup from 'yup'

import { ClosedInfractionSchema, NewInfractionSchema } from './Infraction'
Expand All @@ -8,6 +9,12 @@ import { REACT_APP_CYPRESS_TEST } from '../../../../env'

const shouldUseAlternateValidationInTestEnvironment = process.env.NODE_ENV === 'development' || REACT_APP_CYPRESS_TEST

const ControlPointSchema = Yup.object().test({
message: 'Veuillez définir un point de contrôle',
name: 'has-geom',
test: val => val && !_.isEmpty(val?.coordinates)
})

export const getNewEnvActionControlSchema = (ctx: any): Yup.SchemaOf<EnvActionControl> =>
Yup.object()
.shape({
Expand Down Expand Up @@ -59,10 +66,10 @@ export const getClosedEnvActionControlSchema = (ctx: any): Yup.SchemaOf<EnvActio
actionType: Yup.mixed().oneOf([ActionTypeEnum.CONTROL]),
geom: shouldUseAlternateValidationInTestEnvironment
? Yup.object().nullable()
: Yup.array().ensure().min(1, 'Requis'),
: Yup.array().of(ControlPointSchema).ensure().min(1, 'Veuillez définir un point de contrôle'),
id: Yup.string().required(),
infractions: Yup.array().of(ClosedInfractionSchema).ensure().required(),
themes: Yup.array().of(ThemeSchema).ensure().required(),
themes: Yup.array().of(ThemeSchema).ensure().required().min(1),
vehicleType: Yup.string().when('actionTargetType', (actionTargetType, schema) => {
if (!actionTargetType || actionTargetType === TargetTypeEnum.VEHICLE) {
return schema.nullable().required('Requis')
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import _ from 'lodash'
import * as Yup from 'yup'

import { ThemeSchema } from './Theme'
Expand All @@ -6,6 +7,12 @@ import { REACT_APP_CYPRESS_TEST } from '../../../../env'

const shouldUseAlternateValidationInTestEnvironment = process.env.NODE_ENV === 'development' || REACT_APP_CYPRESS_TEST

const SurveillanceZoneSchema = Yup.object().test({
message: 'Veuillez définir une zone de surveillance',
name: 'has-geom',
test: val => val && !_.isEmpty(val?.coordinates)
})

export const getNewEnvActionSurveillanceSchema = (ctx: any): Yup.SchemaOf<EnvActionSurveillance> =>
Yup.object()
.shape({
Expand Down Expand Up @@ -92,7 +99,8 @@ export const getClosedEnvActionSurveillanceSchema = (ctx: any): Yup.SchemaOf<Env
otherwise: () =>
shouldUseAlternateValidationInTestEnvironment
? Yup.object().nullable()
: Yup.array().ensure().min(1, 'Requis'),
: Yup.array().of(SurveillanceZoneSchema).ensure().min(1, 'Veuillez définir une zone de surveillance'),

then: () => Yup.object().nullable()
}),

Expand Down
4 changes: 2 additions & 2 deletions frontend/src/features/missions/MissionForm/Schemas/Theme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import type { EnvActionTheme } from '../../../../domain/entities/missions'
export const ThemeSchema: Yup.SchemaOf<EnvActionTheme> = Yup.object().shape({
protectedSpecies: Yup.array().of(Yup.string().optional()).nullable().optional(),
subThemes: Yup.array()
.of(Yup.string().required().default(''))
.of(Yup.string().required())
.ensure()
.required()
.min(1, 'Sélectionnez au moins une sous thématique'),
theme: Yup.string().required('Sélectionnez un thême')
theme: Yup.string().nullable().required('Sélectionnez un thème')
})
4 changes: 3 additions & 1 deletion frontend/src/features/missions/MissionForm/Schemas/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,9 @@ const NewMissionSchema: Yup.SchemaOf<NewMission> = Yup.object()
envActions: Yup.array()
.of(NewEnvActionSchema as any)
.nullable(),
geom: shouldUseAlternateValidationInTestEnvironment ? Yup.object().nullable() : MissionZoneSchema,
geom: shouldUseAlternateValidationInTestEnvironment
? Yup.object().nullable()
: Yup.array().of(MissionZoneSchema).ensure().min(1, 'Veuillez définir une zone de mission'),
isClosed: Yup.boolean().oneOf([false]).required(),
missionTypes: MissionTypesSchema,
openBy: Yup.string()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export const updateTheme =
const currentThemeValue = get(mission, themesPath) as EnvActionTheme[]

const newValue = [...currentThemeValue]
newValue.splice(themeIndex, 1, { theme: value })
newValue.splice(themeIndex, 1, { protectedSpecies: [], subThemes: [], theme: value })

setFieldValue(themesPath, newValue)
}
Expand Down
16 changes: 14 additions & 2 deletions frontend/src/features/missions/Missions.helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,13 @@ export const actionFactory = ({
id: uuidv4(),
infractions: [],
observations: '',
themes: [],
themes: [
{
protectedSpecies: undefined,
subThemes: [],
theme: ''
}
],
...action
}
case ActionTypeEnum.NOTE:
Expand All @@ -56,7 +62,13 @@ export const actionFactory = ({
durationMatchesMission: true,
id: uuidv4(),
observations: '',
themes: [],
themes: [
{
protectedSpecies: undefined,
subThemes: [],
theme: ''
}
],
...action
}
}
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/features/missions/MultiZonePicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export function MultiZonePicker({
{addButtonLabel}
</Button>

{!!meta.error && <ErrorMessage>Veuillez définir une zone</ErrorMessage>}
{!!meta.error && <ErrorMessage>{meta.error}</ErrorMessage>}

<>
{polygons.map((polygonCoordinates, index) => (
Expand Down

0 comments on commit c59a56a

Please sign in to comment.