-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
integrations.js
36 lines (32 loc) · 1.28 KB
/
integrations.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
import { fromJS } from 'immutable';
import { CONFIG_SUCCESS } from 'Actions/config';
const integrations = (state = null, action) => {
switch (action.type) {
case CONFIG_SUCCESS:
const integrations = action.payload.integrations || [];
const newState = integrations.reduce((acc, integration) => {
const { hooks, collections, provider, ...providerData } = integration;
acc.providers[provider] = { ...providerData };
if (!collections) {
hooks.forEach((hook) => {
acc.hooks[hook] = provider;
});
return acc;
}
const integrationCollections = collections === "*" ? action.payload.collections.map(collection => collection.name) : collections;
integrationCollections.forEach((collection) => {
hooks.forEach((hook) => {
acc.hooks[collection] ? acc.hooks[collection][hook] = provider : acc.hooks[collection] = { [hook]: provider };
});
});
return acc;
}, { providers:{}, hooks: {} });
return fromJS(newState);
default:
return state;
}
};
export const selectIntegration = (state, collection, hook) => (
collection? state.getIn(['hooks', collection, hook], false) : state.getIn(['hooks', hook], false)
);
export default integrations;