From a8d0a8e306c3aaf8bf86dcc2e27455e108cfda02 Mon Sep 17 00:00:00 2001 From: Claire Dagan Date: Thu, 12 Oct 2023 10:25:32 +0200 Subject: [PATCH] [Mission] save missionState when nagivate from map to sidewindow --- .../mission_list/missions_navigation.spec.ts | 4 +++ .../{navigation => missions}/deleteTab.ts | 0 .../missions/saveMissionInLocalStore.ts | 27 +++++++++++++++++++ .../{navigation => missions}/switchTab.ts | 0 .../ReportingsButton/SearchReportings.tsx | 4 ++- frontend/src/features/SideWindow/index.tsx | 15 ++++++++--- .../missions/MissionsButton/index.tsx | 8 ++++-- .../src/features/missions/MissionsNavBar.tsx | 12 +++++---- frontend/src/utils/routes.ts | 9 ++++--- 9 files changed, 64 insertions(+), 15 deletions(-) rename frontend/src/domain/use_cases/{navigation => missions}/deleteTab.ts (100%) create mode 100644 frontend/src/domain/use_cases/missions/saveMissionInLocalStore.ts rename frontend/src/domain/use_cases/{navigation => missions}/switchTab.ts (100%) diff --git a/frontend/cypress/e2e/side_window/mission_list/missions_navigation.spec.ts b/frontend/cypress/e2e/side_window/mission_list/missions_navigation.spec.ts index 0fba8eaef3..58732a1967 100644 --- a/frontend/cypress/e2e/side_window/mission_list/missions_navigation.spec.ts +++ b/frontend/cypress/e2e/side_window/mission_list/missions_navigation.spec.ts @@ -29,6 +29,10 @@ context('Mission', () => { cy.get('[data-cy="mission-1"] > svg').first().click({ force: true }).wait(250) cy.clickButton("Retourner à l'édition") + // navigate between reportings and missions + cy.clickButton('signalements') + cy.clickButton('missions') + // we want to check whether the second mission has been saved correctly cy.get('[data-cy="mission-2"]').first().forceClick().wait(250) cy.get('*[data-cy="add-control-administration"]').contains('DIRM / DM') diff --git a/frontend/src/domain/use_cases/navigation/deleteTab.ts b/frontend/src/domain/use_cases/missions/deleteTab.ts similarity index 100% rename from frontend/src/domain/use_cases/navigation/deleteTab.ts rename to frontend/src/domain/use_cases/missions/deleteTab.ts diff --git a/frontend/src/domain/use_cases/missions/saveMissionInLocalStore.ts b/frontend/src/domain/use_cases/missions/saveMissionInLocalStore.ts new file mode 100644 index 0000000000..eeeafeffbd --- /dev/null +++ b/frontend/src/domain/use_cases/missions/saveMissionInLocalStore.ts @@ -0,0 +1,27 @@ +import { multiMissionsActions } from '../../shared_slices/MultiMissions' + +export const saveMissionInLocalStore = () => async (dispatch, getState) => { + const { + missionState: { isFormDirty, missionState }, + multiMissions: { selectedMissions } + } = getState() + if (!missionState) { + return + } + + const missionsUpdated = [...selectedMissions] + const missionIndex = missionsUpdated.findIndex(mission => mission.mission.id === missionState?.id) + + // We want to save the active form before navigate to another page in side window + const missionFormatted = { + isFormDirty, + mission: missionState + } + if (missionIndex !== -1) { + missionsUpdated[missionIndex] = missionFormatted + } else { + missionsUpdated.push(missionFormatted) + } + + await dispatch(multiMissionsActions.setSelectedMissions(missionsUpdated)) +} diff --git a/frontend/src/domain/use_cases/navigation/switchTab.ts b/frontend/src/domain/use_cases/missions/switchTab.ts similarity index 100% rename from frontend/src/domain/use_cases/navigation/switchTab.ts rename to frontend/src/domain/use_cases/missions/switchTab.ts diff --git a/frontend/src/features/Reportings/ReportingsButton/SearchReportings.tsx b/frontend/src/features/Reportings/ReportingsButton/SearchReportings.tsx index dc513d2864..f064d3a5be 100644 --- a/frontend/src/features/Reportings/ReportingsButton/SearchReportings.tsx +++ b/frontend/src/features/Reportings/ReportingsButton/SearchReportings.tsx @@ -4,6 +4,7 @@ import styled from 'styled-components' import { sideWindowPaths } from '../../../domain/entities/sideWindow' import { setDisplayedItems, ReportingContext } from '../../../domain/shared_slices/Global' +import { saveMissionInLocalStore } from '../../../domain/use_cases/missions/saveMissionInLocalStore' import { addReporting } from '../../../domain/use_cases/reporting/addReporting' import { useAppSelector } from '../../../hooks/useAppSelector' import { sideWindowActions } from '../../SideWindow/slice' @@ -28,7 +29,8 @@ export function SearchReportings() { dispatch(addReporting(ReportingContext.MAP)) } - const toggleReportingsWindow = () => { + const toggleReportingsWindow = async () => { + await dispatch(saveMissionInLocalStore()) dispatch(sideWindowActions.focusAndGoTo(sideWindowPaths.REPORTINGS)) } diff --git a/frontend/src/features/SideWindow/index.tsx b/frontend/src/features/SideWindow/index.tsx index ad07d3e262..1e1af20e6f 100644 --- a/frontend/src/features/SideWindow/index.tsx +++ b/frontend/src/features/SideWindow/index.tsx @@ -10,8 +10,9 @@ import { StyledRouteContainer, Wrapper } from './style' import { ErrorBoundary } from '../../components/ErrorBoundary' import { sideWindowPaths } from '../../domain/entities/sideWindow' import { ReportingContext } from '../../domain/shared_slices/Global' +import { switchTab } from '../../domain/use_cases/missions/switchTab' import { useAppSelector } from '../../hooks/useAppSelector' -import { isMissionOrMissionsPage, isReportingsPage } from '../../utils/routes' +import { isMissionOrMissionsPage, isMissionPage, isReportingsPage } from '../../utils/routes' import { Mission } from '../missions/MissionForm' import { Missions } from '../missions/MissionsList' import { MissionsNavBar } from '../missions/MissionsNavBar' @@ -34,7 +35,13 @@ function SideWindowWithRef(_, ref: ForwardedRef) { const isReportingsButtonIsActive = useMemo(() => isReportingsPage(currentPath), [currentPath]) const navigate = nextPath => { - if (nextPath) { + if (!nextPath) { + return + } + const isCurrentPathIsMissionPage = isMissionPage(currentPath) + if (isCurrentPathIsMissionPage) { + dispatch(switchTab(nextPath)) + } else { dispatch(sideWindowActions.setCurrentPath(nextPath)) } } @@ -83,7 +90,9 @@ function SideWindowWithRef(_, ref: ForwardedRef) { )} - + {isReportingsButtonIsActive && ( + + )} diff --git a/frontend/src/features/missions/MissionsButton/index.tsx b/frontend/src/features/missions/MissionsButton/index.tsx index 01d476a292..6854036ad3 100644 --- a/frontend/src/features/missions/MissionsButton/index.tsx +++ b/frontend/src/features/missions/MissionsButton/index.tsx @@ -6,6 +6,7 @@ import styled from 'styled-components' import { sideWindowPaths } from '../../../domain/entities/sideWindow' import { setDisplayedItems, ReportingContext, VisibilityState } from '../../../domain/shared_slices/Global' import { addMission } from '../../../domain/use_cases/missions/addMission' +import { saveMissionInLocalStore } from '../../../domain/use_cases/missions/saveMissionInLocalStore' import { reduceReportingFormOnMap } from '../../../domain/use_cases/reporting/reduceReportingFormOnMap' import { useAppSelector } from '../../../hooks/useAppSelector' import { isMissionOrMissionsPage } from '../../../utils/routes' @@ -24,12 +25,15 @@ export function MissionsMenu() { [sideWindow.currentPath, sideWindow.status] ) - const toggleMissionsWindow = () => { - dispatch(sideWindowActions.focusAndGoTo(sideWindowPaths.MISSIONS)) + const toggleMissionsWindow = async () => { + await dispatch(saveMissionInLocalStore()) + await dispatch(sideWindowActions.focusAndGoTo(sideWindowPaths.MISSIONS)) } + const toggleMissionsLayer = () => { dispatch(setDisplayedItems({ displayMissionsLayer: !displayMissionsLayer })) } + const toggleMissionsMenu = e => { e.preventDefault() dispatch( diff --git a/frontend/src/features/missions/MissionsNavBar.tsx b/frontend/src/features/missions/MissionsNavBar.tsx index 31c8f935aa..f9a0f6b319 100644 --- a/frontend/src/features/missions/MissionsNavBar.tsx +++ b/frontend/src/features/missions/MissionsNavBar.tsx @@ -6,8 +6,8 @@ import styled from 'styled-components' import { getMissionStatus, missionStatusLabels } from '../../domain/entities/missions' import { sideWindowPaths } from '../../domain/entities/sideWindow' -import { deleteTab } from '../../domain/use_cases/navigation/deleteTab' -import { switchTab } from '../../domain/use_cases/navigation/switchTab' +import { deleteTab } from '../../domain/use_cases/missions/deleteTab' +import { switchTab } from '../../domain/use_cases/missions/switchTab' import { useAppDispatch } from '../../hooks/useAppDispatch' import { useAppSelector } from '../../hooks/useAppSelector' import { getMissionTitle } from '../../utils/getMissionTitle' @@ -53,15 +53,17 @@ export function MissionsNavBar() { }, [selectedMissions]) const selectTab = nextPath => { - if (nextPath) { - dispatch(switchTab(nextPath)) + if (!nextPath) { + return } + dispatch(switchTab(nextPath)) } const removeTab = nextPath => { if (nextPath) { - dispatch(deleteTab(nextPath)) + return } + dispatch(deleteTab(nextPath)) } return ( diff --git a/frontend/src/utils/routes.ts b/frontend/src/utils/routes.ts index 59df98bf2c..57a0b2db7e 100644 --- a/frontend/src/utils/routes.ts +++ b/frontend/src/utils/routes.ts @@ -11,16 +11,17 @@ export function getMissionPageRoute(path: string) { path as string ) } - -export function isMissionOrMissionsPage(path: string) { - const isMissionPage = !!matchPath<'id', string>( +export function isMissionPage(path: string) { + return !!matchPath<'id', string>( { end: true, path: sideWindowPaths.MISSION }, path as string ) +} +export function isMissionOrMissionsPage(path: string) { const isMissionsPage = !!matchPath( { end: true, @@ -29,7 +30,7 @@ export function isMissionOrMissionsPage(path: string) { path as string ) - return isMissionPage || isMissionsPage + return isMissionPage(path) || isMissionsPage } export function isReportingsPage(path: string) {