diff --git a/frontend/src/api/APIWorker.tsx b/frontend/src/api/APIWorker.tsx index 42879db593..a2adc0bced 100644 --- a/frontend/src/api/APIWorker.tsx +++ b/frontend/src/api/APIWorker.tsx @@ -1,3 +1,6 @@ +import { getAllBeaconMalfunctions } from '@features/BeaconMalfunction/useCases/getAllBeaconMalfunctions' +import { getVesselBeaconMalfunctions } from '@features/BeaconMalfunction/useCases/getVesselBeaconMalfunctions' +import { openBeaconMalfunctionInKanban } from '@features/BeaconMalfunction/useCases/openBeaconMalfunctionInKanban' import { fleetSegmentApi } from '@features/FleetSegment/apis' import { getAllRegulatoryLayers } from '@features/Regulation/useCases/getAllRegulatoryLayers' import { reportingApi } from '@features/Reporting/reportingApi' @@ -9,9 +12,6 @@ import { VesselSidebarTab } from '../domain/entities/vessel/vessel' import { setIsUpdatingVessels } from '../domain/shared_slices/Global' import { getOperationalAlerts } from '../domain/use_cases/alert/getOperationalAlerts' import { getSilencedAlerts } from '../domain/use_cases/alert/getSilencedAlerts' -import { getAllBeaconMalfunctions } from '../domain/use_cases/beaconMalfunction/getAllBeaconMalfunctions' -import { getVesselBeaconMalfunctions } from '../domain/use_cases/beaconMalfunction/getVesselBeaconMalfunctions' -import { openBeaconMalfunctionInKanban } from '../domain/use_cases/beaconMalfunction/openBeaconMalfunctionInKanban' import { getAllGearCodes } from '../domain/use_cases/gearCode/getAllGearCodes' import { getInfractions } from '../domain/use_cases/infraction/getInfractions' import { getVesselControls } from '../domain/use_cases/mission/getVesselControls' diff --git a/frontend/src/api/beaconMalfunction.ts b/frontend/src/api/beaconMalfunction.ts deleted file mode 100644 index 3eaa6dc191..0000000000 --- a/frontend/src/api/beaconMalfunction.ts +++ /dev/null @@ -1,133 +0,0 @@ -import { FrontendApiError } from '@libs/FrontendApiError' - -import { monitorfishApiKy } from './api' - -import type { NOTIFICATION_TYPE, UserType } from '@features/BeaconMalfunction/constants' -import type { - BeaconMalfunction, - BeaconMalfunctionResumeAndDetails, - UpdateBeaconMalfunction, - VesselBeaconMalfunctionsResumeAndHistory -} from '@features/BeaconMalfunction/types' -import type { Vessel } from '@features/Vessel/Vessel.types' - -export const ARCHIVE_BEACON_MALFUNCTION = "Nous n'avons pas pu archiver les avaries VMS" -export const GET_BEACON_MALFUNCTIONS_ERROR_MESSAGE = "Nous n'avons pas pu récupérer les avaries VMS" -export const GET_BEACON_MALFUNCTION_ERROR_MESSAGE = "Nous n'avons pas pu récupérer l'avarie VMS" -export const UPDATE_BEACON_MALFUNCTIONS_ERROR_MESSAGE = "Nous n'avons pas pu mettre à jour le statut de l'avarie VMS" -export const SAVE_BEACON_MALFUNCTION_COMMENT_ERROR_MESSAGE = - "Nous n'avons pas pu ajouter le commentaire sur l'avarie VMS" -export const GET_VESSEL_BEACON_MALFUNCTIONS_ERROR_MESSAGE = "Nous n'avons pas pu récupérer les avaries de ce navire" -export const SEND_NOTIFICATION_ERROR_MESSAGE = "Nous n'avons pas pu envoyer la notification" - -/** - * Get all beacon malfunctions - * - * @throws {@link FrontendApiError} - */ -async function getAllBeaconMalfunctionsFromAPI(): Promise { - try { - return await monitorfishApiKy.get('/bff/v1/beacon_malfunctions').json() - } catch (err) { - throw new FrontendApiError(GET_BEACON_MALFUNCTIONS_ERROR_MESSAGE, (err as FrontendApiError).originalError) - } -} - -/** - * Update a beacon malfunction - * - * @throws {@link FrontendApiError} - */ -async function updateBeaconMalfunctionFromAPI( - id: number, - updatedFields: UpdateBeaconMalfunction -): Promise { - try { - return await monitorfishApiKy - .put(`/bff/v1/beacon_malfunctions/${id}`, { - json: updatedFields - }) - .json() - } catch (err) { - throw new FrontendApiError(UPDATE_BEACON_MALFUNCTIONS_ERROR_MESSAGE, (err as FrontendApiError).originalError) - } -} - -/** - * Get a beacon malfunction - * - * @throws {@link FrontendApiError} - */ -async function getBeaconMalfunctionFromAPI(id: number): Promise { - try { - return await monitorfishApiKy.get(`/bff/v1/beacon_malfunctions/${id}`).json() - } catch (err) { - throw new FrontendApiError(GET_BEACON_MALFUNCTION_ERROR_MESSAGE, (err as FrontendApiError).originalError) - } -} - -/** - * Save a new comment attached to a beacon malfunction - * - * @throws {@link FrontendApiError} - */ -async function saveBeaconMalfunctionCommentFromAPI( - id: number, - comment: { comment: string; userType: keyof typeof UserType } -): Promise { - try { - return await monitorfishApiKy - .post(`/bff/v1/beacon_malfunctions/${id}/comments`, { - json: comment - }) - .json() - } catch (err) { - throw new FrontendApiError(SAVE_BEACON_MALFUNCTION_COMMENT_ERROR_MESSAGE, (err as FrontendApiError).originalError) - } -} - -/** - * Get vessel beacon malfunctions - * - * @throws {@link FrontendApiError} - */ -async function getVesselBeaconsMalfunctionsFromAPI( - vesselId: Vessel.VesselId, - fromDate: Date -): Promise { - try { - return await monitorfishApiKy - .get(`/bff/v1/vessels/beacon_malfunctions?vesselId=${vesselId}&afterDateTime=${fromDate.toISOString()}`) - .json() - } catch (err) { - throw new FrontendApiError(GET_VESSEL_BEACON_MALFUNCTIONS_ERROR_MESSAGE, (err as FrontendApiError).originalError) - } -} - -/** - * Send a notification - Update the request notification column to asynchronously send the message - * - * @throws {@link FrontendApiError} - */ -async function sendNotificationFromAPI( - id: number, - notificationType: keyof typeof NOTIFICATION_TYPE, - foreignFmcCode?: string -): Promise { - try { - await monitorfishApiKy.put( - `/bff/v1/beacon_malfunctions/${id}/${notificationType}?requestedNotificationForeignFmcCode=${foreignFmcCode}` - ) - } catch (err) { - throw new FrontendApiError(SEND_NOTIFICATION_ERROR_MESSAGE, (err as FrontendApiError).originalError) - } -} - -export { - getVesselBeaconsMalfunctionsFromAPI, - saveBeaconMalfunctionCommentFromAPI, - getBeaconMalfunctionFromAPI, - updateBeaconMalfunctionFromAPI, - getAllBeaconMalfunctionsFromAPI, - sendNotificationFromAPI -} diff --git a/frontend/src/api/reporting.ts b/frontend/src/api/reporting.ts deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/frontend/src/domain/use_cases/beaconMalfunction/getAllBeaconMalfunctions.ts b/frontend/src/domain/use_cases/beaconMalfunction/getAllBeaconMalfunctions.ts deleted file mode 100644 index d2bd482bae..0000000000 --- a/frontend/src/domain/use_cases/beaconMalfunction/getAllBeaconMalfunctions.ts +++ /dev/null @@ -1,16 +0,0 @@ -import { getAllBeaconMalfunctionsFromAPI } from '@api/beaconMalfunction' -import { setBeaconMalfunctions } from 'domain/shared_slices/BeaconMalfunction' -import { setError } from 'domain/shared_slices/Global' - -import type { MainAppThunk } from '@store' - -export const getAllBeaconMalfunctions = (): MainAppThunk> => async dispatch => { - try { - const beaconMalfunctions = await getAllBeaconMalfunctionsFromAPI() - - dispatch(setBeaconMalfunctions(beaconMalfunctions)) - } catch (err) { - console.error(err) - dispatch(setError(err)) - } -} diff --git a/frontend/src/domain/use_cases/beaconMalfunction/openBeaconMalfunction.js b/frontend/src/domain/use_cases/beaconMalfunction/openBeaconMalfunction.js deleted file mode 100644 index 23fb7dab1b..0000000000 --- a/frontend/src/domain/use_cases/beaconMalfunction/openBeaconMalfunction.js +++ /dev/null @@ -1,39 +0,0 @@ -import { getBeaconMalfunctionFromAPI } from '../../../api/beaconMalfunction' -import { setOpenedBeaconMalfunction } from '../../shared_slices/BeaconMalfunction' -import { setError } from '../../shared_slices/Global' - -/** - * Open a single beacon malfunction - * @function openBeaconMalfunction - * @param {BeaconMalfunctionResumeAndDetails} beaconMalfunction - the beacon malfunction to open - * @param {boolean} isFromUserAction - if the use case is called from the API Worker - */ -export const openBeaconMalfunction = (beaconMalfunction, isFromUserAction) => (dispatch, getState) => { - const previousBeaconMalfunction = getState().beaconMalfunction.openedBeaconMalfunction - dispatch( - setOpenedBeaconMalfunction({ - beaconMalfunction, - showTab: isFromUserAction - }) - ) - - getBeaconMalfunctionFromAPI(beaconMalfunction.beaconMalfunction?.id) - .then(beaconMalfunctionWithDetails => { - dispatch( - setOpenedBeaconMalfunction({ - beaconMalfunction: beaconMalfunctionWithDetails, - showTab: isFromUserAction - }) - ) - }) - .catch(error => { - console.error(error) - dispatch(setError(error)) - dispatch( - setOpenedBeaconMalfunction({ - beaconMalfunction: previousBeaconMalfunction, - showTab: isFromUserAction - }) - ) - }) -} diff --git a/frontend/src/domain/use_cases/beaconMalfunction/openBeaconMalfunctionInKanban.ts b/frontend/src/domain/use_cases/beaconMalfunction/openBeaconMalfunctionInKanban.ts deleted file mode 100644 index 634b9eedac..0000000000 --- a/frontend/src/domain/use_cases/beaconMalfunction/openBeaconMalfunctionInKanban.ts +++ /dev/null @@ -1,16 +0,0 @@ -import { getBeaconMalfunctionFromAPI } from '../../../api/beaconMalfunction' -import { setOpenedBeaconMalfunctionsInKanban } from '../../shared_slices/BeaconMalfunction' -import { setError } from '../../shared_slices/Global' - -/** - * Open a single beacon malfunction - */ -export const openBeaconMalfunctionInKanban = id => dispatch => { - getBeaconMalfunctionFromAPI(id) - .then(beaconMalfunctionWithDetails => { - dispatch(setOpenedBeaconMalfunctionsInKanban(beaconMalfunctionWithDetails)) - }) - .catch(error => { - dispatch(setError(error)) - }) -} diff --git a/frontend/src/domain/use_cases/beaconMalfunction/saveBeaconMalfunctionCommentFromKanban.js b/frontend/src/domain/use_cases/beaconMalfunction/saveBeaconMalfunctionCommentFromKanban.js deleted file mode 100644 index eba68fb588..0000000000 --- a/frontend/src/domain/use_cases/beaconMalfunction/saveBeaconMalfunctionCommentFromKanban.js +++ /dev/null @@ -1,50 +0,0 @@ -import { saveBeaconMalfunctionCommentFromAPI } from '../../../api/beaconMalfunction' -import { - setOpenedBeaconMalfunction, - setOpenedBeaconMalfunctionsInKanban, - updateVesselBeaconMalfunctionsResumeAndHistory -} from '../../shared_slices/BeaconMalfunction' -import { setError } from '../../shared_slices/Global' - -/** - * Save a new comment to a beacon malfunction - * @function showVesselTrack - * @param {number} beaconMalfunctionId - * @param {string} comment - */ -export const saveBeaconMalfunctionCommentFromKanban = (beaconMalfunctionId, comment) => (dispatch, getState) => { - const { userType } = getState().global - const newCommentInput = { - comment, - userType - } - const beaconMalfunctionToUpdateIsOpenedAsCurrentVesselMalfunction = - getState().beaconMalfunction.vesselBeaconMalfunctionsResumeAndHistory?.current?.beaconMalfunction?.id === - beaconMalfunctionId - const beaconMalfunctionToUpdateIsOpened = - getState().beaconMalfunction.openedBeaconMalfunction?.beaconMalfunction?.id === beaconMalfunctionId - const beaconMalfunctionToUpdateIsOpenedInKanban = - getState().beaconMalfunction.openedBeaconMalfunctionInKanban?.beaconMalfunction?.id === beaconMalfunctionId - - return saveBeaconMalfunctionCommentFromAPI(beaconMalfunctionId, newCommentInput) - .then(beaconMalfunctionWithDetails => { - if (beaconMalfunctionToUpdateIsOpened) { - dispatch( - setOpenedBeaconMalfunction({ - beaconMalfunction: beaconMalfunctionWithDetails, - showTab: false - }) - ) - } - if (beaconMalfunctionToUpdateIsOpenedInKanban) { - dispatch(setOpenedBeaconMalfunctionsInKanban(beaconMalfunctionWithDetails)) - } - if (beaconMalfunctionToUpdateIsOpenedAsCurrentVesselMalfunction) { - dispatch(updateVesselBeaconMalfunctionsResumeAndHistory(beaconMalfunctionWithDetails)) - } - }) - .catch(error => { - console.error(error) - dispatch(setError(error)) - }) -} diff --git a/frontend/src/domain/use_cases/beaconMalfunction/sendNotification.ts b/frontend/src/domain/use_cases/beaconMalfunction/sendNotification.ts deleted file mode 100644 index 8e54db9466..0000000000 --- a/frontend/src/domain/use_cases/beaconMalfunction/sendNotification.ts +++ /dev/null @@ -1,25 +0,0 @@ -import { NOTIFICATION_TYPE } from '@features/BeaconMalfunction/constants' - -import { sendNotificationFromAPI } from '../../../api/beaconMalfunction' -import { setError } from '../../shared_slices/Global' - -/** - * Send a notification message - */ -export const sendNotification = - (beaconMalfunctionId: number, notificationType: string | null, foreignFmcCode?: string) => - (dispatch): Promise => { - if (!notificationType || !Object.keys(NOTIFICATION_TYPE).find(type => type === notificationType)) { - return Promise.resolve() - } - - return sendNotificationFromAPI( - beaconMalfunctionId, - notificationType as keyof typeof NOTIFICATION_TYPE, - foreignFmcCode - ) - .then(() => notificationType) - .catch(error => { - dispatch(setError(error)) - }) - } diff --git a/frontend/src/domain/use_cases/beaconMalfunction/updateBeaconMalfunctionFromKanban.js b/frontend/src/domain/use_cases/beaconMalfunction/updateBeaconMalfunctionFromKanban.js deleted file mode 100644 index 1ffcf24f61..0000000000 --- a/frontend/src/domain/use_cases/beaconMalfunction/updateBeaconMalfunctionFromKanban.js +++ /dev/null @@ -1,48 +0,0 @@ -import { updateBeaconMalfunctionFromAPI } from '../../../api/beaconMalfunction' -import { - setBeaconMalfunctions, - updateLocalBeaconMalfunction, - setOpenedBeaconMalfunctionsInKanban, - setOpenedBeaconMalfunction, - updateVesselBeaconMalfunctionsResumeAndHistory -} from '../../shared_slices/BeaconMalfunction' -import { setError } from '../../shared_slices/Global' - -/** - * Update a beacon malfunction - * @param {number} id - The id of the beacon malfunction - * @param {BeaconMalfunction} nextBeaconMalfunction - The next beacon malfunction - * @param {UpdateBeaconMalfunction} updatedFields - The fields to update - */ -export const updateBeaconMalfunctionFromKanban = (id, nextBeaconMalfunction, updatedFields) => (dispatch, getState) => { - const previousBeaconMalfunctions = getState().beaconMalfunction.beaconMalfunctions - dispatch(updateLocalBeaconMalfunction(nextBeaconMalfunction)) - const beaconMalfunctionToUpdateIsOpenedAsCurrentVesselMalfunction = - getState().beaconMalfunction.vesselBeaconMalfunctionsResumeAndHistory?.current?.beaconMalfunction?.id === id - const beaconMalfunctionToUpdateIsOpened = - getState().beaconMalfunction.openedBeaconMalfunction?.beaconMalfunction?.id === id - const beaconMalfunctionToUpdateIsOpenedInKanban = - getState().beaconMalfunction.openedBeaconMalfunctionInKanban?.beaconMalfunction?.id === id - - return updateBeaconMalfunctionFromAPI(id, updatedFields) - .then(updatedBeaconMalfunctionWithDetails => { - if (beaconMalfunctionToUpdateIsOpened) { - dispatch( - setOpenedBeaconMalfunction({ - beaconMalfunction: updatedBeaconMalfunctionWithDetails, - showTab: false - }) - ) - } - if (beaconMalfunctionToUpdateIsOpenedInKanban) { - dispatch(setOpenedBeaconMalfunctionsInKanban(updatedBeaconMalfunctionWithDetails)) - } - if (beaconMalfunctionToUpdateIsOpenedAsCurrentVesselMalfunction) { - dispatch(updateVesselBeaconMalfunctionsResumeAndHistory(updatedBeaconMalfunctionWithDetails)) - } - }) - .catch(error => { - dispatch(setError(error)) - dispatch(setBeaconMalfunctions(previousBeaconMalfunctions)) - }) -} diff --git a/frontend/src/features/BeaconMalfunction/__tests__/getMalfunctionStartDateText.test.ts b/frontend/src/features/BeaconMalfunction/__tests__/getMalfunctionStartDateText.test.ts index 0c10f891a6..8355592042 100644 --- a/frontend/src/features/BeaconMalfunction/__tests__/getMalfunctionStartDateText.test.ts +++ b/frontend/src/features/BeaconMalfunction/__tests__/getMalfunctionStartDateText.test.ts @@ -13,10 +13,10 @@ describe('domain/entities/beaconMalfunction/index.getMalfunctionStartDateText()' id: 5, internalReferenceNumber: 'FR263465414', ircs: 'IR123', - malfunctionEndDateTime: null, + malfunctionEndDateTime: undefined, malfunctionStartDateTime: '2023-08-21T11:17:22.997231Z', - notificationRequested: null, - riskFactor: null, + notificationRequested: undefined, + riskFactor: undefined, stage: 'ARCHIVED', vesselId: 12, vesselIdentifier: 'EXTERNAL_REFERENCE_NUMBER', diff --git a/frontend/src/features/BeaconMalfunction/apis.ts b/frontend/src/features/BeaconMalfunction/apis.ts new file mode 100644 index 0000000000..e4eeb85c3f --- /dev/null +++ b/frontend/src/features/BeaconMalfunction/apis.ts @@ -0,0 +1,93 @@ +import { monitorfishApi } from '@api/api' +import { FrontendApiError } from '@libs/FrontendApiError' + +import type { NOTIFICATION_TYPE, UserType } from '@features/BeaconMalfunction/constants' +import type { + BeaconMalfunction, + BeaconMalfunctionResumeAndDetails, + UpdateBeaconMalfunction, + VesselBeaconMalfunctionsResumeAndHistory +} from '@features/BeaconMalfunction/types' +import type { Vessel } from '@features/Vessel/Vessel.types' + +export const ARCHIVE_BEACON_MALFUNCTION = "Nous n'avons pas pu archiver les avaries VMS" +export const GET_BEACON_MALFUNCTIONS_ERROR_MESSAGE = "Nous n'avons pas pu récupérer les avaries VMS" +export const GET_BEACON_MALFUNCTION_ERROR_MESSAGE = "Nous n'avons pas pu récupérer l'avarie VMS" +export const UPDATE_BEACON_MALFUNCTIONS_ERROR_MESSAGE = "Nous n'avons pas pu mettre à jour le statut de l'avarie VMS" +export const SAVE_BEACON_MALFUNCTION_COMMENT_ERROR_MESSAGE = + "Nous n'avons pas pu ajouter le commentaire sur l'avarie VMS" +export const GET_VESSEL_BEACON_MALFUNCTIONS_ERROR_MESSAGE = "Nous n'avons pas pu récupérer les avaries de ce navire" +export const SEND_NOTIFICATION_ERROR_MESSAGE = "Nous n'avons pas pu envoyer la notification" + +export const beaconMalfunctionApi = monitorfishApi.injectEndpoints({ + endpoints: builder => ({ + getAllBeaconMalfunctions: builder.query({ + query: () => ({ + method: 'GET', + url: '/bff/v1/beacon_malfunctions' + }), + transformErrorResponse: response => new FrontendApiError(GET_BEACON_MALFUNCTIONS_ERROR_MESSAGE, response) + }), + + getBeaconMalfunction: builder.query({ + query: id => ({ + method: 'GET', + url: `/bff/v1/beacon_malfunctions/${id}` + }), + transformErrorResponse: response => new FrontendApiError(GET_BEACON_MALFUNCTION_ERROR_MESSAGE, response) + }), + + getVesselBeaconsMalfunctions: builder.query< + VesselBeaconMalfunctionsResumeAndHistory, + { fromDate: Date; vesselId: Vessel.VesselId } + >({ + query: ({ fromDate, vesselId }) => ({ + method: 'GET', + params: { + afterDateTime: fromDate.toISOString(), + vesselId + }, + url: '/bff/v1/vessels/beacon_malfunctions' + }), + transformErrorResponse: response => new FrontendApiError(GET_VESSEL_BEACON_MALFUNCTIONS_ERROR_MESSAGE, response) + }), + + saveBeaconMalfunctionComment: builder.mutation< + BeaconMalfunctionResumeAndDetails, + { comment: { comment: string; userType: keyof typeof UserType }; id: number } + >({ + query: ({ comment, id }) => ({ + body: comment, + method: 'POST', + url: `/bff/v1/beacon_malfunctions/${id}/comments` + }), + transformErrorResponse: response => new FrontendApiError(SAVE_BEACON_MALFUNCTION_COMMENT_ERROR_MESSAGE, response) + }), + + sendNotification: builder.mutation< + void, + { foreignFmcCode: string | undefined; id: number; notificationType: keyof typeof NOTIFICATION_TYPE } + >({ + query: ({ foreignFmcCode, id, notificationType }) => ({ + method: 'PUT', + params: { + requestedNotificationForeignFmcCode: foreignFmcCode + }, + url: `/bff/v1/beacon_malfunctions/${id}/${notificationType}` + }), + transformErrorResponse: response => new FrontendApiError(SEND_NOTIFICATION_ERROR_MESSAGE, response) + }), + + updateBeaconMalfunction: builder.mutation< + BeaconMalfunctionResumeAndDetails, + { id: number; updatedFields: UpdateBeaconMalfunction } + >({ + query: ({ id, updatedFields }) => ({ + body: updatedFields, + method: 'PUT', + url: `/bff/v1/beacon_malfunctions/${id}` + }), + transformErrorResponse: response => new FrontendApiError(UPDATE_BEACON_MALFUNCTIONS_ERROR_MESSAGE, response) + }) + }) +}) diff --git a/frontend/src/features/BeaconMalfunction/components/BeaconMalfunctionBoard/BeaconMalfunctionCard.tsx b/frontend/src/features/BeaconMalfunction/components/BeaconMalfunctionBoard/BeaconMalfunctionCard.tsx index a681d4ea97..86f3b93f48 100644 --- a/frontend/src/features/BeaconMalfunction/components/BeaconMalfunctionBoard/BeaconMalfunctionCard.tsx +++ b/frontend/src/features/BeaconMalfunction/components/BeaconMalfunctionBoard/BeaconMalfunctionCard.tsx @@ -5,9 +5,9 @@ import styled from 'styled-components' import { getBeaconCreationOrModificationDate } from './utils' import { VesselStatusSelect } from './VesselStatusSelect' -import { openBeaconMalfunctionInKanban } from '../../../../domain/use_cases/beaconMalfunction/openBeaconMalfunctionInKanban' import { showVesselFromBeaconMalfunctionsKanban } from '../../../../domain/use_cases/vessel/showVesselFromBeaconMalfunctionsKanban' import { END_OF_MALFUNCTION_REASON_RECORD, VESSEL_STATUS } from '../../constants' +import { openBeaconMalfunctionInKanban } from '../../useCases/openBeaconMalfunctionInKanban' import { getMalfunctionStartDateText } from '../../utils' import type { BeaconMalfunction } from '../../types' @@ -20,7 +20,7 @@ export type BeaconMalfunctionCardProps = { isDragging: boolean isDroppedId: boolean | undefined isShowed: boolean - updateVesselStatus: (beaconMalfunction: BeaconMalfunction | undefined, status: string | null) => void + updateVesselStatus: (beaconMalfunction: BeaconMalfunction | undefined, status: string) => void verticalScrollRef: MutableRefObject | undefined } @@ -42,10 +42,13 @@ export function BeaconMalfunctionCard({ ? verticalScrollRef?.current?.scrollHeight > verticalScrollRef?.current?.clientHeight : false - const endOfBeaconMalfunctionReason = useMemo( - () => END_OF_MALFUNCTION_REASON_RECORD[beaconMalfunction?.endOfBeaconMalfunctionReason], - [beaconMalfunction] - ) + const endOfBeaconMalfunctionReason = useMemo(() => { + if (beaconMalfunction?.endOfBeaconMalfunctionReason) { + return END_OF_MALFUNCTION_REASON_RECORD[beaconMalfunction?.endOfBeaconMalfunctionReason] + } + + return undefined + }, [beaconMalfunction]) useEffect(() => { if (isShowed && beaconMalfunction && wrapperRef.current) { diff --git a/frontend/src/features/BeaconMalfunction/components/BeaconMalfunctionBoard/BeaconMalfunctionDetailsFollowUp.tsx b/frontend/src/features/BeaconMalfunction/components/BeaconMalfunctionBoard/BeaconMalfunctionDetailsFollowUp.tsx index f657328d81..9063267bcf 100644 --- a/frontend/src/features/BeaconMalfunction/components/BeaconMalfunctionBoard/BeaconMalfunctionDetailsFollowUp.tsx +++ b/frontend/src/features/BeaconMalfunction/components/BeaconMalfunctionBoard/BeaconMalfunctionDetailsFollowUp.tsx @@ -12,10 +12,10 @@ import { BeaconMalfunctionDetailsFollowUpItem } from './BeaconMalfunctionDetails import { BeaconMalfunctionDetailsFollowUpRow } from './BeaconMalfunctionDetailsFollowUpRow' import { BeaconMalfunctionDetailsType, getContent } from './utils' import { setUserType } from '../../../../domain/shared_slices/Global' -import { saveBeaconMalfunctionCommentFromKanban } from '../../../../domain/use_cases/beaconMalfunction/saveBeaconMalfunctionCommentFromKanban' import { getDate, mergeObjects } from '../../../../utils' import CommentsSVG from '../../../icons/Commentaires.svg?react' import { UserType, VESSEL_STATUS } from '../../constants' +import { saveBeaconMalfunctionCommentFromKanban } from '../../useCases/saveBeaconMalfunctionCommentFromKanban' import type { BeaconMalfunctionFollowUpItem, BeaconMalfunctionStatusValue } from '../../types' import type { CSSProperties } from 'react' diff --git a/frontend/src/features/BeaconMalfunction/components/BeaconMalfunctionBoard/SendNotification.tsx b/frontend/src/features/BeaconMalfunction/components/BeaconMalfunctionBoard/SendNotification.tsx index c1280b93e1..4670a939c2 100644 --- a/frontend/src/features/BeaconMalfunction/components/BeaconMalfunctionBoard/SendNotification.tsx +++ b/frontend/src/features/BeaconMalfunction/components/BeaconMalfunctionBoard/SendNotification.tsx @@ -5,8 +5,8 @@ import { useEffect, useMemo, useRef, useState } from 'react' import { SelectPicker } from 'rsuite' import styled from 'styled-components' -import { sendNotification } from '../../../../domain/use_cases/beaconMalfunction/sendNotification' import { NOTIFICATION_TYPE, SELECTABLE_NOTIFICATION_TYPES } from '../../constants' +import { sendNotification } from '../../useCases/sendNotification' import type { CSSProperties } from 'react' diff --git a/frontend/src/features/BeaconMalfunction/components/BeaconMalfunctionBoard/VesselStatusSelect.tsx b/frontend/src/features/BeaconMalfunction/components/BeaconMalfunctionBoard/VesselStatusSelect.tsx index 6c8989a342..0cd1868dad 100644 --- a/frontend/src/features/BeaconMalfunction/components/BeaconMalfunctionBoard/VesselStatusSelect.tsx +++ b/frontend/src/features/BeaconMalfunction/components/BeaconMalfunctionBoard/VesselStatusSelect.tsx @@ -13,7 +13,7 @@ type VesselStatusSelectProps = { isAbsolute?: boolean isCleanable?: boolean marginTop?: number | undefined - updateVesselStatus: (beaconMalfunction: BeaconMalfunction | undefined, status: string | null) => void + updateVesselStatus: (beaconMalfunction: BeaconMalfunction | undefined, status: string) => void // TODO Type vesselStatus in constants.tsx vesselStatus: { color: string; icon: JSX.Element; label: string; textColor: string; value: string } | undefined } @@ -61,7 +61,7 @@ export function VesselStatusSelect({ ? { marginLeft: 40, marginTop: 120, position: 'absolute' } : { marginLeft: -5, marginTop: marginTop ?? -67, position: 'relative' } } - onChange={status => updateVesselStatus(beaconMalfunction, status)} + onChange={status => updateVesselStatus(beaconMalfunction, status as string)} placeholder="Statut" renderValue={(_, item) => } searchable={false} diff --git a/frontend/src/features/BeaconMalfunction/components/BeaconMalfunctionBoard/index.tsx b/frontend/src/features/BeaconMalfunction/components/BeaconMalfunctionBoard/index.tsx index b8ab5d364a..c68d4fd47a 100644 --- a/frontend/src/features/BeaconMalfunction/components/BeaconMalfunctionBoard/index.tsx +++ b/frontend/src/features/BeaconMalfunction/components/BeaconMalfunctionBoard/index.tsx @@ -8,7 +8,12 @@ import { useSensor, useSensors } from '@dnd-kit/core' -import { STAGE_RECORD, VESSEL_STATUS } from '@features/BeaconMalfunction/constants' +import { + BeaconMalfunctionsStage, + BeaconMalfunctionVesselStatus, + STAGE_RECORD, + VESSEL_STATUS +} from '@features/BeaconMalfunction/constants' import { useMainAppDispatch } from '@hooks/useMainAppDispatch' import { useMainAppSelector } from '@hooks/useMainAppSelector' import { THEME } from '@mtes-mct/monitor-ui' @@ -23,10 +28,10 @@ import { StageColumn } from './StageColumn' import { getBeaconMalfunctionsByStage, searchInBeaconMalfunctions } from './utils' import { VesselStatusSelect } from './VesselStatusSelect' import { setError } from '../../../../domain/shared_slices/Global' -import { getAllBeaconMalfunctions } from '../../../../domain/use_cases/beaconMalfunction/getAllBeaconMalfunctions' -import { updateBeaconMalfunctionFromKanban } from '../../../../domain/use_cases/beaconMalfunction/updateBeaconMalfunctionFromKanban' import { LegacyRsuiteComponentsWrapper } from '../../../../ui/LegacyRsuiteComponentsWrapper' import SearchIconSVG from '../../../icons/Loupe_dark.svg?react' +import { getAllBeaconMalfunctions } from '../../useCases/getAllBeaconMalfunctions' +import { updateBeaconMalfunctionFromKanban } from '../../useCases/updateBeaconMalfunctionFromKanban' import type { BeaconMalfunction, @@ -125,22 +130,22 @@ export function BeaconMalfunctionBoard() { ) const updateVesselStatus = useCallback( - (beaconMalfunction: BeaconMalfunction | undefined, status: string | null) => { + (beaconMalfunction: BeaconMalfunction | undefined, status: string) => { if (!beaconMalfunction) { return } const nextBeaconMalfunction = { ...beaconMalfunction, - vesselStatus: status, + vesselStatus: status as BeaconMalfunctionVesselStatus, vesselStatusLastModificationDateTime: new Date().toISOString() } setIsDroppedId(beaconMalfunction.id) dispatch( - // @ts-ignore updateBeaconMalfunctionFromKanban(beaconMalfunction.id, nextBeaconMalfunction, { - vesselStatus: nextBeaconMalfunction.vesselStatus + stage: nextBeaconMalfunction.stage as BeaconMalfunctionsStage | undefined, + vesselStatus: nextBeaconMalfunction.vesselStatus as BeaconMalfunctionVesselStatus | undefined }) ) }, diff --git a/frontend/src/features/BeaconMalfunction/components/BeaconMalfunctionBoard/utils.tsx b/frontend/src/features/BeaconMalfunction/components/BeaconMalfunctionBoard/utils.tsx index 5901170774..d147bc029b 100644 --- a/frontend/src/features/BeaconMalfunction/components/BeaconMalfunctionBoard/utils.tsx +++ b/frontend/src/features/BeaconMalfunction/components/BeaconMalfunctionBoard/utils.tsx @@ -92,7 +92,7 @@ const getFirstStatusAction = (vesselStatus: BeaconMalfunctionStatusValue, malfun export function getActionText( action: BeaconMalfunctionAction, - endOfBeaconMalfunctionReason: EndOfBeaconMalfunctionReason | null + endOfBeaconMalfunctionReason: EndOfBeaconMalfunctionReason | undefined ) { switch (action.propertyName) { case BeaconMalfunctionPropertyName.VESSEL_STATUS: { diff --git a/frontend/src/features/BeaconMalfunction/types.ts b/frontend/src/features/BeaconMalfunction/types.ts index 915729a085..3c490e90eb 100644 --- a/frontend/src/features/BeaconMalfunction/types.ts +++ b/frontend/src/features/BeaconMalfunction/types.ts @@ -9,15 +9,15 @@ import type { Vessel } from '@features/Vessel/Vessel.types' import type { Integer } from 'type-fest' export type BeaconMalfunction = { - endOfBeaconMalfunctionReason: EndOfBeaconMalfunctionReason + endOfBeaconMalfunctionReason: EndOfBeaconMalfunctionReason | undefined externalReferenceNumber: string flagState: string id: number internalReferenceNumber: string ircs: string - malfunctionEndDateTime: string | null + malfunctionEndDateTime: string | undefined malfunctionStartDateTime: string - notificationRequested: string | null + notificationRequested: string | undefined stage: string vesselIdentifier: string vesselName: string @@ -26,8 +26,8 @@ export type BeaconMalfunction = { } export type UpdateBeaconMalfunction = { - stage: BeaconMalfunctionsStage | null - vesselStatus: BeaconMalfunctionVesselStatus | null + stage: BeaconMalfunctionsStage | undefined + vesselStatus: BeaconMalfunctionVesselStatus | undefined } export type BeaconMalfunctionComment = { @@ -58,13 +58,13 @@ export type BeaconMalfunctionNotification = { beaconMalfunctionId: Integer communicationMeans: string dateTime: string - errorMessage: string | null + errorMessage: string | undefined id: number notificationType: string recipientAddressOrNumber: string recipientFunction: string recipientName: string - success: boolean | null + success: boolean | undefined } export type BeaconMalfunctionNotifications = { @@ -83,15 +83,15 @@ export type BeaconMalfunctionResumeAndDetails = { } export type VesselBeaconMalfunctionsResumeAndHistory = { - current: BeaconMalfunctionResumeAndDetails | null + current: BeaconMalfunctionResumeAndDetails | undefined history: BeaconMalfunctionResumeAndDetails[] resume: VesselBeaconMalfunctionsResume vesselIdentity: Vessel.VesselIdentity } export type VesselBeaconMalfunctionsResume = { - lastBeaconMalfunctionDateTime: string | null - lastBeaconMalfunctionVesselStatus: string | null + lastBeaconMalfunctionDateTime: string | undefined + lastBeaconMalfunctionVesselStatus: string | undefined numberOfBeaconsAtPort: number numberOfBeaconsAtSea: number } diff --git a/frontend/src/features/BeaconMalfunction/useCases/getAllBeaconMalfunctions.ts b/frontend/src/features/BeaconMalfunction/useCases/getAllBeaconMalfunctions.ts new file mode 100644 index 0000000000..93d8f9597f --- /dev/null +++ b/frontend/src/features/BeaconMalfunction/useCases/getAllBeaconMalfunctions.ts @@ -0,0 +1,19 @@ +import { RTK_FORCE_REFETCH_QUERY_OPTIONS } from '@api/constants' +import { beaconMalfunctionApi } from '@features/BeaconMalfunction/apis' + +import { setBeaconMalfunctions } from '../../../domain/shared_slices/BeaconMalfunction' +import { setError } from '../../../domain/shared_slices/Global' + +import type { MainAppThunk } from '@store' + +export const getAllBeaconMalfunctions = (): MainAppThunk> => async dispatch => { + try { + const beaconMalfunctions = await dispatch( + beaconMalfunctionApi.endpoints.getAllBeaconMalfunctions.initiate(undefined, RTK_FORCE_REFETCH_QUERY_OPTIONS) + ).unwrap() + + dispatch(setBeaconMalfunctions(beaconMalfunctions)) + } catch (err) { + dispatch(setError(err)) + } +} diff --git a/frontend/src/domain/use_cases/beaconMalfunction/getVesselBeaconMalfunctions.ts b/frontend/src/features/BeaconMalfunction/useCases/getVesselBeaconMalfunctions.ts similarity index 69% rename from frontend/src/domain/use_cases/beaconMalfunction/getVesselBeaconMalfunctions.ts rename to frontend/src/features/BeaconMalfunction/useCases/getVesselBeaconMalfunctions.ts index ffe8bd934f..06c65b0894 100644 --- a/frontend/src/domain/use_cases/beaconMalfunction/getVesselBeaconMalfunctions.ts +++ b/frontend/src/features/BeaconMalfunction/useCases/getVesselBeaconMalfunctions.ts @@ -1,16 +1,17 @@ +import { RTK_FORCE_REFETCH_QUERY_OPTIONS } from '@api/constants' +import { beaconMalfunctionApi } from '@features/BeaconMalfunction/apis' import { extractVesselIdentityProps } from '@features/Vessel/utils' import { DisplayedErrorKey } from '@libs/DisplayedError/constants' import { openBeaconMalfunction } from './openBeaconMalfunction' -import { getVesselBeaconsMalfunctionsFromAPI } from '../../../api/beaconMalfunction' import { loadVesselBeaconMalfunctions, resetVesselBeaconMalfunctionsResumeAndHistory, setVesselBeaconMalfunctionsResumeAndHistory -} from '../../shared_slices/BeaconMalfunction' -import { displayedErrorActions } from '../../shared_slices/DisplayedError' -import { removeError } from '../../shared_slices/Global' -import { displayOrLogError } from '../error/displayOrLogError' +} from '../../../domain/shared_slices/BeaconMalfunction' +import { displayedErrorActions } from '../../../domain/shared_slices/DisplayedError' +import { removeError } from '../../../domain/shared_slices/Global' +import { displayOrLogError } from '../../../domain/use_cases/error/displayOrLogError' export const getVesselBeaconMalfunctions = (isFromUserAction: boolean) => async (dispatch, getState) => { const { selectedVessel } = getState().vessel @@ -30,10 +31,13 @@ export const getVesselBeaconMalfunctions = (isFromUserAction: boolean) => async } try { - const vesselBeaconsMalfunctions = await getVesselBeaconsMalfunctionsFromAPI( - selectedVessel.vesselId, - vesselBeaconMalfunctionsFromDate - ) + const vesselBeaconsMalfunctions = await dispatch( + beaconMalfunctionApi.endpoints.getVesselBeaconsMalfunctions.initiate( + { fromDate: vesselBeaconMalfunctionsFromDate, vesselId: selectedVessel.vesselId }, + RTK_FORCE_REFETCH_QUERY_OPTIONS + ) + )().unwrap() + dispatch( setVesselBeaconMalfunctionsResumeAndHistory({ ...vesselBeaconsMalfunctions, diff --git a/frontend/src/features/BeaconMalfunction/useCases/openBeaconMalfunction.ts b/frontend/src/features/BeaconMalfunction/useCases/openBeaconMalfunction.ts new file mode 100644 index 0000000000..8f8c06aa70 --- /dev/null +++ b/frontend/src/features/BeaconMalfunction/useCases/openBeaconMalfunction.ts @@ -0,0 +1,45 @@ +import { RTK_FORCE_REFETCH_QUERY_OPTIONS } from '@api/constants' +import { beaconMalfunctionApi } from '@features/BeaconMalfunction/apis' + +import { setOpenedBeaconMalfunction } from '../../../domain/shared_slices/BeaconMalfunction' +import { setError } from '../../../domain/shared_slices/Global' + +import type { BeaconMalfunctionResumeAndDetails } from '@features/BeaconMalfunction/types' +import type { MainAppThunk } from '@store' + +export const openBeaconMalfunction = + (beaconMalfunction: BeaconMalfunctionResumeAndDetails, isFromUserAction: boolean): MainAppThunk> => + async (dispatch, getState) => { + const previousBeaconMalfunction = getState().beaconMalfunction.openedBeaconMalfunction + + dispatch( + setOpenedBeaconMalfunction({ + beaconMalfunction, + showTab: isFromUserAction + }) + ) + + try { + const beaconMalfunctionWithDetails = await dispatch( + beaconMalfunctionApi.endpoints.getBeaconMalfunction.initiate( + beaconMalfunction.beaconMalfunction?.id as number, + RTK_FORCE_REFETCH_QUERY_OPTIONS + ) + ).unwrap() + + dispatch( + setOpenedBeaconMalfunction({ + beaconMalfunction: beaconMalfunctionWithDetails, + showTab: isFromUserAction + }) + ) + } catch (error) { + dispatch(setError(error)) + dispatch( + setOpenedBeaconMalfunction({ + beaconMalfunction: previousBeaconMalfunction, + showTab: isFromUserAction + }) + ) + } + } diff --git a/frontend/src/features/BeaconMalfunction/useCases/openBeaconMalfunctionInKanban.ts b/frontend/src/features/BeaconMalfunction/useCases/openBeaconMalfunctionInKanban.ts new file mode 100644 index 0000000000..377bbdfc7d --- /dev/null +++ b/frontend/src/features/BeaconMalfunction/useCases/openBeaconMalfunctionInKanban.ts @@ -0,0 +1,21 @@ +import { RTK_FORCE_REFETCH_QUERY_OPTIONS } from '@api/constants' +import { beaconMalfunctionApi } from '@features/BeaconMalfunction/apis' + +import { setOpenedBeaconMalfunctionsInKanban } from '../../../domain/shared_slices/BeaconMalfunction' +import { setError } from '../../../domain/shared_slices/Global' + +import type { MainAppThunk } from '@store' + +export const openBeaconMalfunctionInKanban = + (id: number): MainAppThunk> => + async dispatch => { + try { + const beaconMalfunctionWithDetails = await dispatch( + beaconMalfunctionApi.endpoints.getBeaconMalfunction.initiate(id, RTK_FORCE_REFETCH_QUERY_OPTIONS) + ).unwrap() + + dispatch(setOpenedBeaconMalfunctionsInKanban(beaconMalfunctionWithDetails)) + } catch (error) { + dispatch(setError(error)) + } + } diff --git a/frontend/src/features/BeaconMalfunction/useCases/saveBeaconMalfunctionCommentFromKanban.ts b/frontend/src/features/BeaconMalfunction/useCases/saveBeaconMalfunctionCommentFromKanban.ts new file mode 100644 index 0000000000..f8646c9d86 --- /dev/null +++ b/frontend/src/features/BeaconMalfunction/useCases/saveBeaconMalfunctionCommentFromKanban.ts @@ -0,0 +1,59 @@ +import { beaconMalfunctionApi } from '@features/BeaconMalfunction/apis' + +import { + setOpenedBeaconMalfunction, + setOpenedBeaconMalfunctionsInKanban, + updateVesselBeaconMalfunctionsResumeAndHistory +} from '../../../domain/shared_slices/BeaconMalfunction' +import { setError } from '../../../domain/shared_slices/Global' + +import type { UserType } from '@features/BeaconMalfunction/constants' +import type { MainAppThunk } from '@store' + +export const saveBeaconMalfunctionCommentFromKanban = + (beaconMalfunctionId: number, comment: string): MainAppThunk> => + async (dispatch, getState) => { + const state = getState() + const { userType } = getState().global + + const newCommentInput = { + comment, + userType: userType as keyof typeof UserType + } + + const beaconMalfunctionToUpdateIsOpenedAsCurrentVesselMalfunction = + state.beaconMalfunction.vesselBeaconMalfunctionsResumeAndHistory?.current?.beaconMalfunction?.id === + beaconMalfunctionId + const beaconMalfunctionToUpdateIsOpened = + state.beaconMalfunction.openedBeaconMalfunction?.beaconMalfunction?.id === beaconMalfunctionId + const beaconMalfunctionToUpdateIsOpenedInKanban = + state.beaconMalfunction.openedBeaconMalfunctionInKanban?.beaconMalfunction?.id === beaconMalfunctionId + + try { + const beaconMalfunctionWithDetails = await dispatch( + beaconMalfunctionApi.endpoints.saveBeaconMalfunctionComment.initiate({ + comment: newCommentInput, + id: beaconMalfunctionId + }) + ).unwrap() + + if (beaconMalfunctionToUpdateIsOpened) { + dispatch( + setOpenedBeaconMalfunction({ + beaconMalfunction: beaconMalfunctionWithDetails, + showTab: false + }) + ) + } + // If the malfunction is open in the Kanban view, update that slice. + if (beaconMalfunctionToUpdateIsOpenedInKanban) { + dispatch(setOpenedBeaconMalfunctionsInKanban(beaconMalfunctionWithDetails)) + } + // If the malfunction is the current vessel malfunction, update that slice. + if (beaconMalfunctionToUpdateIsOpenedAsCurrentVesselMalfunction) { + dispatch(updateVesselBeaconMalfunctionsResumeAndHistory(beaconMalfunctionWithDetails)) + } + } catch (error) { + dispatch(setError(error)) + } + } diff --git a/frontend/src/features/BeaconMalfunction/useCases/sendNotification.ts b/frontend/src/features/BeaconMalfunction/useCases/sendNotification.ts new file mode 100644 index 0000000000..441b252edf --- /dev/null +++ b/frontend/src/features/BeaconMalfunction/useCases/sendNotification.ts @@ -0,0 +1,34 @@ +import { beaconMalfunctionApi } from '@features/BeaconMalfunction/apis' +import { NOTIFICATION_TYPE } from '@features/BeaconMalfunction/constants' + +import { setError } from '../../../domain/shared_slices/Global' + +import type { MainAppThunk } from '@store' + +export const sendNotification = + ( + beaconMalfunctionId: number, + notificationType: string | null, + foreignFmcCode?: string + ): MainAppThunk> => + async dispatch => { + if (!notificationType || !Object.keys(NOTIFICATION_TYPE).includes(notificationType)) { + return Promise.resolve() + } + + try { + await dispatch( + beaconMalfunctionApi.endpoints.sendNotification.initiate({ + foreignFmcCode, + id: beaconMalfunctionId, + notificationType: notificationType as keyof typeof NOTIFICATION_TYPE + }) + ).unwrap() + + return notificationType + } catch (error) { + dispatch(setError(error)) + + return undefined + } + } diff --git a/frontend/src/features/BeaconMalfunction/useCases/updateBeaconMalfunctionFromKanban.ts b/frontend/src/features/BeaconMalfunction/useCases/updateBeaconMalfunctionFromKanban.ts new file mode 100644 index 0000000000..2771f8395e --- /dev/null +++ b/frontend/src/features/BeaconMalfunction/useCases/updateBeaconMalfunctionFromKanban.ts @@ -0,0 +1,62 @@ +import { beaconMalfunctionApi } from '@features/BeaconMalfunction/apis' + +import { + setBeaconMalfunctions, + setOpenedBeaconMalfunction, + setOpenedBeaconMalfunctionsInKanban, + updateLocalBeaconMalfunction, + updateVesselBeaconMalfunctionsResumeAndHistory +} from '../../../domain/shared_slices/BeaconMalfunction' +import { setError } from '../../../domain/shared_slices/Global' + +import type { BeaconMalfunction, UpdateBeaconMalfunction } from '@features/BeaconMalfunction/types' +import type { MainAppThunk } from '@store' + +export const updateBeaconMalfunctionFromKanban = + ( + id: number, + nextBeaconMalfunction: BeaconMalfunction, + updatedFields: UpdateBeaconMalfunction + ): MainAppThunk> => + async (dispatch, getState) => { + // Save the current beacon malfunctions for potential rollback. + const previousBeaconMalfunctions = getState().beaconMalfunction.beaconMalfunctions + + // Perform an optimistic update in the local state. + dispatch(updateLocalBeaconMalfunction(nextBeaconMalfunction)) + + const state = getState() + const beaconMalfunctionToUpdateIsOpenedAsCurrentVesselMalfunction = + state.beaconMalfunction.vesselBeaconMalfunctionsResumeAndHistory?.current?.beaconMalfunction?.id === id + const beaconMalfunctionToUpdateIsOpened = + state.beaconMalfunction.openedBeaconMalfunction?.beaconMalfunction?.id === id + const beaconMalfunctionToUpdateIsOpenedInKanban = + state.beaconMalfunction.openedBeaconMalfunctionInKanban?.beaconMalfunction?.id === id + + try { + const updatedBeaconMalfunctionWithDetails = await dispatch( + beaconMalfunctionApi.endpoints.updateBeaconMalfunction.initiate({ + id, + updatedFields + }) + ).unwrap() + + if (beaconMalfunctionToUpdateIsOpened) { + dispatch( + setOpenedBeaconMalfunction({ + beaconMalfunction: updatedBeaconMalfunctionWithDetails, + showTab: false + }) + ) + } + if (beaconMalfunctionToUpdateIsOpenedInKanban) { + dispatch(setOpenedBeaconMalfunctionsInKanban(updatedBeaconMalfunctionWithDetails)) + } + if (beaconMalfunctionToUpdateIsOpenedAsCurrentVesselMalfunction) { + dispatch(updateVesselBeaconMalfunctionsResumeAndHistory(updatedBeaconMalfunctionWithDetails)) + } + } catch (error) { + dispatch(setError(error)) + dispatch(setBeaconMalfunctions(previousBeaconMalfunctions)) + } + } diff --git a/frontend/src/features/SideWindow/index.tsx b/frontend/src/features/SideWindow/index.tsx index 0fe53838ae..13a477b314 100644 --- a/frontend/src/features/SideWindow/index.tsx +++ b/frontend/src/features/SideWindow/index.tsx @@ -27,13 +27,13 @@ import { SideWindowMenuKey } from '../../domain/entities/sideWindow/constants' import { closeBeaconMalfunctionInKanban } from '../../domain/shared_slices/BeaconMalfunction' import { getOperationalAlerts } from '../../domain/use_cases/alert/getOperationalAlerts' import { getSilencedAlerts } from '../../domain/use_cases/alert/getSilencedAlerts' -import { getAllBeaconMalfunctions } from '../../domain/use_cases/beaconMalfunction/getAllBeaconMalfunctions' import { getAllGearCodes } from '../../domain/use_cases/gearCode/getAllGearCodes' import { getInfractions } from '../../domain/use_cases/infraction/getInfractions' import { useMainAppDispatch } from '../../hooks/useMainAppDispatch' import { useMainAppSelector } from '../../hooks/useMainAppSelector' import { SideWindowAlerts } from '../Alert/components/SideWindowAlerts' import { BeaconMalfunctionBoard } from '../BeaconMalfunction/components/BeaconMalfunctionBoard' +import { getAllBeaconMalfunctions } from '../BeaconMalfunction/useCases/getAllBeaconMalfunctions' import { Loader as MissionFormLoader } from '../Mission/components/MissionForm/Loader' import { MissionList } from '../Mission/components/MissionList' import { PriorNotificationList } from '../PriorNotification/components/PriorNotificationList' diff --git a/frontend/src/features/Vessel/components/VesselSidebar/Equipment/VesselEquipment.tsx b/frontend/src/features/Vessel/components/VesselSidebar/Equipment/VesselEquipment.tsx index 45f8f4d1de..7e6edd889f 100644 --- a/frontend/src/features/Vessel/components/VesselSidebar/Equipment/VesselEquipment.tsx +++ b/frontend/src/features/Vessel/components/VesselSidebar/Equipment/VesselEquipment.tsx @@ -9,10 +9,10 @@ import { useIsSuperUser } from '../../../../../auth/hooks/useIsSuperUser' import { COLORS } from '../../../../../constants/constants' import { vesselsAreEquals } from '../../../../../domain/entities/vessel/vessel' import { setBeaconMalfunctionsTab } from '../../../../../domain/shared_slices/BeaconMalfunction' -import { getVesselBeaconMalfunctions } from '../../../../../domain/use_cases/beaconMalfunction/getVesselBeaconMalfunctions' import { useMainAppDispatch } from '../../../../../hooks/useMainAppDispatch' import { useMainAppSelector } from '../../../../../hooks/useMainAppSelector' import { EquipmentTab } from '../../../../BeaconMalfunction/constants' +import { getVesselBeaconMalfunctions } from '../../../../BeaconMalfunction/useCases/getVesselBeaconMalfunctions' export function VesselEquipment() { const dispatch = useMainAppDispatch() diff --git a/frontend/src/features/Vessel/components/VesselSidebar/Equipment/resume/CurrentBeaconMalfunctionBody.tsx b/frontend/src/features/Vessel/components/VesselSidebar/Equipment/resume/CurrentBeaconMalfunctionBody.tsx index 59d44787d7..86c98804a6 100644 --- a/frontend/src/features/Vessel/components/VesselSidebar/Equipment/resume/CurrentBeaconMalfunctionBody.tsx +++ b/frontend/src/features/Vessel/components/VesselSidebar/Equipment/resume/CurrentBeaconMalfunctionBody.tsx @@ -4,9 +4,9 @@ import { SelectPicker } from 'rsuite' import styled from 'styled-components' import { COLORS } from '../../../../../../constants/constants' -import { updateBeaconMalfunctionFromKanban } from '../../../../../../domain/use_cases/beaconMalfunction/updateBeaconMalfunctionFromKanban' import { useMainAppDispatch } from '../../../../../../hooks/useMainAppDispatch' import { VESSEL_STATUS } from '../../../../../BeaconMalfunction/constants' +import { updateBeaconMalfunctionFromKanban } from '../../../../../BeaconMalfunction/useCases/updateBeaconMalfunctionFromKanban' import { getMalfunctionStartDateText } from '../../../../../BeaconMalfunction/utils' import TimeAgoSVG from '../../../../../icons/Label_horaire_VMS.svg?react' @@ -49,6 +49,7 @@ export function CurrentBeaconMalfunctionBody({ dispatch( updateBeaconMalfunctionFromKanban(beaconMalfunction.id, nextBeaconMalfunction, { + stage: nextBeaconMalfunction.stage, vesselStatus: nextBeaconMalfunction.vesselStatus }) ) diff --git a/frontend/src/features/Vessel/components/VesselSidebar/warnings/BeaconMalfunctionWarning.tsx b/frontend/src/features/Vessel/components/VesselSidebar/warnings/BeaconMalfunctionWarning.tsx index c2d8e352cc..8b8eaea125 100644 --- a/frontend/src/features/Vessel/components/VesselSidebar/warnings/BeaconMalfunctionWarning.tsx +++ b/frontend/src/features/Vessel/components/VesselSidebar/warnings/BeaconMalfunctionWarning.tsx @@ -1,8 +1,8 @@ +import { openBeaconMalfunctionInKanban } from '@features/BeaconMalfunction/useCases/openBeaconMalfunctionInKanban' import { openSideWindowPath } from '@features/SideWindow/useCases/openSideWindowPath' import { useMainAppDispatch } from '@hooks/useMainAppDispatch' import { THEME } from '@mtes-mct/monitor-ui' import { SideWindowMenuKey } from 'domain/entities/sideWindow/constants' -import { openBeaconMalfunctionInKanban } from 'domain/use_cases/beaconMalfunction/openBeaconMalfunctionInKanban' import styled from 'styled-components' import BeaconMalfunctionSVG from '../../../../icons/Icone_VMS_dark.svg?react'