Skip to content

Commit

Permalink
[Tech] clean front-end properties sent to the backend for all reporti…
Browse files Browse the repository at this point in the history
…ngs useCases and for update a controlUnit
  • Loading branch information
claire2212 committed Jun 20, 2024
1 parent f7179f5 commit 2f35a33
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 21 deletions.
14 changes: 10 additions & 4 deletions frontend/src/domain/use_cases/reporting/archiveReporting.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -15,8 +16,8 @@ export const createMissionFromReporting = (values: Reporting | Partial<Reporting
reportingFormVisibility: { context: reportingContext }
} = getState().global

const valuesToSave = omit(values, ['attachedMission'])
const newOrNextReportingData = isNewReporting(valuesToSave.id) ? { ...valuesToSave, id: undefined } : valuesToSave
const valuesToSave = omit(values, ...REPORTING_VALUES_TO_EXCLUDE_FOR_API)
const newOrNextReportingData = isNewReporting(values.id) ? { ...valuesToSave, id: undefined } : valuesToSave
const endpoint = reportingsAPI.endpoints.createReporting

try {
Expand Down
6 changes: 5 additions & 1 deletion frontend/src/domain/use_cases/reporting/reopenReporting.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { REPORTING_VALUES_TO_EXCLUDE_FOR_API } from '@features/Reportings/constants'
import { omit } from 'lodash'

import { reportingsAPI } from '../../../api/reportingsAPI'
import { setToast, ReportingContext } from '../../shared_slices/Global'
import { reportingActions } from '../../shared_slices/reporting'
Expand All @@ -8,7 +11,8 @@ export const reopenReporting =
(values: Reporting, reportingContext: ReportingContext) => 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],
Expand Down
12 changes: 2 additions & 10 deletions frontend/src/domain/use_cases/reporting/saveReporting.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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 (
<MapMenuDialog.Container>
Expand All @@ -64,9 +82,9 @@ export function ControlUnitDialog() {
<Button accent={Accent.SECONDARY} Icon={Icon.Plus} isFullWidth onClick={openNewMission}>
Créer une mission avec cette unité
</Button>
<ControlUnitContactList controlUnit={controlUnit} onSubmit={updateControlUnit} />
<ControlUnitContactList controlUnit={controlUnit} onSubmit={update} />
<ControlUnitResourceList controlUnit={controlUnit} />
<AreaNote controlUnit={controlUnit} onSubmit={updateControlUnit} />
<AreaNote controlUnit={controlUnit} onSubmit={update} />
</StyledMapMenuDialogBody>
</Formik>
</Wrapper>
Expand Down
8 changes: 8 additions & 0 deletions frontend/src/features/Reportings/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export const REPORTING_VALUES_TO_EXCLUDE_FOR_API = [
'attachedMission',
'semaphore',
'controlUnit',
'displayedSource',
'seaFront',
'controlStatus'
]
12 changes: 11 additions & 1 deletion frontend/src/features/Station/components/StationForm/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 2f35a33

Please sign in to comment.