-
Notifications
You must be signed in to change notification settings - Fork 1
/
mission-recognized-vessel.tsx
74 lines (63 loc) · 2.63 KB
/
mission-recognized-vessel.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import { MissionGeneralInfo } from '@common/types/mission-types.ts'
import { FormikEffect, FormikNumberInput } from '@mtes-mct/monitor-ui'
import { Formik } from 'formik'
import React, { useEffect, useRef, useState } from 'react'
import useAddOrUpdateGeneralInfo from '../../../hooks/use-add-update-distance-consumption.tsx'
import useIsMissionFinished from '@features/pam/mission/hooks/use-is-mission-finished.tsx'
const DEBOUNCE_TIME_TRIGGER = 1000
type RecognizedVessel = {
nbrOfRecognizedVessel?: number
}
interface MissionRecognizedVesselProps {
missionId: number
generalInfo?: MissionGeneralInfo
}
const MissionRecognizedVessel: React.FC<MissionRecognizedVesselProps> = ({ missionId, generalInfo }) => {
const [updateGeneralInfo] = useAddOrUpdateGeneralInfo()
const isMissionFinished = useIsMissionFinished(missionId?.toString())
const timerRef = useRef<ReturnType<typeof setTimeout>>()
const [initValue, setInitValue] = useState<RecognizedVessel>()
useEffect(() => {
setInitValue({ nbrOfRecognizedVessel: generalInfo?.nbrOfRecognizedVessel })
}, [generalInfo])
const handleSubmit = async ({ nbrOfRecognizedVessel }: RecognizedVessel): Promise<void> => {
clearTimeout(timerRef.current)
timerRef.current = setTimeout(() => updateRecognizedVessel(nbrOfRecognizedVessel), DEBOUNCE_TIME_TRIGGER)
}
const updateRecognizedVessel = async (nbrOfRecognizedVessel?: number) => {
const info = { ...generalInfo, missionId, nbrOfRecognizedVessel }
await updateGeneralInfo({
variables: { info }
})
}
const validateError = (isMissionFinished?: boolean, nbrOfRecognizedVessel?: number) =>
isMissionFinished && !nbrOfRecognizedVessel
? { nbrOfRecognizedVessel: 'Nombre total de navires reconnus dans les approches maritimes est requis' }
: undefined
return (
<>
{initValue && (
<Formik
initialValues={initValue}
initialErrors={validateError(isMissionFinished, initValue.nbrOfRecognizedVessel)}
onSubmit={handleSubmit}
validateOnChange={true}
validate={values => validateError(isMissionFinished, values.nbrOfRecognizedVessel)}
>
<>
<FormikEffect onChange={handleSubmit} />
<FormikNumberInput
isLight={false}
isRequired={true}
name="nbrOfRecognizedVessel"
data-testid="mission-information-general-recognized-vessel"
label="Nombre total de navires reconnus dans les approches maritimes (ZEE)"
isErrorMessageHidden={true}
/>
</>
</Formik>
)}
</>
)
}
export default MissionRecognizedVessel