44 lines
1.3 KiB
TypeScript
44 lines
1.3 KiB
TypeScript
import { createSlice } from '@reduxjs/toolkit'
|
|
import type { PayloadAction } from '@reduxjs/toolkit'
|
|
import { NotificationProps, NotificationQueueState } from './types'
|
|
|
|
const initialState: NotificationQueueState = {
|
|
currentNotification: undefined,
|
|
queue: [],
|
|
}
|
|
|
|
export const notificationQueueSlice = createSlice({
|
|
name: 'notifications',
|
|
initialState,
|
|
reducers: {
|
|
setNotifications: (state, action: PayloadAction<NotificationProps[]>) => {
|
|
state.queue = action.payload
|
|
},
|
|
setCurrentNotification: (state, action: PayloadAction<NotificationProps | undefined>) => {
|
|
state.currentNotification = action.payload
|
|
},
|
|
pushNotification: (state, action: PayloadAction<NotificationProps>) => {
|
|
let { queue } = state
|
|
const { payload: newNotification } = action
|
|
|
|
if (queue.length) queue.push(newNotification)
|
|
else {
|
|
queue.push(newNotification)
|
|
state.currentNotification = newNotification
|
|
}
|
|
},
|
|
dismissCurrentNotification: (state) => {
|
|
state.queue.shift()
|
|
state.currentNotification = state.queue[0] || undefined
|
|
}
|
|
}
|
|
})
|
|
|
|
export const {
|
|
setNotifications,
|
|
setCurrentNotification,
|
|
pushNotification,
|
|
dismissCurrentNotification
|
|
} = notificationQueueSlice.actions
|
|
|
|
export default notificationQueueSlice.reducer |