| 1 |
import { createSlice, PayloadAction } from "@reduxjs/toolkit"; |
| 2 |
import { AlertProps } from "@patternfly/react-core"; |
| 3 |
|
| 4 |
export interface AlertInfo { |
| 5 |
name: string; |
| 6 |
title: React.ReactNode; |
| 7 |
variant: AlertProps["variant"]; |
| 8 |
} |
| 9 |
|
| 10 |
const alertsSlice = createSlice({ |
| 11 |
name: "alerts", |
| 12 |
initialState: [] as AlertInfo[], |
| 13 |
reducers: { |
| 14 |
addAlert: (state, action: PayloadAction<AlertInfo>) => { |
| 15 |
state.push(action.payload); |
| 16 |
}, |
| 17 |
removeAlert: (state, action: PayloadAction<{ name: string }>) => { |
| 18 |
const index = state.findIndex( |
| 19 |
(alert) => alert.name === action.payload.name |
| 20 |
); |
| 21 |
|
| 22 |
if (index !== -1) { |
| 23 |
state.splice(index, 1); |
| 24 |
} |
| 25 |
}, |
| 26 |
removeAllAlerts: (state) => { |
| 27 |
state.length = 0; |
| 28 |
}, |
| 29 |
}, |
| 30 |
}); |
| 31 |
|
| 32 |
export const { addAlert, removeAlert, removeAllAlerts } = alertsSlice.actions; |
| 33 |
|
| 34 |
export default alertsSlice.reducer; |