generated from gridsuite/gridapp-template
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
261 additions
and
38 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
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,55 @@ | ||
/** | ||
* Copyright (c) 2024, RTE (http://www.rte-france.com) | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
*/ | ||
import { useEffect, useId, useMemo, useState } from 'react'; | ||
import { useDispatch, useSelector } from 'react-redux'; | ||
import { | ||
makeGetNotification, | ||
NotificationSlice, | ||
RootState, | ||
} from '../redux/slices/Notification'; | ||
|
||
const useNotification = (actionTypes: string[]) => { | ||
const [ready, setReady] = useState(false); | ||
const subscriberId = useId(); | ||
const dispatch = useDispatch(); | ||
|
||
const getNotification = useMemo(makeGetNotification, []); | ||
const notification = useSelector((state: RootState) => | ||
getNotification(state, { subscriberId }) | ||
); | ||
|
||
// to subscribe notification center | ||
useEffect(() => { | ||
dispatch( | ||
NotificationSlice.actions.subscribe({ | ||
subscriberId, | ||
actionTypes, | ||
}) | ||
); | ||
|
||
return () => { | ||
// Unsubscribe when component unmounts | ||
dispatch( | ||
NotificationSlice.actions.unsubscribe({ | ||
subscriberId, | ||
actionTypes, | ||
}) | ||
); | ||
}; | ||
}, [dispatch, subscriberId, actionTypes]); | ||
|
||
useEffect(() => { | ||
if (notification) { | ||
setReady(false); | ||
dispatch(NotificationSlice.actions.clear(notification)); | ||
} | ||
}, [notification, dispatch]); | ||
|
||
return { setReady, ready }; | ||
}; | ||
|
||
export default useNotification; |
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,28 @@ | ||
/** | ||
* Copyright (c) 2024, RTE (http://www.rte-france.com) | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
*/ | ||
|
||
// middleware/notificationMiddleware.ts | ||
import { Middleware } from '@reduxjs/toolkit'; | ||
import { NotificationSlice } from '../redux/slices/Notification'; | ||
|
||
const notificationMiddleware: Middleware = | ||
(storeAPI) => (next) => (action: any) => { | ||
const result = next(action); | ||
if ( | ||
!Object.values(NotificationSlice.actions) | ||
.map((action) => action.type) | ||
.some((type) => type === action.type) | ||
) { | ||
// notify only action not in notification slice | ||
storeAPI.dispatch( | ||
NotificationSlice.actions.notify({ actionType: action.type }) | ||
); | ||
} | ||
return result; | ||
}; | ||
|
||
export default notificationMiddleware; |
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
Oops, something went wrong.