forked from WorldBrain/Memex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reducer.js
196 lines (180 loc) · 6.15 KB
/
reducer.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import { createReducer } from 'redux-act'
import * as actions from './actions'
import {
STATUS,
FILTERS,
IMPORT_TYPE as TYPE,
DOWNLOAD_STATUS as DL_STAT,
DEF_CONCURRENCY,
} from './constants'
const defaultStats = {
[TYPE.HISTORY]: 0,
[TYPE.BOOKMARK]: 0,
[TYPE.OTHERS]: 0,
}
const defaultState = {
processErrors: false,
downloadData: [],
completed: defaultStats, // Count of docs already in DB (estimates view)
fail: defaultStats, // Fail counts for completed import items
success: defaultStats, // Success counts for completed import items
totals: defaultStats, // Static state to use to derive remaining counts from
importStatus: STATUS.LOADING,
loadingMsg: 'Calculating size of bookmarks',
downloadDataFilter: FILTERS.FAIL,
concurrency: DEF_CONCURRENCY,
isAdvEnabled: false,
allowTypes: {
[TYPE.HISTORY]: false,
[TYPE.BOOKMARK]: false,
[TYPE.OTHERS]: '',
},
showDownloadDetails: false,
blobUrl: null,
indexTitle: false,
}
/**
* A sub-reducer that either the success or fail count of a given import type, depending on success flag.
* @param {any} state The entire state of the parent reducer.
* @param {string} type The import type to update the count of (bookmarks/history)
* @param {isSuccess} boolean Denotes whether or not to update 'success' or 'fail' count.
*/
const updateCountReducer = (state, type, isSuccess) => {
const stateKey = isSuccess ? 'success' : 'fail'
return {
[stateKey]: { ...state[stateKey], [type]: state[stateKey][type] + 1 },
}
}
/**
* Add given download details as a new row, as well as triggering an update of counts
*/
const addDownloadDetailsReducer = (
state,
{ url, type, status = DL_STAT.FAIL, error },
) => {
// Ignore all details data apart from errors
const downloadData =
status !== DL_STAT.FAIL
? state.downloadData
: [{ url, type, status, error }, ...state.downloadData]
return {
...state,
...updateCountReducer(state, type, status === DL_STAT.SUCC),
downloadData,
}
}
const toggleAllowTypeReducer = (state, type) => ({
...state,
allowTypes: {
...state.allowTypes,
[type]: !state.allowTypes[type],
},
})
const setAllowTypeReducer = (state, value) => ({
...state,
allowTypes: {
...state.allowTypes,
[TYPE.OTHERS]: state.allowTypes[TYPE.OTHERS].length === 0 ? value : '',
},
})
const finishImportsReducer = ({ loading = false, finish = true }) => (
state,
) => ({
...state,
allowTypes: finish ? defaultState.allowTypes : state.allowTypes,
importStatus: loading ? STATUS.LOADING : STATUS.IDLE,
blobUrl: finish ? null : state.blobUrl,
downloadData: [],
success: defaultStats,
fail: defaultStats,
})
const prepareImportReducer = (state) => {
return {
...state,
importStatus: STATUS.LOADING,
loadingMsg: 'Calculating size of history & bookmarks',
}
}
const cancelImportReducer = (state) => ({
...state,
importStatus: STATUS.LOADING,
blobUrl: null,
loadingMsg: 'Import progress gets saved',
})
const initEstimateCounts = (state, { remaining, completed }) => ({
...state,
totals: { ...state.totals, ...remaining },
completed: { ...state.completed, ...completed },
})
// Sets whatever key to the specified val
const genericReducer = (key, val) => (state) => ({ ...state, [key]: val })
// Sets whatever key to payload
const payloadReducer = (key) => (state, payload) => ({
...state,
[key]: payload,
})
// Simple reducers constructed for state keys
const setImportState = (val) => genericReducer('importStatus', val)
export default createReducer(
{
[actions.prepareImport]: prepareImportReducer,
[actions.startImport]: setImportState(STATUS.RUNNING),
[actions.stopImport]: setImportState(STATUS.STOPPED),
[actions.finishImport]: finishImportsReducer({ loading: true }),
[actions.readyImport]: finishImportsReducer({
loading: false,
finish: false,
}),
[actions.cancelImport]: cancelImportReducer,
[actions.pauseImport]: setImportState(STATUS.PAUSED),
[actions.resumeImport]: setImportState(STATUS.RUNNING),
[actions.addImportItem]: addDownloadDetailsReducer,
[actions.toggleAllowType]: toggleAllowTypeReducer,
[actions.setAllowType]: setAllowTypeReducer,
[actions.filterDownloadDetails]: payloadReducer('downloadDataFilter'),
[actions.initImportState]: payloadReducer('importStatus'),
[actions.initEstimateCounts]: initEstimateCounts,
[actions.initTotalsCounts]: payloadReducer('totals'),
[actions.initFailCounts]: payloadReducer('fail'),
[actions.initSuccessCounts]: payloadReducer('success'),
[actions.initDownloadData]: payloadReducer('downloadData'),
// Adv settings mode reducers
[actions.initAllowTypes]: (state, allowTypes) => ({
...state,
allowTypes: {
[TYPE.BOOKMARK]:
allowTypes[TYPE.BOOKMARK] ||
defaultState.allowTypes[TYPE.BOOKMARK],
[TYPE.HISTORY]:
allowTypes[TYPE.HISTORY] ||
defaultState.allowTypes[TYPE.HISTORY],
[TYPE.OTHERS]:
allowTypes[TYPE.OTHERS] ||
defaultState.allowTypes[TYPE.OTHERS],
},
}),
[actions.setConcurrency]: (state, concurrency) => ({
...state,
concurrency,
}),
[actions.showDownloadDetails]: (state) => ({
...state,
showDownloadDetails: !state.showDownloadDetails,
}),
[actions.setProcessErrs]: (state, processErrors) => ({
...state,
processErrors,
importStatus: STATUS.LOADING,
loadingMsg: 'Preparing Import.',
}),
[actions.setBlobUrl]: (state, blobUrl) => ({
...state,
blobUrl,
}),
[actions.toggleIndexTitle]: (state) => ({
...state,
indexTitle: !state.indexTitle,
}),
},
defaultState,
)