2018-10-05 11:02:00 +02:00
|
|
|
// @flow
|
|
|
|
|
import * as types from "./types";
|
|
|
|
|
|
|
|
|
|
import { apiClient } from "@scm-manager/ui-components";
|
|
|
|
|
import type { Action, IndexResources } from "@scm-manager/ui-types";
|
2018-10-05 12:11:35 +02:00
|
|
|
import { isPending } from "./pending";
|
|
|
|
|
import { getFailure } from "./failure";
|
2018-10-05 11:02:00 +02:00
|
|
|
|
|
|
|
|
// Action
|
|
|
|
|
|
2018-10-05 13:14:33 +02:00
|
|
|
export const FETCH_INDEXRESOURCES = "scm/INDEXRESOURCES";
|
2018-10-05 11:02:00 +02:00
|
|
|
export const FETCH_INDEXRESOURCES_PENDING = `${FETCH_INDEXRESOURCES}_${
|
|
|
|
|
types.PENDING_SUFFIX
|
|
|
|
|
}`;
|
|
|
|
|
export const FETCH_INDEXRESOURCES_SUCCESS = `${FETCH_INDEXRESOURCES}_${
|
|
|
|
|
types.SUCCESS_SUFFIX
|
|
|
|
|
}`;
|
|
|
|
|
export const FETCH_INDEXRESOURCES_FAILURE = `${FETCH_INDEXRESOURCES}_${
|
|
|
|
|
types.FAILURE_SUFFIX
|
|
|
|
|
}`;
|
|
|
|
|
|
|
|
|
|
const INDEX_RESOURCES_LINK = "/";
|
|
|
|
|
|
|
|
|
|
export function fetchIndexResources() {
|
|
|
|
|
return function(dispatch: any) {
|
|
|
|
|
dispatch(fetchIndexResourcesPending());
|
|
|
|
|
return apiClient
|
|
|
|
|
.get(INDEX_RESOURCES_LINK)
|
|
|
|
|
.then(response => response.json())
|
|
|
|
|
.then(resources => {
|
|
|
|
|
dispatch(fetchIndexResourcesSuccess(resources));
|
|
|
|
|
})
|
|
|
|
|
.catch(err => {
|
|
|
|
|
dispatch(fetchIndexResourcesFailure(err));
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function fetchIndexResourcesPending(): Action {
|
|
|
|
|
return {
|
|
|
|
|
type: FETCH_INDEXRESOURCES_PENDING
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function fetchIndexResourcesSuccess(resources: IndexResources): Action {
|
|
|
|
|
return {
|
|
|
|
|
type: FETCH_INDEXRESOURCES_SUCCESS,
|
|
|
|
|
payload: resources
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function fetchIndexResourcesFailure(err: Error): Action {
|
|
|
|
|
return {
|
|
|
|
|
type: FETCH_INDEXRESOURCES_FAILURE,
|
|
|
|
|
payload: err
|
|
|
|
|
};
|
|
|
|
|
}
|
2018-10-05 11:15:19 +02:00
|
|
|
|
|
|
|
|
// reducer
|
|
|
|
|
export default function reducer(
|
|
|
|
|
state: Object = {},
|
|
|
|
|
action: Action = { type: "UNKNOWN" }
|
|
|
|
|
): Object {
|
|
|
|
|
if (!action.payload) {
|
|
|
|
|
return state;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch (action.type) {
|
|
|
|
|
case FETCH_INDEXRESOURCES_SUCCESS:
|
|
|
|
|
return {
|
|
|
|
|
...state,
|
2018-10-05 11:28:27 +02:00
|
|
|
links: action.payload._links
|
2018-10-05 11:15:19 +02:00
|
|
|
};
|
|
|
|
|
default:
|
|
|
|
|
return state;
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-10-05 12:11:35 +02:00
|
|
|
|
|
|
|
|
// selectors
|
|
|
|
|
|
|
|
|
|
export function isFetchIndexResourcesPending(state: Object) {
|
|
|
|
|
return isPending(state, FETCH_INDEXRESOURCES);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function getFetchIndexResourcesFailure(state: Object) {
|
|
|
|
|
return getFailure(state, FETCH_INDEXRESOURCES);
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-05 13:48:21 +02:00
|
|
|
export function getLinks(state: Object) {
|
2018-10-05 13:14:33 +02:00
|
|
|
return state.indexResources.links;
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-05 12:11:35 +02:00
|
|
|
export function getUiPluginsLink(state: Object) {
|
2018-10-05 13:48:21 +02:00
|
|
|
if (state.indexResources.links && state.indexResources.links["uiPlugins"])
|
|
|
|
|
return state.indexResources.links["uiPlugins"].href;
|
|
|
|
|
return undefined;
|
2018-10-05 12:11:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function getMeLink(state: Object) {
|
2018-10-05 13:48:21 +02:00
|
|
|
if (state.indexResources.links && state.indexResources.links["me"])
|
2018-10-05 12:11:35 +02:00
|
|
|
return state.indexResources.links["me"].href;
|
|
|
|
|
return undefined;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function getLogoutLink(state: Object) {
|
2018-10-05 13:48:21 +02:00
|
|
|
if (state.indexResources.links && state.indexResources.links["logout"])
|
2018-10-05 12:11:35 +02:00
|
|
|
return state.indexResources.links["logout"].href;
|
|
|
|
|
return undefined;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function getLoginLink(state: Object) {
|
2018-10-05 13:48:21 +02:00
|
|
|
if (state.indexResources.links && state.indexResources.links["login"])
|
2018-10-05 12:11:35 +02:00
|
|
|
return state.indexResources.links["login"].href;
|
|
|
|
|
return undefined;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function getUsersLink(state: Object) {
|
2018-10-05 13:48:21 +02:00
|
|
|
if (state.indexResources.links && state.indexResources.links["users"])
|
2018-10-05 12:11:35 +02:00
|
|
|
return state.indexResources.links["users"].href;
|
|
|
|
|
return undefined;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function getGroupsLink(state: Object) {
|
2018-10-05 13:48:21 +02:00
|
|
|
if (state.indexResources.links && state.indexResources.links["groups"])
|
2018-10-05 12:11:35 +02:00
|
|
|
return state.indexResources.links["groups"].href;
|
|
|
|
|
return undefined;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function getConfigLink(state: Object) {
|
2018-10-05 13:48:21 +02:00
|
|
|
if (state.indexResources.links && state.indexResources.links["config"])
|
2018-10-05 12:11:35 +02:00
|
|
|
return state.indexResources.links["config"].href;
|
|
|
|
|
return undefined;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function getRepositoriesLink(state: Object) {
|
2018-10-05 13:48:21 +02:00
|
|
|
if (state.indexResources.links && state.indexResources.links["repositories"])
|
2018-10-05 12:11:35 +02:00
|
|
|
return state.indexResources.links["repositories"].href;
|
|
|
|
|
return undefined;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function getHgConfigLink(state: Object) {
|
2018-10-05 13:48:21 +02:00
|
|
|
if (state.indexResources.links && state.indexResources.links["hgConfig"])
|
2018-10-05 12:11:35 +02:00
|
|
|
return state.indexResources.links["hgConfig"].href;
|
|
|
|
|
return undefined;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function getGitConfigLink(state: Object) {
|
2018-10-05 13:48:21 +02:00
|
|
|
if (state.indexResources.links && state.indexResources.links["gitConfig"])
|
2018-10-05 12:11:35 +02:00
|
|
|
return state.indexResources.links["gitConfig"].href;
|
|
|
|
|
return undefined;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function getSvnConfigLink(state: Object) {
|
2018-10-05 13:48:21 +02:00
|
|
|
if (state.indexResources.links && state.indexResources.links["svnConfig"])
|
2018-10-05 12:11:35 +02:00
|
|
|
return state.indexResources.links["svnConfig"].href;
|
|
|
|
|
return undefined;
|
|
|
|
|
}
|