gab-social/app/javascript/gabsocial/reducers/toasts.js
2020-12-16 02:39:07 -05:00

31 lines
811 B
JavaScript

import {
TOAST_SHOW,
TOAST_DISMISS,
TOAST_CLEAR,
} from '../actions/toasts'
import { Map as ImmutableMap, List as ImmutableList } from 'immutable'
const makeMessageFromData = (data) => {
return `${data.type}`.split('_').join(' ').toLowerCase()
}
const initialState = ImmutableList([])
export default function toasts(state = initialState, action) {
switch(action.type) {
case TOAST_SHOW:
console.log("action:", action)
return state.set(state.size, ImmutableMap({
key: state.size > 0 ? state.last().get('key') + 1 : 0,
message: makeMessageFromData(action.toastData),
type: action.toastType,
}))
case TOAST_DISMISS:
return state.filterNot(item => item.get('key') === action.toastKey)
case TOAST_CLEAR:
return state.clear()
default:
return state
}
}