This repository has been archived by the owner on Jun 4, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 278
/
index.js
266 lines (235 loc) · 7.36 KB
/
index.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
import { combineReducers } from 'redux';
import {assoc, assocPath, contains, dissoc, merge, omit, propOr} from 'ramda';
/*
{
// If tabs map to saved connections, then that mapping is stored here.
// If tabs aren't yet saved, then they won't be in this map.
// Tab IDs and Connection IDs are used to index parts of the store below.
tabMap: {
[tabid1]: [connectionId1]
},
selectedTab: tabid1,
selectedTable: {
[tabid1]: 'my-table'
}
// Local store of the connections.
// Preloaded with the response from connectionsRequest
connections: {
[tabid1]: {
username: 'chris',
[...]
}
}
// API Requests that just hold the status and the content
connectionsRequests: {
statusCode: 200,
content: [
{
}
]
},
connectRequests: {
[connectionId]: {
statusCode: 200
}
},
saveConnectionsRequests: {
[tabid1]: {
statusCode: 200
}
}
tablesRequests: {
[connectionId1]: {
status: 200,
content: ['table1', 'table2', ...]
}
},
getElasticsearchMappingsRequests: {
[connectionId1]: {
status: 200,
content: {index1: {}, index2: {}}
}
},
previewTableRequests: {
[connectionId1]: {
status: 200,
content: ...
}
},
s3KeysRequests: {
[connectionId]: {
status: 200,
content: [...]
}
}
apacheDrillStorageRequests: {
[connectionId]: {
status: 200,
content: [...]
}
}
}
*/
// Some of the API reducers we want to reset in order to restart the fetchData flow inside Settings.react.js
// An example of such case is when the credentials are updated and we want to go through
// again the process of fetching tables/files etc. For that to be done easily these API reducers
// should be rester to initial values.
const canBeReset = [
'apacheDrillStorageRequests',
'apacheDrillS3KeysRequests',
'elasticsearchMappingsRequests',
'previewTableRequest',
's3KeysRequests',
'tablesRequests'
];
const isResetCall = (store, type) => contains(store, canBeReset) && type === 'RESET';
function createApiReducer(store) {
return function ApiReducer(state = {}, action) {
let newState = state;
if (action.type === store) {
const {payload} = action;
if (Array.isArray(payload.id)) {
newState = assocPath(payload.id, {
status: payload.status,
content: payload.content
}, state);
} else if (payload.id) {
newState = assoc(payload.id, {
status: payload.status,
content: payload.content
}, state);
} else {
newState = merge(
state,
{status: payload.status, content: payload.content}
);
}
} else if (isResetCall(store, action.type)) {
const {payload} = action;
if (Array.isArray(payload.id)) {
newState = assocPath(payload.id, {}, state);
} else if (payload.id) {
newState = assoc(payload.id, {}, state);
} else {
newState = {};
}
}
return newState;
};
}
export const connectionsRequest = createApiReducer('connectionsRequest');
export const settingsRequest = createApiReducer('settingsRequest');
export const deleteConnectionsRequests = createApiReducer('deleteConnectionsRequests');
export const saveConnectionsRequests = createApiReducer('saveConnectionsRequests');
export const connectorUrlsRequest = createApiReducer('connectorUrlsRequest');
export const apacheDrillStorageRequests = createApiReducer('apacheDrillStorageRequests');
export const apacheDrillS3KeysRequests = createApiReducer('apacheDrillS3KeysRequests');
export const createCertsRequest = createApiReducer('createCertsRequest');
export const connectRequests = createApiReducer('connectRequests');
export const elasticsearchMappingsRequests = createApiReducer('elasticsearchMappingsRequests');
export const previewTableRequests = createApiReducer('previewTableRequest');
export const s3KeysRequests = createApiReducer('s3KeysRequests');
export const tablesRequests = createApiReducer('tablesRequests');
export const schemaRequests = createApiReducer('schemaRequests');
export const queryRequests = createApiReducer('queryRequests');
function tabMap(state = {}, action) {
let newState = state;
if (action.type === 'MERGE_TAB_MAP') {
newState = merge(state, action.payload);
}
return newState;
}
function selectedTab(state = '', action) {
if (action.type === 'SET_TAB') {
return action.payload;
}
return state;
}
function selectedTables(state = {}, action) {
if (action.type === 'SET_TABLE') {
return merge(state, action.payload);
} else if (action.type === 'RESET') {
return merge(state, assoc(action.payload.id, '', state));
}
return state;
}
// object for each tab that tells us if the credentials have been modified since last save to disk
function connectionsNeedToBeSaved(state = {}, action) {
if (action.type === 'SET_CONNECTIONS_NEED_TO_BE_SAVED') {
return assoc(
action.payload.tabId,
action.payload.content,
state
);
}
return state;
}
function selectedIndecies(state = {}, action) {
if (action.type === 'SET_INDEX') {
return merge(state, action.payload);
} else if (action.type === 'RESET') {
return merge(state, assoc(action.payload.id, '', state));
}
return state;
}
function connections(state = {}, action) {
if (action.type === 'MERGE_CONNECTIONS') {
return merge(state, action.payload);
} else if (action.type === 'UPDATE_CREDENTIAL') {
return assoc(
action.payload.tableId,
merge(
state[action.payload.tableId],
action.payload.update
),
state
);
} else if (action.type === 'DELETE_CREDENTIAL') {
return dissoc(action.payload, state);
}
return state;
}
function previews(state = {}, action) {
if (action.type === 'UPDATE_PREVIEW') {
/*
* deep-merge the new payload with the existing state, indexed
* by connectionId
*/
const {connectionId} = action.payload;
return merge(
state,
{
[connectionId]: merge(
propOr({}, connectionId, state),
omit('connectionId', action.payload)
)
}
);
}
return state;
}
const rootReducer = combineReducers({
tabMap,
connections,
createCertsRequest,
selectedTab,
selectedTables,
selectedIndecies,
settingsRequest,
connectRequests,
connectionsRequest,
connectionsNeedToBeSaved,
connectorUrlsRequest,
saveConnectionsRequests,
deleteConnectionsRequests,
tablesRequests,
elasticsearchMappingsRequests,
previewTableRequests,
s3KeysRequests,
apacheDrillStorageRequests,
apacheDrillS3KeysRequests,
schemaRequests,
queryRequests,
previews
});
export default rootReducer;