2018-02-28 15:45:16 -05:00
|
|
|
import { fromJS, List } from 'immutable';
|
2019-12-18 18:16:02 +02:00
|
|
|
import { CONFIG_SUCCESS } from '../actions/config';
|
|
|
|
import { Integrations, IntegrationsAction, Integration } from '../types/redux';
|
2016-10-10 15:34:21 -03:00
|
|
|
|
2019-12-18 18:16:02 +02:00
|
|
|
interface Acc {
|
|
|
|
providers: Record<string, {}>;
|
|
|
|
hooks: Record<string, string | Record<string, string>>;
|
|
|
|
}
|
|
|
|
|
|
|
|
const integrations = (state = null, action: IntegrationsAction): Integrations | null => {
|
2016-10-10 15:34:21 -03:00
|
|
|
switch (action.type) {
|
2018-07-27 09:13:52 -06:00
|
|
|
case CONFIG_SUCCESS: {
|
2019-12-18 18:16:02 +02:00
|
|
|
const integrations: Integration[] = action.payload.get('integrations', List()).toJS() || [];
|
2018-08-07 14:46:54 -06:00
|
|
|
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 === '*'
|
2019-12-18 18:16:02 +02:00
|
|
|
? action.payload.get('collections').map(collection => collection.get('name'))
|
|
|
|
: (collections as string[]);
|
2018-08-07 14:46:54 -06:00
|
|
|
integrationCollections.forEach(collection => {
|
|
|
|
hooks.forEach(hook => {
|
|
|
|
acc.hooks[collection]
|
2019-12-18 18:16:02 +02:00
|
|
|
? ((acc.hooks[collection] as Record<string, string>)[hook] = provider)
|
2018-08-07 14:46:54 -06:00
|
|
|
: (acc.hooks[collection] = { [hook]: provider });
|
|
|
|
});
|
2017-01-10 22:23:22 -02:00
|
|
|
});
|
|
|
|
return acc;
|
2018-08-07 14:46:54 -06:00
|
|
|
},
|
2019-12-18 18:16:02 +02:00
|
|
|
{ providers: {}, hooks: {} } as Acc,
|
2018-08-07 14:46:54 -06:00
|
|
|
);
|
2016-10-10 15:34:21 -03:00
|
|
|
return fromJS(newState);
|
2018-07-27 09:13:52 -06:00
|
|
|
}
|
2016-10-10 15:34:21 -03:00
|
|
|
default:
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-12-18 18:16:02 +02:00
|
|
|
export const selectIntegration = (state: Integrations, collection: string | null, hook: string) =>
|
2018-08-07 14:46:54 -06:00
|
|
|
collection
|
|
|
|
? state.getIn(['hooks', collection, hook], false)
|
|
|
|
: state.getIn(['hooks', hook], false);
|
2016-10-10 15:34:21 -03:00
|
|
|
|
|
|
|
export default integrations;
|