-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Préavis - Un préavis envoyé reste "En cours de diffusion" (#3693)
## Linked issues - Resolve #3689 ---- - [ ] Tests E2E (Cypress)
- Loading branch information
Showing
9 changed files
with
152 additions
and
26 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
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
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
26 changes: 26 additions & 0 deletions
26
frontend/src/features/PriorNotification/hooks/useInterval.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,26 @@ | ||
import { useEffect, useRef } from 'react' | ||
|
||
export function useInterval(callback: () => void, condition: boolean, delay: number): void { | ||
const savedCallback = useRef<() => void>() | ||
const intervalId = useRef<number | undefined>() | ||
|
||
useEffect(() => { | ||
savedCallback.current = callback | ||
}, [callback]) | ||
|
||
useEffect(() => { | ||
if (!condition) { | ||
return () => clearInterval(intervalId.current) | ||
} | ||
|
||
function tick() { | ||
if (savedCallback.current) { | ||
savedCallback.current() | ||
} | ||
} | ||
|
||
intervalId.current = window.setInterval(tick, delay) | ||
|
||
return () => clearInterval(intervalId.current) | ||
}, [condition, delay]) | ||
} |
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
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
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
68 changes: 68 additions & 0 deletions
68
frontend/src/features/PriorNotification/useCases/refreshPriorNotification.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,68 @@ | ||
import { RtkCacheTagType } from '@api/constants' | ||
import { addSideWindowBanner } from '@features/SideWindow/useCases/addSideWindowBanner' | ||
import { DisplayedErrorKey } from '@libs/DisplayedError/constants' | ||
import { FrontendApiError } from '@libs/FrontendApiError' | ||
import { Level } from '@mtes-mct/monitor-ui' | ||
import { handleThunkError } from '@utils/handleThunkError' | ||
import { displayOrLogError } from 'domain/use_cases/error/displayOrLogError' | ||
|
||
import { priorNotificationApi } from '../priorNotificationApi' | ||
import { priorNotificationActions } from '../slice' | ||
|
||
import type { PriorNotification } from '../PriorNotification.types' | ||
import type { MainAppThunk } from '@store' | ||
|
||
export const refreshPriorNotification = | ||
( | ||
identifier: PriorNotification.Identifier, | ||
fingerprint: string, | ||
isManuallyCreated: boolean | ||
): MainAppThunk<Promise<void>> => | ||
async dispatch => { | ||
try { | ||
const logbookPriorNotification = await dispatch( | ||
priorNotificationApi.endpoints.getPriorNotificationDetail.initiate({ | ||
...identifier, | ||
isManuallyCreated | ||
}) | ||
).unwrap() | ||
|
||
// Update prior notification list if prior notification fingerprint has changed | ||
if (logbookPriorNotification.fingerprint !== fingerprint) { | ||
dispatch(priorNotificationApi.util.invalidateTags([RtkCacheTagType.PriorNotifications])) | ||
} | ||
|
||
// Close card and display a warning banner if prior notification has been deleted (in the meantime) | ||
if (logbookPriorNotification.logbookMessage.isDeleted) { | ||
dispatch(priorNotificationActions.closePriorNotificationCardAndForm()) | ||
dispatch( | ||
addSideWindowBanner({ | ||
children: 'Ce préavis a été supprimé (entre temps).', | ||
closingDelay: 5000, | ||
isClosable: true, | ||
level: Level.WARNING, | ||
withAutomaticClosing: true | ||
}) | ||
) | ||
|
||
return | ||
} | ||
|
||
dispatch(priorNotificationActions.setOpenedPriorNotificationDetail(logbookPriorNotification)) | ||
} catch (err) { | ||
if (err instanceof FrontendApiError) { | ||
dispatch( | ||
displayOrLogError( | ||
err, | ||
() => refreshPriorNotification(identifier, fingerprint, isManuallyCreated), | ||
true, | ||
DisplayedErrorKey.SIDE_WINDOW_PRIOR_NOTIFICATION_FORM_ERROR | ||
) | ||
) | ||
|
||
return | ||
} | ||
|
||
handleThunkError(err) | ||
} | ||
} |
6 changes: 3 additions & 3 deletions
6
...ideWindow/useCases/addMainWindowBanner.ts → ...ideWindow/useCases/addSideWindowBanner.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