2019-10-19 16:38:07 +02:00
|
|
|
import * as types from '../../../modules/types';
|
|
|
|
|
import { isPending } from '../../../modules/pending';
|
|
|
|
|
import { getFailure } from '../../../modules/failure';
|
|
|
|
|
import { Action, Plugin, PluginCollection } from '@scm-manager/ui-types';
|
|
|
|
|
import { apiClient } from '@scm-manager/ui-components';
|
|
|
|
|
|
|
|
|
|
export const FETCH_PLUGINS = 'scm/plugins/FETCH_PLUGINS';
|
2019-07-04 17:32:12 +02:00
|
|
|
export const FETCH_PLUGINS_PENDING = `${FETCH_PLUGINS}_${types.PENDING_SUFFIX}`;
|
|
|
|
|
export const FETCH_PLUGINS_SUCCESS = `${FETCH_PLUGINS}_${types.SUCCESS_SUFFIX}`;
|
|
|
|
|
export const FETCH_PLUGINS_FAILURE = `${FETCH_PLUGINS}_${types.FAILURE_SUFFIX}`;
|
|
|
|
|
|
2019-10-19 16:38:07 +02:00
|
|
|
export const FETCH_PLUGIN = 'scm/plugins/FETCH_PLUGIN';
|
2019-07-04 17:32:12 +02:00
|
|
|
export const FETCH_PLUGIN_PENDING = `${FETCH_PLUGIN}_${types.PENDING_SUFFIX}`;
|
|
|
|
|
export const FETCH_PLUGIN_SUCCESS = `${FETCH_PLUGIN}_${types.SUCCESS_SUFFIX}`;
|
|
|
|
|
export const FETCH_PLUGIN_FAILURE = `${FETCH_PLUGIN}_${types.FAILURE_SUFFIX}`;
|
|
|
|
|
|
2019-10-19 16:38:07 +02:00
|
|
|
export const FETCH_PENDING_PLUGINS = 'scm/plugins/FETCH_PENDING_PLUGINS';
|
|
|
|
|
export const FETCH_PENDING_PLUGINS_PENDING = `${FETCH_PENDING_PLUGINS}_${types.PENDING_SUFFIX}`;
|
|
|
|
|
export const FETCH_PENDING_PLUGINS_SUCCESS = `${FETCH_PENDING_PLUGINS}_${types.SUCCESS_SUFFIX}`;
|
|
|
|
|
export const FETCH_PENDING_PLUGINS_FAILURE = `${FETCH_PENDING_PLUGINS}_${types.FAILURE_SUFFIX}`;
|
2019-09-16 14:31:55 +02:00
|
|
|
|
2019-07-04 17:32:12 +02:00
|
|
|
// fetch plugins
|
|
|
|
|
export function fetchPluginsByLink(link: string) {
|
|
|
|
|
return function(dispatch: any) {
|
|
|
|
|
dispatch(fetchPluginsPending());
|
|
|
|
|
return apiClient
|
|
|
|
|
.get(link)
|
|
|
|
|
.then(response => response.json())
|
|
|
|
|
.then(plugins => {
|
|
|
|
|
dispatch(fetchPluginsSuccess(plugins));
|
|
|
|
|
})
|
|
|
|
|
.catch(err => {
|
|
|
|
|
dispatch(fetchPluginsFailure(err));
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function fetchPluginsPending(): Action {
|
|
|
|
|
return {
|
2019-10-19 16:38:07 +02:00
|
|
|
type: FETCH_PLUGINS_PENDING,
|
2019-07-04 17:32:12 +02:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function fetchPluginsSuccess(plugins: PluginCollection): Action {
|
|
|
|
|
return {
|
|
|
|
|
type: FETCH_PLUGINS_SUCCESS,
|
2019-10-19 16:38:07 +02:00
|
|
|
payload: plugins,
|
2019-07-04 17:32:12 +02:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function fetchPluginsFailure(err: Error): Action {
|
|
|
|
|
return {
|
|
|
|
|
type: FETCH_PLUGINS_FAILURE,
|
2019-10-19 16:38:07 +02:00
|
|
|
payload: err,
|
2019-07-04 17:32:12 +02:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// fetch plugin
|
|
|
|
|
export function fetchPluginByLink(plugin: Plugin) {
|
|
|
|
|
return fetchPlugin(plugin._links.self.href, plugin.name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function fetchPluginByName(link: string, name: string) {
|
2019-10-19 16:38:07 +02:00
|
|
|
const pluginUrl = link.endsWith('/') ? link : link + '/';
|
2019-07-04 17:32:12 +02:00
|
|
|
return fetchPlugin(pluginUrl + name, name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function fetchPlugin(link: string, name: string) {
|
|
|
|
|
return function(dispatch: any) {
|
|
|
|
|
dispatch(fetchPluginPending(name));
|
|
|
|
|
return apiClient
|
|
|
|
|
.get(link)
|
|
|
|
|
.then(response => response.json())
|
|
|
|
|
.then(plugin => {
|
|
|
|
|
dispatch(fetchPluginSuccess(plugin));
|
|
|
|
|
})
|
|
|
|
|
.catch(err => {
|
|
|
|
|
dispatch(fetchPluginFailure(name, err));
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function fetchPluginPending(name: string): Action {
|
|
|
|
|
return {
|
|
|
|
|
type: FETCH_PLUGIN_PENDING,
|
|
|
|
|
payload: {
|
2019-10-19 16:38:07 +02:00
|
|
|
name,
|
2019-07-04 17:32:12 +02:00
|
|
|
},
|
2019-10-19 16:38:07 +02:00
|
|
|
itemId: name,
|
2019-07-04 17:32:12 +02:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function fetchPluginSuccess(plugin: Plugin): Action {
|
|
|
|
|
return {
|
|
|
|
|
type: FETCH_PLUGIN_SUCCESS,
|
|
|
|
|
payload: plugin,
|
2019-10-19 16:38:07 +02:00
|
|
|
itemId: plugin.name,
|
2019-07-04 17:32:12 +02:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function fetchPluginFailure(name: string, error: Error): Action {
|
|
|
|
|
return {
|
|
|
|
|
type: FETCH_PLUGIN_FAILURE,
|
|
|
|
|
payload: {
|
|
|
|
|
name,
|
2019-10-19 16:38:07 +02:00
|
|
|
error,
|
2019-07-04 17:32:12 +02:00
|
|
|
},
|
2019-10-19 16:38:07 +02:00
|
|
|
itemId: name,
|
2019-07-04 17:32:12 +02:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-16 14:31:55 +02:00
|
|
|
// fetch pending plugins
|
|
|
|
|
export function fetchPendingPlugins(link: string) {
|
|
|
|
|
return function(dispatch: any) {
|
|
|
|
|
dispatch(fetchPendingPluginsPending());
|
|
|
|
|
return apiClient
|
|
|
|
|
.get(link)
|
|
|
|
|
.then(response => response.json())
|
|
|
|
|
.then(PendingPlugins => {
|
|
|
|
|
dispatch(fetchPendingPluginsSuccess(PendingPlugins));
|
|
|
|
|
})
|
|
|
|
|
.catch(err => {
|
|
|
|
|
dispatch(fetchPendingPluginsFailure(err));
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function fetchPendingPluginsPending(): Action {
|
|
|
|
|
return {
|
2019-10-19 16:38:07 +02:00
|
|
|
type: FETCH_PENDING_PLUGINS_PENDING,
|
2019-09-16 14:31:55 +02:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function fetchPendingPluginsSuccess(PendingPlugins: {}): Action {
|
|
|
|
|
return {
|
|
|
|
|
type: FETCH_PENDING_PLUGINS_SUCCESS,
|
2019-10-19 16:38:07 +02:00
|
|
|
payload: PendingPlugins,
|
2019-09-16 14:31:55 +02:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function fetchPendingPluginsFailure(err: Error): Action {
|
|
|
|
|
return {
|
|
|
|
|
type: FETCH_PENDING_PLUGINS_FAILURE,
|
2019-10-19 16:38:07 +02:00
|
|
|
payload: err,
|
2019-09-16 14:31:55 +02:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-04 17:32:12 +02:00
|
|
|
// reducer
|
2019-10-19 16:38:07 +02:00
|
|
|
function normalizeByName(state: object, pluginCollection: PluginCollection) {
|
2019-07-04 17:32:12 +02:00
|
|
|
const names = [];
|
|
|
|
|
const byNames = {};
|
|
|
|
|
for (const plugin of pluginCollection._embedded.plugins) {
|
|
|
|
|
names.push(plugin.name);
|
|
|
|
|
byNames[plugin.name] = plugin;
|
|
|
|
|
}
|
|
|
|
|
return {
|
2019-09-16 14:31:55 +02:00
|
|
|
...state,
|
2019-07-04 17:32:12 +02:00
|
|
|
list: {
|
|
|
|
|
...pluginCollection,
|
|
|
|
|
_embedded: {
|
2019-10-19 16:38:07 +02:00
|
|
|
plugins: names,
|
|
|
|
|
},
|
2019-07-04 17:32:12 +02:00
|
|
|
},
|
2019-10-19 16:38:07 +02:00
|
|
|
byNames: byNames,
|
2019-07-04 17:32:12 +02:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-19 16:38:07 +02:00
|
|
|
const reducerByNames = (state: object, plugin: Plugin) => {
|
2019-07-04 17:32:12 +02:00
|
|
|
return {
|
|
|
|
|
...state,
|
|
|
|
|
byNames: {
|
|
|
|
|
...state.byNames,
|
2019-10-19 16:38:07 +02:00
|
|
|
[plugin.name]: plugin,
|
|
|
|
|
},
|
2019-07-04 17:32:12 +02:00
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default function reducer(
|
2019-10-19 16:38:07 +02:00
|
|
|
state: object = {},
|
|
|
|
|
action: Action = {
|
|
|
|
|
type: 'UNKNOWN',
|
|
|
|
|
},
|
|
|
|
|
): object {
|
2019-07-04 17:32:12 +02:00
|
|
|
if (!action.payload) {
|
|
|
|
|
return state;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch (action.type) {
|
|
|
|
|
case FETCH_PLUGINS_SUCCESS:
|
2019-09-16 14:31:55 +02:00
|
|
|
return normalizeByName(state, action.payload);
|
2019-07-04 17:32:12 +02:00
|
|
|
case FETCH_PLUGIN_SUCCESS:
|
|
|
|
|
return reducerByNames(state, action.payload);
|
2019-09-16 14:31:55 +02:00
|
|
|
case FETCH_PENDING_PLUGINS_SUCCESS:
|
2019-10-19 16:38:07 +02:00
|
|
|
return {
|
|
|
|
|
...state,
|
|
|
|
|
pending: action.payload,
|
|
|
|
|
};
|
2019-07-04 17:32:12 +02:00
|
|
|
default:
|
|
|
|
|
return state;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// selectors
|
2019-10-19 16:38:07 +02:00
|
|
|
export function getPluginCollection(state: object) {
|
2019-07-04 17:32:12 +02:00
|
|
|
if (state.plugins && state.plugins.list && state.plugins.byNames) {
|
|
|
|
|
const plugins = [];
|
|
|
|
|
for (let pluginName of state.plugins.list._embedded.plugins) {
|
2019-07-08 14:43:08 +02:00
|
|
|
plugins.push(state.plugins.byNames[pluginName]);
|
2019-07-04 17:32:12 +02:00
|
|
|
}
|
|
|
|
|
return {
|
|
|
|
|
...state.plugins.list,
|
|
|
|
|
_embedded: {
|
2019-10-19 16:38:07 +02:00
|
|
|
plugins,
|
|
|
|
|
},
|
2019-07-04 17:32:12 +02:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-19 16:38:07 +02:00
|
|
|
export function isFetchPluginsPending(state: object) {
|
2019-07-04 17:32:12 +02:00
|
|
|
return isPending(state, FETCH_PLUGINS);
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-19 16:38:07 +02:00
|
|
|
export function getFetchPluginsFailure(state: object) {
|
2019-07-04 17:32:12 +02:00
|
|
|
return getFailure(state, FETCH_PLUGINS);
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-19 16:38:07 +02:00
|
|
|
export function getPlugin(state: object, name: string) {
|
2019-07-04 17:32:12 +02:00
|
|
|
if (state.plugins && state.plugins.byNames) {
|
|
|
|
|
return state.plugins.byNames[name];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-19 16:38:07 +02:00
|
|
|
export function isFetchPluginPending(state: object, name: string) {
|
2019-07-04 17:32:12 +02:00
|
|
|
return isPending(state, FETCH_PLUGIN, name);
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-19 16:38:07 +02:00
|
|
|
export function getFetchPluginFailure(state: object, name: string) {
|
2019-07-04 17:32:12 +02:00
|
|
|
return getFailure(state, FETCH_PLUGIN, name);
|
|
|
|
|
}
|
2019-09-16 14:31:55 +02:00
|
|
|
|
2019-10-19 16:38:07 +02:00
|
|
|
export function getPendingPlugins(state: object) {
|
2019-09-16 14:31:55 +02:00
|
|
|
if (state.plugins && state.plugins.pending) {
|
|
|
|
|
return state.plugins.pending;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-19 16:38:07 +02:00
|
|
|
export function isFetchPendingPluginsPending(state: object) {
|
2019-09-16 14:31:55 +02:00
|
|
|
return isPending(state, FETCH_PENDING_PLUGINS);
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-19 16:38:07 +02:00
|
|
|
export function getFetchPendingPluginsFailure(state: object) {
|
2019-09-16 14:31:55 +02:00
|
|
|
return getFailure(state, FETCH_PENDING_PLUGINS);
|
|
|
|
|
}
|