badguardhome/client/src/reducers/toasts.js

45 lines
1.3 KiB
JavaScript
Raw Normal View History

2019-02-07 12:40:26 +00:00
import { handleActions } from 'redux-actions';
import nanoid from 'nanoid';
2019-05-17 15:17:17 +00:00
import { addErrorToast, addSuccessToast, addNoticeToast, removeToast } from '../actions';
2019-02-07 12:40:26 +00:00
const toasts = handleActions({
[addErrorToast]: (state, { payload }) => {
const errorToast = {
id: nanoid(),
message: payload.error.toString(),
type: 'error',
};
const newState = { ...state, notices: [...state.notices, errorToast] };
return newState;
},
[addSuccessToast]: (state, { payload }) => {
const successToast = {
id: nanoid(),
message: payload,
type: 'success',
};
const newState = { ...state, notices: [...state.notices, successToast] };
return newState;
},
2019-05-17 15:17:17 +00:00
[addNoticeToast]: (state, { payload }) => {
const noticeToast = {
id: nanoid(),
message: payload.error.toString(),
type: 'notice',
};
const newState = { ...state, notices: [...state.notices, noticeToast] };
return newState;
},
2019-02-07 12:40:26 +00:00
[removeToast]: (state, { payload }) => {
const filtered = state.notices.filter(notice => notice.id !== payload);
const newState = { ...state, notices: filtered };
return newState;
},
}, { notices: [] });
export default toasts;