This repository was archived by the owner on May 17, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstore.js
123 lines (109 loc) · 3.42 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
import { inputExpressionChanged } from 'modules/input-expression';
import { modalOpenChanged } from 'modules/modal-open';
import { modeChanged } from 'modules/mode';
import { uriChanged } from 'modules/uri';
import { runTranspiler } from 'modules/run-transpiler';
import { copyToClipboardFnChanged } from 'modules/copy-to-clipboard';
import { namespaceChanged } from 'modules/namespace';
import { createStore, applyMiddleware } from 'redux';
import thunk from 'redux-thunk';
import {
localAppRegistryActivated,
globalAppRegistryActivated
} from 'mongodb-redux-common/app-registry';
import reducer from 'modules';
/**
* Set the custom copy to clipboard function.
*
* @param {Store} store - The store.
* @param {Function} fn - The function.
*/
export const setCopyToClipboardFn = (store, fn) => {
store.dispatch(copyToClipboardFnChanged(fn));
};
/**
* Set the data provider.
*
* @param {Store} store - The store.
* @param {Error} error - The error (if any) while connecting.
* @param {Object} provider - The data provider.
*/
export const setDataProvider = (store, error, provider) => {
store.dispatch(uriChanged(provider.client.model.driverUrl));
};
/**
* Set the namespace in the store.
*
* @param {Store} store - The store.
* @param {String} ns - The namespace in "db.collection" format.
*/
export const setNamespace = (store, ns) => {
store.dispatch(namespaceChanged(ns));
};
/**
* Configure the store for use.
*
* @param {Object} options - The options.
*
* @returns {Store} The store.
*/
const configureStore = (options = {}) => {
const store = createStore(reducer, applyMiddleware(thunk));
if (options.localAppRegistry) {
const localAppRegistry = options.localAppRegistry;
store.dispatch(localAppRegistryActivated(localAppRegistry));
localAppRegistry.on('open-aggregation-export-to-language', (aggregation) => {
store.dispatch(modeChanged('Pipeline'));
store.dispatch(modalOpenChanged(true));
store.dispatch(runTranspiler({ aggregation: aggregation }));
store.dispatch(inputExpressionChanged({ aggregation: aggregation }));
});
localAppRegistry.on('open-query-export-to-language', (queryStrings) => {
let query = {};
if (typeof queryStrings === 'string') {
query.filter = queryStrings === '' ? '{}' : queryStrings;
} else {
[
'filter',
'project',
'sort',
'collation',
'skip',
'limit',
'maxTimeMS'
].forEach((k) => {
if (!queryStrings[k] || queryStrings[k] === '') {
if (k === 'filter') {
query[k] = '{}';
}
} else {
query[k] = queryStrings[k];
}
});
}
store.dispatch(modeChanged('Query'));
store.dispatch(modalOpenChanged(true));
store.dispatch(runTranspiler(query));
store.dispatch(inputExpressionChanged(query));
});
}
if (options.globalAppRegistry) {
const globalAppRegistry = options.globalAppRegistry;
store.dispatch(globalAppRegistryActivated(globalAppRegistry));
}
if (options.dataProvider) {
setDataProvider(
store,
options.dataProvider.error,
options.dataProvider.dataProvider
);
}
if (options.namespace) {
setNamespace(store, options.namespace);
}
if (options.copyToClipboardFn) {
store.dispatch(copyToClipboardFnChanged(options.copyToClipboardFn));
}
return store;
};
export default configureStore;