messageReducer.js
2.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/**
* # guideReducer.js
*
* The reducer for all the actions from the various log states
*/
'use strict';
/**
* ## Imports
*
* InitialState
*/
import InitialState from './messageInitialState';
import Immutable, {List, Record} from 'immutable';
const initialState = new InitialState;
const {
MESSAGE_DETAIL,
REQUEST_MSG_LIST,
REQUEST_MSG_LIST_SUCCESS,
REQUEST_MSG_LIST_FAILURE,
BATCH_SET_MSG_IS_READ,
BATCH_SET_MSG_IS_READ_SUCCESS,
BATCH_SET_MSG_IS_READ_FAILURE,
REQUEST_MSG_NUM,
REQUEST_MSG_NUM_SUCCESS,
REQUEST_MSG_NUM_FAILURE,
SWITCH_SHOP,
LOGOUT,
} = require('../../constants/actionTypes').default;
/**
* ## guideReducer function
* @param {Object} state - initialState
* @param {Object} action - type and payload
*/
export default function messageReducer(state = initialState, action) {
if (!(state instanceof InitialState)) return initialState.merge(state);
switch (action.type) {
case MESSAGE_DETAIL: {
let nextState = state.set('messageDetail', action.payload);
return nextState;
}
case REQUEST_MSG_LIST: {
let nextState = state.set('isFetching', true)
.set('error', null);
return nextState;
}
case REQUEST_MSG_LIST_SUCCESS: {
let nextState = state.set('isFetching', false)
.set('error', null)
.set('messageList', Immutable.fromJS(action.payload.list));
return nextState;
}
case REQUEST_MSG_LIST_FAILURE: {
return state.set('isFetching', false)
.set('error', action.payload);
}
case REQUEST_MSG_NUM_SUCCESS: {
let nextState = state.set('unreadNum', action.payload);
return nextState;
}
case BATCH_SET_MSG_IS_READ_SUCCESS: {
// console.log(state);
let nextState = state.setIn(['messageList', state.messageDetail, 'isRead'], true);
// console.log(nextState);
return nextState;
}
case REQUEST_MSG_NUM:
case REQUEST_MSG_NUM_FAILURE:
case BATCH_SET_MSG_IS_READ:
return state;
case BATCH_SET_MSG_IS_READ_FAILURE: {
let nextState = state.setIn(['messageList', state.messageDetail, 'isRead'], false);
return nextState;
}
case SWITCH_SHOP:
case LOGOUT:
return initialState;
}
return state;
}