postingReducer.js
6.24 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/**
* # 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_SUCCESS,
POSTING_POST_FAILURE,
POSTING_IMAGE_UPLOAD_SUCCESS,
POSTING_POST_FINISHED,
POSTING_STATE_RESET,
POSTING_STATE_MERGE,
POSTING_EVENT_LOGOUT,
USER_FORBID_STATE_REQUEST,
USER_FORBID_STATE_SUCCESS,
USER_FORBID_STATE_FAILURE,
} = 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 {canSubmit, userState, post, postState} = action.payload;
let nextState = state.set('canSubmit', canSubmit)
.setIn(['userState', 'forbidNickname'], userState.forbidNickname)
.setIn(['userState', 'forbidSpeaking'], userState.forbidSpeaking)
.setIn(['post', 'title'], post.title)
.setIn(['post', 'content'], post.content)
.setIn(['post', 'sectionId'], post.sectionId)
.setIn(['post', 'sectionName'], post.sectionName)
.setIn(['post', 'assets'], Immutable.fromJS(post.assets))
.setIn(['postState', 'isPosting'], postState.isPosting)
.setIn(['postState', 'error'], postState.error)
.setIn(['postState', 'showProcess'], postState.showProcess)
.setIn(['postState', 'status'], postState.status)
.setIn(['postState', 'stage'], postState.stage)
.setIn(['postState', 'uploadedImageCount'], postState.uploadedImageCount)
.setIn(['postState', 'uploadedImagePercent'], postState.uploadedImagePercent);
return nextState;
}
case POSTING_EVENT_LOGOUT:
case POSTING_STATE_RESET: {
return initialState;
}
case POSTING_TITLE_EDITED: {
let {title, canSubmit} = action.payload;
let nextState = state.set('canSubmit', canSubmit)
.setIn(['post', 'title'], title);
return nextState;
}
case POSTING_CONTENT_EDITED: {
let {content, canSubmit} = action.payload;
let nextState = state.set('canSubmit', canSubmit)
.setIn(['post', 'content'], content);
return nextState;
}
case POSTING_BOARD_SELECTED: {
let {sectionName, sectionId, canSubmit} = action.payload;
let nextState = state.set('canSubmit', canSubmit)
.setIn(['post', 'sectionName'], sectionName)
.setIn(['post', 'sectionId'], sectionId);
return nextState;
}
case POSTING_ASSETS_SELECTED: {
let {assets, canSubmit} = action.payload;
let nextState = state.setIn(['post', 'assets'], Immutable.fromJS(assets)).set('canSubmit',canSubmit);
return nextState;
}
case POSTING_POST_START: {
let nextState = state.setIn(['postState', 'isPosting'], true)
.setIn(['postState', 'error'], null)
.setIn(['postState', 'showProcess'], true)
.setIn(['postState', 'status'], 1);
return nextState;
}
case POSTING_POST_SUCCESS: {
let nextState = state.setIn(['postState', 'isPosting'], false)
.setIn(['postState', 'error'], null)
.setIn(['postState', 'status'], 2);
return nextState;
}
case POSTING_IMAGE_UPLOAD_SUCCESS: {
let {assets, uploadedImageCount, uploadedImagePercent} = action.payload;
let nextState = state.setIn(['post', 'assets'], Immutable.fromJS(assets))
.setIn(['postState', 'uploadedImageCount'], uploadedImageCount)
.setIn(['postState', 'uploadedImagePercent'], uploadedImagePercent);
return nextState;
}
case POSTING_POST_FAILURE: {
let nextState = state.setIn(['postState', 'isPosting'], false)
.setIn(['postState', 'error'], action.payload)
.setIn(['postState', 'status'], 3);
return nextState;
}
case POSTING_BOARD_REQUEST: {
let nextState = state.setIn(['boards', 'isFetching'], true)
.setIn(['boards', 'error'], null);
return nextState;
}
case POSTING_BOARD_SUCCESS: {
let nextState = state.setIn(['boards', 'isFetching'], false)
.setIn(['boards', 'error'], null)
.setIn(['boards', 'list'], Immutable.fromJS(action.payload));
return nextState;
}
case POSTING_BOARD_FAILURE: {
let nextState = state.setIn(['boards', 'isFetching'], false)
.setIn(['boards', 'error'], action.payload);
return nextState;
}
case USER_FORBID_STATE_REQUEST: {
let nextState = state.setIn(['userState', 'isFetching'], true)
.setIn(['userState', 'error'], null);
return nextState;
}
case USER_FORBID_STATE_SUCCESS: {
let {forbidNickname, forbidSpeaking} = action.payload;
let nextState = state.setIn(['userState', 'isFetching'], false)
.setIn(['userState', 'error'], null)
.setIn(['userState', 'forbidNickname'], forbidNickname)
.setIn(['userState', 'forbidSpeaking'], forbidSpeaking);
return nextState;
}
case USER_FORBID_STATE_FAILURE: {
let nextState = state.setIn(['userState', 'isFetching'], false)
.setIn(['userState', 'error'], action.payload);
return nextState;
}
default:
break;
}
return state;
}