textualize/frontend/redux/features/notifications/notificationQueueSlice.ts
Yehoshua Sandler 095c1ca8ec
Refactor notifications to redux (#6)
* feat: make new connections

refact: context groups | feat: area detection

and a bunch of small things. hate yourself for this massive commit

* refact: initial RTK setup for Notifications

* refact: removed Notification Context
2023-09-04 10:02:08 -05:00

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: 'propertyList',
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