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 16:44:24 +02:00
|
|
|
import { Dispatch } from "redux";
|
2018-08-13 16:35:19 +02:00
|
|
|
import type { Config } from "../types/Config";
|
2018-08-09 11:36:10 +02:00
|
|
|
|
2018-08-09 16:34:08 +02:00
|
|
|
export const FETCH_CONFIG = "scm/config/FETCH_CONFIG";
|
2018-08-09 11:36:10 +02:00
|
|
|
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}`;
|
|
|
|
|
|
2018-08-13 16:35:19 +02:00
|
|
|
export const MODIFY_CONFIG = "scm/config/MODIFY_CONFIG";
|
2018-08-09 16:44:24 +02:00
|
|
|
export const MODIFY_CONFIG_PENDING = `${MODIFY_CONFIG}_${types.PENDING_SUFFIX}`;
|
|
|
|
|
export const MODIFY_CONFIG_SUCCESS = `${MODIFY_CONFIG}_${types.SUCCESS_SUFFIX}`;
|
|
|
|
|
export const MODIFY_CONFIG_FAILURE = `${MODIFY_CONFIG}_${types.FAILURE_SUFFIX}`;
|
2018-08-21 09:17:15 +02:00
|
|
|
export const MODIFY_CONFIG_RESET = `${MODIFY_CONFIG}_${types.RESET_SUFFIX}`;
|
2018-08-09 16:44:24 +02:00
|
|
|
|
2018-08-09 11:36:10 +02:00
|
|
|
const CONFIG_URL = "config";
|
2018-08-09 16:44:24 +02:00
|
|
|
const CONTENT_TYPE_CONFIG = "application/vnd.scmm-config+json;v=2";
|
2018-08-09 11:36:10 +02:00
|
|
|
|
|
|
|
|
//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
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-13 16:35:19 +02:00
|
|
|
export function fetchConfigSuccess(config: Config): Action {
|
2018-08-09 11:36:10 +02:00
|
|
|
return {
|
|
|
|
|
type: FETCH_CONFIG_SUCCESS,
|
|
|
|
|
payload: config
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function fetchConfigFailure(error: Error): Action {
|
|
|
|
|
return {
|
|
|
|
|
type: FETCH_CONFIG_FAILURE,
|
|
|
|
|
payload: {
|
|
|
|
|
error
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-09 16:44:24 +02:00
|
|
|
// modify config
|
2018-08-13 16:35:19 +02:00
|
|
|
export function modifyConfig(config: Config, callback?: () => void) {
|
2018-08-09 16:44:24 +02:00
|
|
|
return function(dispatch: Dispatch) {
|
|
|
|
|
dispatch(modifyConfigPending(config));
|
|
|
|
|
return apiClient
|
2018-08-21 10:48:34 +02:00
|
|
|
.put(config._links.update.href, config, CONTENT_TYPE_CONFIG)
|
2018-08-09 16:44:24 +02:00
|
|
|
.then(() => {
|
|
|
|
|
dispatch(modifyConfigSuccess(config));
|
|
|
|
|
if (callback) {
|
|
|
|
|
callback();
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.catch(cause => {
|
|
|
|
|
dispatch(
|
|
|
|
|
modifyConfigFailure(
|
|
|
|
|
config,
|
|
|
|
|
new Error(`could not modify config: ${cause.message}`)
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-13 16:35:19 +02:00
|
|
|
export function modifyConfigPending(config: Config): Action {
|
2018-08-09 16:44:24 +02:00
|
|
|
return {
|
|
|
|
|
type: MODIFY_CONFIG_PENDING,
|
|
|
|
|
payload: config
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-13 16:35:19 +02:00
|
|
|
export function modifyConfigSuccess(config: Config): Action {
|
2018-08-09 16:44:24 +02:00
|
|
|
return {
|
|
|
|
|
type: MODIFY_CONFIG_SUCCESS,
|
|
|
|
|
payload: config
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-13 16:35:19 +02:00
|
|
|
export function modifyConfigFailure(config: Config, error: Error): Action {
|
2018-08-09 16:44:24 +02:00
|
|
|
return {
|
|
|
|
|
type: MODIFY_CONFIG_FAILURE,
|
|
|
|
|
payload: {
|
|
|
|
|
error,
|
|
|
|
|
config
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-21 09:17:15 +02:00
|
|
|
export function modifyConfigReset() {
|
|
|
|
|
return {
|
|
|
|
|
type: MODIFY_CONFIG_RESET
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-09 11:36:10 +02:00
|
|
|
//reducer
|
|
|
|
|
|
2018-08-30 09:47:33 +02:00
|
|
|
function removeNullValues(config: Config) {
|
|
|
|
|
if (!config.adminGroups) {
|
|
|
|
|
config.adminGroups = [];
|
|
|
|
|
}
|
|
|
|
|
if (!config.adminUsers) {
|
|
|
|
|
config.adminUsers = [];
|
|
|
|
|
}
|
|
|
|
|
if (!config.proxyExcludes) {
|
|
|
|
|
config.proxyExcludes = [];
|
|
|
|
|
}
|
|
|
|
|
return config;
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-09 11:36:10 +02:00
|
|
|
function reducer(state: any = {}, action: any = {}) {
|
|
|
|
|
switch (action.type) {
|
|
|
|
|
case FETCH_CONFIG_SUCCESS:
|
2018-08-30 09:47:33 +02:00
|
|
|
const config = removeNullValues(action.payload);
|
2018-08-09 11:36:10 +02:00
|
|
|
return {
|
|
|
|
|
...state,
|
2018-08-30 09:47:33 +02:00
|
|
|
entries: config,
|
2018-08-09 13:05:59 +02:00
|
|
|
configUpdatePermission: action.payload._links.update ? true : false
|
2018-08-09 11:36:10 +02:00
|
|
|
};
|
|
|
|
|
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);
|
|
|
|
|
}
|
2018-08-13 08:33:44 +02:00
|
|
|
|
2018-08-13 08:43:13 +02:00
|
|
|
export function isModifyConfigPending(state: Object) {
|
|
|
|
|
return isPending(state, MODIFY_CONFIG);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function getModifyConfigFailure(state: Object) {
|
|
|
|
|
return getFailure(state, MODIFY_CONFIG);
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-13 08:33:44 +02:00
|
|
|
export function getConfig(state: Object) {
|
|
|
|
|
if (state.config && state.config.entries) {
|
|
|
|
|
return state.config.entries;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function getConfigUpdatePermission(state: Object) {
|
|
|
|
|
if (state.config && state.config.configUpdatePermission) {
|
|
|
|
|
return state.config.configUpdatePermission;
|
|
|
|
|
}
|
|
|
|
|
}
|