-
Notifications
You must be signed in to change notification settings - Fork 2k
/
store.js
178 lines (144 loc) · 5.02 KB
/
store.js
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
/**
* External dependencies
*/
import Immutable from 'immutable';
import partial from 'lodash/partial';
/**
* Internal dependencies
*/
import { actionTypes, appStates } from './constants';
import { createReducerStore } from 'lib/store';
/**
* Module variables
*/
const initialState = Immutable.fromJS( {
count: 0,
importers: {},
importerLocks: {},
api: {
isHydrated: false,
isFetching: false,
retryCount: 0
}
} );
const equals = ( a, b ) => a === b;
const increment = a => a + 1;
const removableStates = [ appStates.CANCEL_PENDING, appStates.DEFUNCT ];
const shouldRemove = importer => removableStates.some( partial( equals, importer.get( 'importerState' ) ) );
const adjustImporterLock = ( state, { action } ) => {
switch ( action.type ) {
case actionTypes.LOCK_IMPORT:
return state.setIn( [ 'importerLocks', action.importerId ], true );
case actionTypes.UNLOCK_IMPORT:
return state.setIn( [ 'importerLocks', action.importerId ], false );
default:
return state;
}
}
const ImporterStore = createReducerStore( function( state, payload ) {
let { action } = payload,
newState;
switch ( action.type ) {
case actionTypes.RESET_STORE:
return initialState;
case actionTypes.DEV_SET_STATE:
// Convert the importer list into an object
action.newState.importers = action.newState.importers
.reduce( ( total, importer ) => Object.assign( total, { [ importer.id ]: importer } ), {} );
newState = Immutable.fromJS( action.newState );
newState = Immutable.is( state, newState ) ? state : newState;
break;
case actionTypes.API_REQUEST:
newState = state.setIn( [ 'api', 'isFetching' ], true );
break;
case actionTypes.API_FAILURE:
newState = state
.setIn( [ 'api', 'isFetching' ], false )
.updateIn( [ 'api', 'retryCount' ], increment );
break;
case actionTypes.API_SUCCESS:
newState = state
.setIn( [ 'api', 'isFetching' ], false )
.setIn( [ 'api', 'isHydrated' ], true )
.setIn( [ 'api', 'retryCount' ], 0 );
break;
case actionTypes.CANCEL_IMPORT:
case actionTypes.RESET_IMPORT:
// Remove the specified importer from the list of current importers
newState = state.update( 'importers', importers => {
return importers.filterNot( importer => importer.get( 'importerId' ) === action.importerId );
} );
break;
case actionTypes.FAIL_UPLOAD:
newState = state
.setIn( [ 'importers', action.importerId, 'importerState' ], appStates.UPLOAD_FAILURE )
.setIn( [ 'importers', action.importerId, 'errorData' ], { type: 'uploadError', description: action.error } );
break;
case actionTypes.FINISH_UPLOAD:
newState = state
.deleteIn( [ 'importers' ], action.importerId )
.setIn( [ 'importers', action.importerStatus.importerId ], Immutable.fromJS( action.importerStatus ) );
break;
case actionTypes.START_MAPPING_AUTHORS:
newState = state.setIn( [ 'importers', action.importerId, 'importerState' ], appStates.MAP_AUTHORS );
break;
case actionTypes.MAP_AUTHORS:
newState = state.updateIn( [ 'importers', action.importerId, 'customData', 'sourceAuthors' ], authors => (
authors.map( author => {
if ( action.sourceAuthor.id !== author.get( 'id' ) ) {
return author;
}
return author.set( 'mappedTo', action.targetAuthor );
} )
) );
break;
case actionTypes.RECEIVE_IMPORT_STATUS:
newState = state.setIn( [ 'api', 'isHydrated' ], true );
if ( newState.getIn( [ 'importerLocks', action.importerStatus.importerId ], false ) ) {
break;
}
if ( action.importerStatus.importerState === appStates.DEFUNCT ) {
newState = newState
.deleteIn( [ 'importers', action.importerStatus.importerId ] );
break;
}
newState = newState
.setIn( [ 'importers', action.importerStatus.importerId ], Immutable.fromJS( action.importerStatus ) )
.update( 'importers', importers => importers.filterNot( shouldRemove ) );
break;
case actionTypes.SET_UPLOAD_PROGRESS:
newState = state.setIn( [ 'importers', action.importerId, 'percentComplete' ],
action.uploadLoaded / ( action.uploadTotal + Number.EPSILON ) * 100
);
break;
case actionTypes.START_IMPORT:
const newImporter = Immutable.fromJS( {
importerId: action.importerId,
type: action.importerType,
importerState: appStates.READY_FOR_UPLOAD,
site: { ID: action.siteId }
} );
newState = state
.update( 'count', count => count + 1 )
.setIn( [ 'importers', action.importerId ], newImporter );
break;
case actionTypes.START_IMPORTING:
newState = state
.setIn( [ 'importers', action.importerId, 'importerState' ], appStates.IMPORTING );
break;
case actionTypes.START_UPLOAD:
newState = state
.setIn( [ 'importers', action.importerId, 'importerState' ], appStates.UPLOADING )
.setIn( [ 'importers', action.importerId, 'filename' ], action.filename );
break;
default:
newState = state;
break;
}
newState = adjustImporterLock( newState, payload );
return newState;
}, initialState );
export function getState() {
return ImporterStore.get().toJS();
}
export default ImporterStore;