From e286f5d2dabbf89f99894b6531c9a73ab6760bb5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Delattre?= Date: Fri, 2 Jun 2017 20:58:38 +0200 Subject: [PATCH] Update the alert reducer from switch to map Switch to immutable while we're here --- src/public/js/components/alerts/alert.js | 11 ++++--- src/public/js/reducers/alerts.js | 37 ++++++++++++------------ 2 files changed, 23 insertions(+), 25 deletions(-) diff --git a/src/public/js/components/alerts/alert.js b/src/public/js/components/alerts/alert.js index bcb46ae..5de2c8d 100644 --- a/src/public/js/components/alerts/alert.js +++ b/src/public/js/components/alerts/alert.js @@ -1,17 +1,16 @@ -import React from 'react' - -import SweetAlert from 'react-bootstrap-sweetalert'; +import React from "react" +import SweetAlert from "react-bootstrap-sweetalert"; export default function Alert(props) { - if (!props.alerts.show) { + if (!props.alerts.get("show")) { return null } return ( ) } diff --git a/src/public/js/reducers/alerts.js b/src/public/js/reducers/alerts.js index c6a0f97..e51855d 100644 --- a/src/public/js/reducers/alerts.js +++ b/src/public/js/reducers/alerts.js @@ -1,30 +1,29 @@ -const defaultState = { +import { Map } from "immutable" + +const defaultState = Map({ show: false, message: "", type: "", -}; +}); -export default function Alert(state = defaultState, action) { - switch (action.type) { - case 'ADD_ALERT_ERROR': - return Object.assign({}, state, { - message: action.payload.message, - show: true, - type: "error", - }) - case 'ADD_ALERT_OK': - return Object.assign({}, state, { + +const handlers = { + "ADD_ALERT_ERROR": (state, action) => state.merge(Map({ + message: action.payload.message, + show: true, + type: "error", + })), + "ADD_ALERT_OK": (state, action) => state.merge(Map({ message: action.payload.message, show: true, type: "success", - }) - case 'DISMISS_ALERT': - return Object.assign({}, state, { + })), + "DISMISS_ALERT": state => state.merge(Map({ message: "", show: false, type: "", - }) - default: - return state; - } + })), } + +export default (state = defaultState, action) => + handlers[action.type] ? handlers[action.type](state, action) : state;