postingReducer.js
4.28 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/**
* # postingReducer.js
*
* The reducer for all the actions from the various log states
*/
'use strict';
/**
* ## Imports
*
* InitialState
*/
import InitialState from './postingInitialState';
import Immutable, {List, Record} from 'immutable';
const {
POSTING_BOARD_REQUEST,
POSTING_BOARD_SUCCESS,
POSTING_BOARD_FAILURE,
POSTING_BOARD_SELECTED,
POSTING_ASSETS_SELECTED,
POSTING_TITLE_EDITED,
POSTING_CONTENT_EDITED,
POSTING_POST_START,
POSTING_POST_FAILURE,
POSTING_UPLOAD_UPDATE,
POSTING_POST_FINISHED,
POSTING_STATE_RESET,
POSTING_STATE_MERGE,
} = require('../../constants/actionTypes').default;
const initialState = new InitialState;
/**
* ## guideReducer function
* @param {Object} state - initialState
* @param {Object} action - type and payload
*/
export default function postingReducer(state = initialState, action) {
if (!(state instanceof InitialState)) return initialState.merge(state);
switch (action.type) {
case POSTING_STATE_MERGE: {
let nextState = initialState.merge(action.payload);
return state.merge(action.payload);
}
break;
case POSTING_STATE_RESET: {
return initialState;
}
break;
case POSTING_TITLE_EDITED: {
let dataValid=false;
if (action.payload.length) {
dataValid = state.currentBoardName.length>0;
}
let nextState = state.set('title',action.payload).set('dataValid',dataValid);
return nextState;
}
break;
case POSTING_CONTENT_EDITED: {
let dataValid=false;
if (action.payload.length) {
dataValid = state.currentBoardName.length>0;
}
let nextState = state.set('content',action.payload).set('dataValid',dataValid);
return nextState;
}
break;
case POSTING_BOARD_REQUEST: {
let nextState = state.set('isFetching', true).set('error', null);
return nextState;
}
break;
case POSTING_BOARD_SUCCESS: {
let boards = action.payload||[];
let nextState = state.set('isFetching',false)
.set('error', null)
.set('boards', Immutable.fromJS(boards));
return nextState;
}
break;
case POSTING_BOARD_FAILURE: {
let nextState = state.set('isFetching', false).set('error', action.payload);
return nextState;
}
break;
case POSTING_BOARD_SELECTED: {
let dataValid=state.title.length>0||state.content.length>0;
let nextState = state.set('currentBoardName',action.payload.boardName).set('currentBoardId',action.payload.boardCode).set('dataValid',dataValid);
return nextState;
}
break;
case POSTING_ASSETS_SELECTED: {
let data = action.payload||[];
let dataValid=false;
if (data.length) {
dataValid = state.currentBoardName.length>0;
}
let nextState = state.set('assets',Immutable.fromJS(data)).set('dataValid',dataValid);
return nextState;
}
break;
case POSTING_POST_START: {
let nextState = state.set('inPosting',true).set('postFail',false).set('postingBannerShow',true).set('uploadState',0);
return nextState;
}
break;
case POSTING_UPLOAD_UPDATE: {
let {assets,finishCount,percent} = action.payload;
let nextState = state.set('assets',Immutable.fromJS(assets))
.set('finishedCount',finishCount)
.set('postPercent',percent);
return nextState;
}
break;
case POSTING_POST_FAILURE: {
let nextState = state.set('inPosting',false).set('postFail',true).set('uploadState',2);
return nextState;
}
break;
case POSTING_POST_FINISHED: {
let success = action.payload;
let upState = success?1:2
let nextState = state.set('inPosting',!success).set('postFail',!success).set('uploadState',upState);
return nextState;
}
break;
default:
break;
}
return state;
}