Skip to content

Commit

Permalink
fix(coords): issue over coords toFixed(2)
Browse files Browse the repository at this point in the history
 - 2 number over the decimal only for every coords
  • Loading branch information
xtiannyeto committed Dec 25, 2024
1 parent 13606a5 commit c37869b
Show file tree
Hide file tree
Showing 10 changed files with 34 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ const MissionPageHeaderWrapper: React.FC<MissionPageHeaderProps> = ({
onClickExport,
exportLoading
}) => {
//const exportRapportEnabled = useIsMissionCompleteForStats(mission?.id)
const { formatMissionName } = useDate()
const exportRapportEnabled = mission?.completenessForStats?.status === CompletenessForStatsStatusEnum.COMPLETE

Expand Down
10 changes: 8 additions & 2 deletions frontend/src/v2/features/common/hooks/use-coordinate.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
interface CoordinateHook {
getCoords: (lat?: number, lng?: number) => [number?, number?]
extractLatLngFromMultiPoint: (value?: string) => [number?, number?]
}

Expand All @@ -12,14 +13,19 @@ export function useCoordinate(): CoordinateHook {
const latitude = parseFloat(match[2])
const longitude = parseFloat(match[1])
if (!isNaN(latitude) && !isNaN(longitude)) {
lat = latitude
lng = longitude
lat = Number(latitude.toFixed(2))
lng = Number(longitude.toFixed(2))
}
}
return [lat, lng]
}

const getCoords = (lat?: number, lng?: number): [number?, number?] => {
return [lat ? Number(lat.toFixed(2)) : 0, lng ? Number(lng.toFixed(2)) : 0]
}

