2020-01-13 01:03:22 -06:00
|
|
|
import { createSlice, PayloadAction } from '@reduxjs/toolkit';
|
2018-10-24 07:33:53 -05:00
|
|
|
import { AppNotification, AppNotificationsState } from 'app/types/';
|
2018-10-22 07:22:40 -05:00
|
|
|
|
2018-10-23 09:00:04 -05:00
|
|
|
export const initialState: AppNotificationsState = {
|
|
|
|
appNotifications: [] as AppNotification[],
|
2018-10-22 07:22:40 -05:00
|
|
|
};
|
|
|
|
|
2020-10-22 03:19:30 -05:00
|
|
|
/**
|
|
|
|
* Reducer and action to show toast notifications of various types (success, warnings, errors etc). Use to show
|
|
|
|
* transient info to user, like errors that cannot be otherwise handled or success after an action.
|
|
|
|
*
|
|
|
|
* Use factory functions in core/copy/appNotifications to create the payload.
|
|
|
|
*/
|
2020-01-13 01:03:22 -06:00
|
|
|
const appNotificationsSlice = createSlice({
|
|
|
|
name: 'appNotifications',
|
|
|
|
initialState,
|
|
|
|
reducers: {
|
2021-02-08 03:56:04 -06:00
|
|
|
notifyApp: (state, action: PayloadAction<AppNotification>) => {
|
|
|
|
const newAlert = action.payload;
|
|
|
|
|
|
|
|
for (const existingAlert of state.appNotifications) {
|
|
|
|
if (
|
|
|
|
newAlert.icon === existingAlert.icon &&
|
|
|
|
newAlert.severity === existingAlert.severity &&
|
|
|
|
newAlert.text === existingAlert.text &&
|
|
|
|
newAlert.title === existingAlert.title &&
|
|
|
|
newAlert.component === existingAlert.component
|
|
|
|
) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
state.appNotifications.push(newAlert);
|
|
|
|
},
|
2020-08-26 04:38:39 -05:00
|
|
|
clearAppNotification: (state, action: PayloadAction<string>): AppNotificationsState => ({
|
2020-01-13 01:03:22 -06:00
|
|
|
...state,
|
2021-01-20 00:59:48 -06:00
|
|
|
appNotifications: state.appNotifications.filter((appNotification) => appNotification.id !== action.payload),
|
2020-01-13 01:03:22 -06:00
|
|
|
}),
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
export const { notifyApp, clearAppNotification } = appNotificationsSlice.actions;
|
|
|
|
|
|
|
|
export const appNotificationsReducer = appNotificationsSlice.reducer;
|