Skip to content

Commit

Permalink
fix: Synchronize field update with save
Browse files Browse the repository at this point in the history
  • Loading branch information
maximeperrault authored and maximeperraultdev committed Sep 19, 2024
1 parent 0d96e0d commit 16855a9
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 79 deletions.
11 changes: 1 addition & 10 deletions frontend/src/domain/use_cases/missions/switchTab.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { attachReportingToMissionSliceActions } from '../../../features/missions/MissionForm/AttachReporting/slice'
import { missionFormsActions } from '../../../features/missions/MissionForm/slice'
import { missionActions } from '../../../features/missions/slice'
import { sideWindowActions } from '../../../features/SideWindow/slice'
Expand All @@ -9,9 +8,7 @@ import type { HomeAppThunk } from '@store/index'

export const switchTab =
(path: string): HomeAppThunk =>
async (dispatch, getState) => {
const { missions } = getState().missionForms

async dispatch => {
const routeParams = getMissionPageRoute(path)
const id = getIdTyped(routeParams?.params.id)

Expand All @@ -28,10 +25,4 @@ export const switchTab =
dispatch(missionActions.setSelectedMissionIdOnMap(id))

dispatch(sideWindowActions.setCurrentPath(path))

// since we are switching to another mission, we need to update the attached reportings store
// because it's the form who listen to this store
dispatch(
attachReportingToMissionSliceActions.setAttachedReportings(missions[id]?.missionForm?.attachedReportings ?? [])
)
}
12 changes: 9 additions & 3 deletions frontend/src/features/missions/MissionForm/MissionForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import { ExternalActionsModal } from './ExternalActionsModal'
import { FormikSyncMissionFields } from './FormikSyncMissionFields'
import { GeneralInformationsForm } from './GeneralInformationsForm'
import { useMissionAndActionsCompletion } from './hooks/useMissionAndActionsCompletion'
import { useSyncFormValuesWithRedux } from './hooks/useSyncFormValuesWithRedux'
import { useUpdateOtherControlTypes } from './hooks/useUpdateOtherControlTypes'
import { useUpdateSurveillance } from './hooks/useUpdateSurveillance'
import { MissionFormBottomBar } from './MissionFormBottomBar'
Expand Down Expand Up @@ -99,7 +98,6 @@ export function MissionForm({

const isFormDirty = useMemo(() => selectedMissions[id]?.isFormDirty ?? false, [id, selectedMissions])

useSyncFormValuesWithRedux(isAutoSaveEnabled)
const { missionCompletionFrontStatus } = useMissionAndActionsCompletion()
useUpdateSurveillance()
useUpdateOtherControlTypes()
Expand Down Expand Up @@ -179,6 +177,14 @@ export function MissionForm({
}

const validateBeforeOnChange = useDebouncedCallback(async (nextValues, forceSave) => {
dispatch(
missionFormsActions.setMission({
engagedControlUnit,
isFormDirty,
missionForm: nextValues
})
)

if (!isAutoSaveEnabled || engagedControlUnit || !isMissionFormValid) {
return
}
Expand All @@ -188,7 +194,7 @@ export function MissionForm({
}

dispatch(saveMission(nextValues, false, false))
}, 300)
}, 150)

useEffect(() => {
if (isNewMission && !engagedControlUnit && previousEngagedControlUnit !== engagedControlUnit) {
Expand Down

This file was deleted.

7 changes: 7 additions & 0 deletions frontend/src/features/missions/MissionForm/index.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { useAppDispatch } from '@hooks/useAppDispatch'
import { Banner, Icon, Level, THEME } from '@mtes-mct/monitor-ui'
import { Form, Formik } from 'formik'
import { noop } from 'lodash'
import { useMemo } from 'react'
import styled from 'styled-components'

import { attachReportingToMissionSliceActions } from './AttachReporting/slice'
import { MissionForm } from './MissionForm'
import { MissionSchema } from './Schemas'
import { useAppSelector } from '../../../hooks/useAppSelector'
Expand All @@ -13,13 +15,18 @@ import { missionFactory } from '../Missions.helpers'
import type { Mission as MissionType, NewMission } from '../../../domain/entities/missions'

export function MissionFormWrapper() {
const dispatch = useAppDispatch()
const activeMissionId = useAppSelector(state => state.missionForms.activeMissionId)

const selectedMission = useAppSelector(state =>
activeMissionId ? state.missionForms.missions[activeMissionId] : undefined
)
const engagedControlUnit = selectedMission?.engagedControlUnit

dispatch(
attachReportingToMissionSliceActions.setAttachedReportings(selectedMission?.missionForm.attachedReportings ?? [])
)

const activeAction = selectedMission?.activeAction

const missionIsNewMission = useMemo(() => isNewMission(activeMissionId), [activeMissionId])
Expand Down

0 comments on commit 16855a9

Please sign in to comment.