From 3ac1a232ac946f07c28b129242326e240b0f1887 Mon Sep 17 00:00:00 2001 From: Claire Dagan Date: Thu, 20 Jun 2024 09:40:53 +0200 Subject: [PATCH] [Tech] clean front-end properties sent to the backend for all reportings useCases and for update a controlUnit --- .../use_cases/reporting/archiveReporting.ts | 14 +++++++---- .../reporting/createMissionFromReporting.ts | 5 ++-- .../use_cases/reporting/reopenReporting.ts | 6 ++++- .../use_cases/reporting/saveReporting.ts | 12 ++-------- .../components/ControlUnitDialog/index.tsx | 24 ++++++++++++++++--- frontend/src/features/Reportings/constants.ts | 8 +++++++ .../Station/components/StationForm/utils.ts | 12 +++++++++- 7 files changed, 60 insertions(+), 21 deletions(-) create mode 100644 frontend/src/features/Reportings/constants.ts diff --git a/frontend/src/domain/use_cases/reporting/archiveReporting.ts b/frontend/src/domain/use_cases/reporting/archiveReporting.ts index a1c163674..8e062985d 100644 --- a/frontend/src/domain/use_cases/reporting/archiveReporting.ts +++ b/frontend/src/domain/use_cases/reporting/archiveReporting.ts @@ -1,8 +1,11 @@ +import { REPORTING_VALUES_TO_EXCLUDE_FOR_API } from '@features/Reportings/constants' +import { omit } from 'lodash' + import { reportingsAPI } from '../../../api/reportingsAPI' import { setToast } from '../../shared_slices/Global' import { reportingActions } from '../../shared_slices/reporting' -import type { Reporting } from '../../entities/reporting' +import type { ReportingData } from '../../entities/reporting' export const archiveReportingFromTable = (id: number) => async (dispatch, getState) => { const { @@ -17,9 +20,12 @@ export const archiveReportingFromTable = (id: number) => async (dispatch, getSta reportingToArchive = reporting } - const response = await dispatch( - reportingsAPI.endpoints.updateReporting.initiate({ ...(reportingToArchive as Reporting), isArchived: true }) - ) + const valuesToUpdate = { + ...omit(reportingToArchive, ...REPORTING_VALUES_TO_EXCLUDE_FOR_API), + isArchived: true + } as ReportingData + + const response = await dispatch(reportingsAPI.endpoints.updateReporting.initiate(valuesToUpdate)) if ('error' in response) { throw Error("Erreur à l'archivage du signalement") } else { diff --git a/frontend/src/domain/use_cases/reporting/createMissionFromReporting.ts b/frontend/src/domain/use_cases/reporting/createMissionFromReporting.ts index 342b892da..f8c709aaf 100644 --- a/frontend/src/domain/use_cases/reporting/createMissionFromReporting.ts +++ b/frontend/src/domain/use_cases/reporting/createMissionFromReporting.ts @@ -1,3 +1,4 @@ +import { REPORTING_VALUES_TO_EXCLUDE_FOR_API } from '@features/Reportings/constants' import omit from 'lodash/omit' import { reportingsAPI } from '../../../api/reportingsAPI' @@ -15,8 +16,8 @@ export const createMissionFromReporting = (values: Reporting | Partial async (dispatch, getState) => { const { reportings } = getState().reporting try { - const response = await dispatch(reportingsAPI.endpoints.updateReporting.initiate(values)) + const valuesToUpdate = omit(values, ...REPORTING_VALUES_TO_EXCLUDE_FOR_API) + const response = await dispatch(reportingsAPI.endpoints.updateReporting.initiate(valuesToUpdate)) if ('data' in response) { const updatedReporting = { ...reportings[response.data.id], diff --git a/frontend/src/domain/use_cases/reporting/saveReporting.ts b/frontend/src/domain/use_cases/reporting/saveReporting.ts index 6e22635f9..4daa96169 100644 --- a/frontend/src/domain/use_cases/reporting/saveReporting.ts +++ b/frontend/src/domain/use_cases/reporting/saveReporting.ts @@ -1,3 +1,4 @@ +import { REPORTING_VALUES_TO_EXCLUDE_FOR_API } from '@features/Reportings/constants' import omit from 'lodash/omit' import { reportingsAPI } from '../../../api/reportingsAPI' @@ -10,19 +11,10 @@ import { MapInteractionListenerEnum, updateMapInteractionListeners } from '../ma import type { Reporting, ReportingData } from '../../entities/reporting' -const REPORTING_VALUES_TO_EXCLUDE = [ - 'attachedMission', - 'semaphore', - 'controlUnit', - 'displayedSource', - 'seaFront', - 'controlStatus' -] - export const saveReporting = (values: Reporting, reportingContext: ReportingContext, quitAfterSave = false) => async dispatch => { - const valuesToSave = omit(values, ...REPORTING_VALUES_TO_EXCLUDE) + const valuesToSave = omit(values, ...REPORTING_VALUES_TO_EXCLUDE_FOR_API) const reportingIsNew = isNewReporting(values.id) const reportingId = reportingIsNew ? undefined : Number(values.id) diff --git a/frontend/src/features/ControlUnit/components/ControlUnitDialog/index.tsx b/frontend/src/features/ControlUnit/components/ControlUnitDialog/index.tsx index 697aa0aa1..27eb6f00d 100644 --- a/frontend/src/features/ControlUnit/components/ControlUnitDialog/index.tsx +++ b/frontend/src/features/ControlUnit/components/ControlUnitDialog/index.tsx @@ -1,5 +1,6 @@ -import { Accent, Button, Icon, MapMenuDialog } from '@mtes-mct/monitor-ui' +import { Accent, Button, ControlUnit, Icon, MapMenuDialog } from '@mtes-mct/monitor-ui' import { Formik } from 'formik' +import { omit } from 'lodash' import { noop } from 'lodash/fp' import { useCallback } from 'react' import styled from 'styled-components' @@ -40,6 +41,23 @@ export function ControlUnitDialog() { dispatch(mainWindowActions.setHasFullHeightRightDialogOpen(false)) }, [dispatch]) + const update = useCallback( + async nextControlUnit => { + const valuesToSave = omit(nextControlUnit, [ + 'administration', + 'coordinates', + 'controlUnitResourceIds', + 'controlUnitResources', + 'controlUnitContactIds', + 'controlUnitContacts', + 'departmentArea' + ]) as ControlUnit.ControlUnitData + + await updateControlUnit(valuesToSave) + }, + [updateControlUnit] + ) + if (!controlUnit) { return ( @@ -64,9 +82,9 @@ export function ControlUnitDialog() { - + - + diff --git a/frontend/src/features/Reportings/constants.ts b/frontend/src/features/Reportings/constants.ts new file mode 100644 index 000000000..c3c09b4be --- /dev/null +++ b/frontend/src/features/Reportings/constants.ts @@ -0,0 +1,8 @@ +export const REPORTING_VALUES_TO_EXCLUDE_FOR_API = [ + 'attachedMission', + 'semaphore', + 'controlUnit', + 'displayedSource', + 'seaFront', + 'controlStatus' +] diff --git a/frontend/src/features/Station/components/StationForm/utils.ts b/frontend/src/features/Station/components/StationForm/utils.ts index 464543953..d63409cb2 100644 --- a/frontend/src/features/Station/components/StationForm/utils.ts +++ b/frontend/src/features/Station/components/StationForm/utils.ts @@ -5,7 +5,17 @@ import type { Station } from '../../../../domain/entities/station' export function getStationDataFromStationFormValues(baseFormValues: StationFormValues): Station.StationData { return { - ...omit(['coordinates', 'controlUnitResourceIds', 'controlUnitResources'], baseFormValues), + ...omit( + [ + 'administration', + 'coordinates', + 'controlUnitResourceIds', + 'controlUnitResources', + 'controlUnitResourceIds', + 'controlUnitResources' + ], + baseFormValues + ), latitude: baseFormValues.coordinates![0], longitude: baseFormValues.coordinates![1] } as Station.StationData