-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d27c07b
commit 6aa9237
Showing
2 changed files
with
73 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
frontend/src/features/missions/MissionForm/hooks/useSyncFormValuesWithRedux.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { useAppDispatch } from '@hooks/useAppDispatch' | ||
import { useAppSelector } from '@hooks/useAppSelector' | ||
import { useFormikContext } from 'formik' | ||
import { useEffect } from 'react' | ||
import { useDebouncedCallback } from 'use-debounce' | ||
|
||
import { missionFormsActions } from '../slice' | ||
import { getIsMissionFormValid } from '../utils' | ||
|
||
import type { Mission } from 'domain/entities/missions' | ||
|
||
export function useSyncFormValuesWithRedux(isAutoSaveEnabled: boolean) { | ||
const dispatch = useAppDispatch() | ||
const { dirty, values } = useFormikContext<Mission>() | ||
const activeMissionId = useAppSelector(state => state.missionForms.activeMissionId) | ||
const activeMission = useAppSelector(state => | ||
activeMissionId ? state.missionForms.missions[activeMissionId] : undefined | ||
) | ||
const engagedControlUnit = useAppSelector(state => | ||
activeMissionId ? state.missionForms.missions[activeMissionId]?.engagedControlUnit : undefined | ||
) | ||
|
||
const dispatchFormUpdate = useDebouncedCallback(async (newValues: Mission) => { | ||
if (!newValues || newValues.id !== activeMissionId) { | ||
return | ||
} | ||
|
||
const isFormDirty = isMissionFormDirty() | ||
|
||
dispatch( | ||
missionFormsActions.setMission({ | ||
engagedControlUnit, | ||
isFormDirty, | ||
missionForm: newValues | ||
}) | ||
) | ||
}, 350) | ||
|
||
/** | ||
* The form is dirty if: | ||
* - In auto-save mode, an error is found (hence the form is not saved) | ||
* - In manual save mode, values have been modified (using the `dirty` props of Formik) | ||
*/ | ||
function isMissionFormDirty() { | ||
if (!isAutoSaveEnabled) { | ||
if (dirty) { | ||
return dirty | ||
} | ||
|
||
/** | ||
* If the form was already dirty and still open, the new `dirty` property is not valid anymore as Formik | ||
* has been re-instantiated with the saved values. | ||
* We use the last `isFormDirty` value instead of `dirty`. | ||
*/ | ||
return activeMission?.isFormDirty ?? false | ||
} | ||
|
||
const isMissionFormValid = getIsMissionFormValid(values) | ||
|
||
return !isMissionFormValid | ||
} | ||
|
||
useEffect(() => { | ||
dispatchFormUpdate(values) | ||
}, [values, dispatchFormUpdate]) | ||
} |