2018-08-09 11:36:10 +02:00
|
|
|
// @flow
|
|
|
|
|
import { apiClient } from "../../apiclient";
|
|
|
|
|
import * as types from "../../modules/types";
|
|
|
|
|
import type { Action } from "../../types/Action";
|
2018-08-09 12:09:50 +02:00
|
|
|
import { isPending } from "../../modules/pending";
|
|
|
|
|
import { getFailure } from "../../modules/failure";
|
2018-08-09 11:36:10 +02:00
|
|
|
|
|
|
|
|
export const FETCH_CONFIG = "scm/groups/FETCH_CONFIG";
|
|
|
|
|
export const FETCH_CONFIG_PENDING = `${FETCH_CONFIG}_${types.PENDING_SUFFIX}`;
|
|
|
|
|
export const FETCH_CONFIG_SUCCESS = `${FETCH_CONFIG}_${types.SUCCESS_SUFFIX}`;
|
|
|
|
|
export const FETCH_CONFIG_FAILURE = `${FETCH_CONFIG}_${types.FAILURE_SUFFIX}`;
|
|
|
|
|
|
|
|
|
|
const CONFIG_URL = "config";
|
|
|
|
|
|
|
|
|
|
//fetch config
|
|
|
|
|
export function fetchConfig() {
|
|
|
|
|
return function(dispatch: any) {
|
|
|
|
|
dispatch(fetchConfigPending());
|
|
|
|
|
return apiClient
|
|
|
|
|
.get(CONFIG_URL)
|
|
|
|
|
.then(response => {
|
|
|
|
|
return response.json();
|
|
|
|
|
})
|
|
|
|
|
.then(data => {
|
|
|
|
|
dispatch(fetchConfigSuccess(data));
|
|
|
|
|
})
|
|
|
|
|
.catch(cause => {
|
|
|
|
|
const error = new Error(`could not fetch config: ${cause.message}`);
|
|
|
|
|
dispatch(fetchConfigFailure(error));
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function fetchConfigPending(): Action {
|
|
|
|
|
return {
|
|
|
|
|
type: FETCH_CONFIG_PENDING
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function fetchConfigSuccess(config: any): Action {
|
|
|
|
|
return {
|
|
|
|
|
type: FETCH_CONFIG_SUCCESS,
|
|
|
|
|
payload: config
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function fetchConfigFailure(error: Error): Action {
|
|
|
|
|
return {
|
|
|
|
|
type: FETCH_CONFIG_FAILURE,
|
|
|
|
|
payload: {
|
|
|
|
|
error
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//reducer
|
|
|
|
|
|
|
|
|
|
function reducer(state: any = {}, action: any = {}) {
|
|
|
|
|
switch (action.type) {
|
|
|
|
|
case FETCH_CONFIG_SUCCESS:
|
|
|
|
|
return {
|
|
|
|
|
...state,
|
|
|
|
|
config: {
|
|
|
|
|
entries: action.payload,
|
|
|
|
|
configUpdatePermission: action.payload._links.update ? true : false
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
default:
|
|
|
|
|
return state;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default reducer;
|
2018-08-09 12:09:50 +02:00
|
|
|
|
|
|
|
|
// selectors
|
|
|
|
|
|
|
|
|
|
export function isFetchConfigPending(state: Object) {
|
|
|
|
|
return isPending(state, FETCH_CONFIG);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function getFetchConfigFailure(state: Object) {
|
|
|
|
|
return getFailure(state, FETCH_CONFIG);
|
|
|
|
|
}
|