return {
getCoords,
extractLatLngFromMultiPoint
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,6 @@ const MissionActionItemEnvControl: React.FC<MissionActionItemEnvControlProps> =
<MissionInfractionEnvList
name="infractions"
fieldArray={fieldArray}
vehicleType={values.vehicleType}
actionTargetType={values.actionTargetType}
availableControlTypes={values.availableControlTypesForInfraction}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { FormikEffect, FormikTextarea } from '@mtes-mct/monitor-ui'
import { Formik } from 'formik'
import { FC } from 'react'
import { Stack } from 'rsuite'
import { MissionActionOutput } from 'src/v2/features/common/types/mission-action-output'
import { FormikSelectVesselSize } from '../../../common/components/ui/formik-select-vessel-size'
import { MissionActionOutput } from '../../../common/types/mission-action-output'
import MissionControlNavForm from '../../../mission-control/components/elements/mission-control-nav-form'
import MissionControlNavSummary from '../../../mission-control/components/ui/mission-control-nav-summary'
import { useMissionActionNavControl } from '../../hooks/use-mission-action-nav-control'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { FormikErrors } from 'formik'
import { useAbstractFormik } from '../../common/hooks/use-abstract-formik-form'
import { useCoordinate } from '../../common/hooks/use-coordinate'
import { useDate } from '../../common/hooks/use-date'
import { AbstractFormikSubFormHook } from '../../common/types/abstract-formik-hook'
import { MissionActionOutput } from '../../common/types/mission-action-output'
Expand All @@ -10,13 +11,14 @@ export function useMissionActionAntiPollution(
action: MissionActionOutput,
onChange: (newAction: MissionActionOutput) => Promise<unknown>
): AbstractFormikSubFormHook<ActionAntiPollutionInput> {
const { getCoords } = useCoordinate()
const value = action?.data as MissionNavActionDataOutput
const { preprocessDateForPicker, postprocessDateFromPicker } = useDate()

const fromFieldValueToInput = (data: MissionNavActionDataOutput): ActionAntiPollutionInput => {
const endDate = preprocessDateForPicker(data.endDateTimeUtc)
const startDate = preprocessDateForPicker(data.startDateTimeUtc)
return { ...data, dates: [startDate, endDate], geoCoords: [data.latitude, data.longitude] }
return { ...data, dates: [startDate, endDate], geoCoords: getCoords(data.latitude, data.longitude) }
}

const fromInputToFieldValue = (value: ActionAntiPollutionInput): MissionNavActionDataOutput => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { FormikErrors } from 'formik'
import { useAbstractFormik } from '../../common/hooks/use-abstract-formik-form'
import { useCoordinate } from '../../common/hooks/use-coordinate'
import { useDate } from '../../common/hooks/use-date'
import { AbstractFormikSubFormHook } from '../../common/types/abstract-formik-hook'
import { MissionActionOutput } from '../../common/types/mission-action-output'
Expand All @@ -11,6 +12,7 @@ export function useMissionActionFishControl(
onChange: (newAction: MissionActionOutput, debounceTime?: number) => Promise<unknown>,
isMissionFinished?: boolean
): AbstractFormikSubFormHook<ActionFishControlInput> {
const { getCoords } = useCoordinate()
const value = action?.data as MissionFishActionDataOutput
const { preprocessDateForPicker, postprocessDateFromPicker } = useDate()

Expand All @@ -21,7 +23,7 @@ export function useMissionActionFishControl(
...data,
dates: [startDate, endDate],
isMissionFinished: !!isMissionFinished,
geoCoords: [data.latitude, data.longitude] //TODO: Check coords 2 number after the comma unless, it will trigger a diff
geoCoords: getCoords(data.latitude, data.longitude)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { FormikErrors } from 'formik'
import { useAbstractFormik } from '../../common/hooks/use-abstract-formik-form'
import { useDate } from '../../common/hooks/use-date'
import { AbstractFormikSubFormHook } from '../../common/types/abstract-formik-hook'
import { MissionActionDataOutput, MissionActionOutput } from '../../common/types/mission-action-output'
import { MissionActionData } from '../../common/types/mission-action-data'
import { MissionActionOutput } from '../../common/types/mission-action-output'
import { ActionGenericDateObservationInput } from '../types/action-type'

export function useMissionActionGenericDateObservation(
Expand All @@ -11,25 +12,26 @@ export function useMissionActionGenericDateObservation(
): AbstractFormikSubFormHook<ActionGenericDateObservationInput> {
const { preprocessDateForPicker, postprocessDateFromPicker } = useDate()

const fromFieldValueToInput = (data: MissionActionDataOutput): ActionGenericDateObservationInput => {
const fromFieldValueToInput = (data: MissionActionData): ActionGenericDateObservationInput => {
const endDate = preprocessDateForPicker(data.endDateTimeUtc)
const startDate = preprocessDateForPicker(data.startDateTimeUtc)
return { ...data, dates: [startDate, endDate] }
}

const fromInputToFieldValue = (value: ActionGenericDateObservationInput): MissionActionDataOutput => {
const fromInputToFieldValue = (value: ActionGenericDateObservationInput): MissionActionData => {
const { dates, ...newData } = value
const endDateTimeUtc = postprocessDateFromPicker(dates[1])
const startDateTimeUtc = postprocessDateFromPicker(dates[0])
return { ...newData, startDateTimeUtc, endDateTimeUtc }
}

const { initValue, handleSubmit, isError } = useAbstractFormik<
MissionActionDataOutput,
ActionGenericDateObservationInput
>(action.data, fromFieldValueToInput, fromInputToFieldValue)
const { initValue, handleSubmit, isError } = useAbstractFormik<MissionActionData, ActionGenericDateObservationInput>(
action.data,
fromFieldValueToInput,
fromInputToFieldValue
)

const onSubmit = async (valueToSubmit?: MissionActionDataOutput) => {
const onSubmit = async (valueToSubmit?: MissionActionData) => {
if (!valueToSubmit) return
await onChange({ ...action, data: valueToSubmit })
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { FormikErrors } from 'formik'
import { boolean, number, object } from 'yup'
import { useAbstractFormik } from '../../common/hooks/use-abstract-formik-form'
import { useCoordinate } from '../../common/hooks/use-coordinate'
import { useDate } from '../../common/hooks/use-date'
import { AbstractFormikSubFormHook } from '../../common/types/abstract-formik-hook'
import { MissionActionOutput } from '../../common/types/mission-action-output'
Expand All @@ -12,6 +13,7 @@ export function useMissionActionIllegalImmigration(
onChange: (newAction: MissionActionOutput) => Promise<unknown>,
isMissionFinished?: boolean
): AbstractFormikSubFormHook<ActionIllegalImmigrationInput> {
const { getCoords } = useCoordinate()
const value = action?.data as MissionNavActionDataOutput
const { preprocessDateForPicker, postprocessDateFromPicker } = useDate()

Expand All @@ -22,7 +24,7 @@ export function useMissionActionIllegalImmigration(
...data,
dates: [startDate, endDate],
isMissionFinished: !!isMissionFinished,
geoCoords: [data.latitude, data.longitude]
geoCoords: getCoords(data.latitude, data.longitude)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { VesselSizeEnum } from '@common/types/env-mission-types'
import { FormikErrors } from 'formik'
import { boolean, mixed, object, string } from 'yup'
import { useAbstractFormik } from '../../common/hooks/use-abstract-formik-form'
import { useCoordinate } from '../../common/hooks/use-coordinate'
import { useDate } from '../../common/hooks/use-date'
import { AbstractFormikSubFormHook } from '../../common/types/abstract-formik-hook'
import { MissionActionOutput } from '../../common/types/mission-action-output'
Expand All @@ -13,6 +14,7 @@ export function useMissionActionNavControl(
onChange: (newAction: MissionActionOutput) => Promise<unknown>,
isMissionFinished?: boolean
): AbstractFormikSubFormHook<ActionNavControlInput> {
const { getCoords } = useCoordinate()
const value = action?.data as MissionNavActionDataOutput
const { preprocessDateForPicker, postprocessDateFromPicker } = useDate()

Expand All @@ -23,7 +25,7 @@ export function useMissionActionNavControl(
...data,
dates: [startDate, endDate],
isMissionFinished: !!isMissionFinished,
geoCoords: [data.latitude, data.longitude]
geoCoords: getCoords(data.latitude, data.longitude)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { FormikErrors } from 'formik'
import { array, boolean, date, number, object } from 'yup'
import { useAbstractFormik } from '../../common/hooks/use-abstract-formik-form'
import { useCoordinate } from '../../common/hooks/use-coordinate'
import { useDate } from '../../common/hooks/use-date'
import { AbstractFormikSubFormHook } from '../../common/types/abstract-formik-hook'
import { MissionActionOutput } from '../../common/types/mission-action-output'
Expand All @@ -13,6 +14,7 @@ export function useMissionActionRescue(
onChange: (newAction: MissionActionOutput) => Promise<unknown>,
isMissionFinished?: boolean
): AbstractFormikSubFormHook<ActionRescueInput> {
const { getCoords } = useCoordinate()
const value = action?.data as MissionNavActionDataOutput
const { preprocessDateForPicker, postprocessDateFromPicker } = useDate()

Expand All @@ -25,7 +27,7 @@ export function useMissionActionRescue(
rescueType,
dates: [startDate, endDate],
isMissionFinished: !!isMissionFinished,
geoCoords: [data.latitude ?? 0, data.longitude ?? 0]
geoCoords: getCoords(data.latitude, data.longitude)
}
}

Expand Down

0 comments on commit c37869b

Please sign in to comment.