/ src / modules / Alert / Alert.reducer.js
Alert.reducer.js
 1  import alertInitialState from '../../common/data/alert'
 2  import reducerUtil from '../../common/utils/reducer'
 3  
 4  const SHOW_ALERT = 'SHOW_ALERT'
 5  const HIDE_ALERT = 'HIDE_ALERT'
 6  
 7  export const showAlertAction = (
 8    msg,
 9    positiveLabel,
10    negativeLabel,
11    positiveListener,
12    negativeListener,
13  ) => ({
14    type: SHOW_ALERT,
15    payload: {
16      msg,
17      positiveLabel,
18      negativeLabel,
19      positiveListener,
20      negativeListener,
21    },
22  })
23  
24  export const hideAlertAction = () => ({
25    type: HIDE_ALERT,
26    payload: null,
27  })
28  
29  const showAlert = (state, payload) => {
30    const {
31      msg,
32      positiveLabel,
33      negativeLabel,
34      positiveListener,
35      negativeListener,
36    } = payload
37  
38    return Object.assign({}, state, {
39      visible: true,
40      msg,
41      positiveLabel: positiveLabel !== undefined ? positiveLabel : 'OK',
42      negativeLabel,
43      positiveListener,
44      negativeListener,
45    })
46  }
47  
48  const hideAlert = state => {
49    return Object.assign({}, state, {
50      visible: false,
51    })
52  }
53  
54  const map = {
55    [SHOW_ALERT]: showAlert,
56    [HIDE_ALERT]: hideAlert,
57  }
58  
59  export default reducerUtil(map, alertInitialState)