Files
SCM-Manager/scm-ui/ui-webapp/src/admin/plugins/modules/plugins.ts

254 lines
6.3 KiB
TypeScript
Raw Normal View History

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}`;
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}`;
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-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 {
type: FETCH_PLUGINS_PENDING,
2019-07-04 17:32:12 +02:00
};
}
export function fetchPluginsSuccess(plugins: PluginCollection): Action {
return {
type: FETCH_PLUGINS_SUCCESS,
payload: plugins,
2019-07-04 17:32:12 +02:00
};
}
export function fetchPluginsFailure(err: Error): Action {
return {
type: FETCH_PLUGINS_FAILURE,
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) {
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: {
name,
2019-07-04 17:32:12 +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,
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,
error,
2019-07-04 17:32:12 +02:00
},
itemId: name,
2019-07-04 17:32:12 +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 {
type: FETCH_PENDING_PLUGINS_PENDING,
};
}
export function fetchPendingPluginsSuccess(PendingPlugins: {}): Action {
return {
type: FETCH_PENDING_PLUGINS_SUCCESS,
payload: PendingPlugins,
};
}
export function fetchPendingPluginsFailure(err: Error): Action {
return {
type: FETCH_PENDING_PLUGINS_FAILURE,
payload: err,
};
}
2019-07-04 17:32:12 +02:00
// reducer
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 {
...state,
2019-07-04 17:32:12 +02:00
list: {
...pluginCollection,
_embedded: {
plugins: names,
},
2019-07-04 17:32:12 +02:00
},
byNames: byNames,
2019-07-04 17:32:12 +02:00
};
}
const reducerByNames = (state: object, plugin: Plugin) => {
2019-07-04 17:32:12 +02:00
return {
...state,
byNames: {
...state.byNames,
[plugin.name]: plugin,
},
2019-07-04 17:32:12 +02:00
};
};
export default function reducer(
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:
return normalizeByName(state, action.payload);
2019-07-04 17:32:12 +02:00
case FETCH_PLUGIN_SUCCESS:
return reducerByNames(state, action.payload);
case FETCH_PENDING_PLUGINS_SUCCESS:
return {
...state,
pending: action.payload,
};
2019-07-04 17:32:12 +02:00
default:
return state;
}
}
// selectors
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: {
plugins,
},
2019-07-04 17:32:12 +02:00
};
}
}
export function isFetchPluginsPending(state: object) {
2019-07-04 17:32:12 +02:00
return isPending(state, FETCH_PLUGINS);
}
export function getFetchPluginsFailure(state: object) {
2019-07-04 17:32:12 +02:00
return getFailure(state, FETCH_PLUGINS);
}
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];
}
}
export function isFetchPluginPending(state: object, name: string) {
2019-07-04 17:32:12 +02:00
return isPending(state, FETCH_PLUGIN, name);
}
export function getFetchPluginFailure(state: object, name: string) {
2019-07-04 17:32:12 +02:00
return getFailure(state, FETCH_PLUGIN, name);
}
export function getPendingPlugins(state: object) {
if (state.plugins && state.plugins.pending) {
return state.plugins.pending;
}
}
export function isFetchPendingPluginsPending(state: object) {
return isPending(state, FETCH_PENDING_PLUGINS);
}
export function getFetchPendingPluginsFailure(state: object) {
return getFailure(state, FETCH_PENDING_PLUGINS);
}