2020-03-23 15:35:58 +01:00
|
|
|
/*
|
|
|
|
|
* MIT License
|
|
|
|
|
*
|
|
|
|
|
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
|
|
|
|
|
*
|
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
|
*
|
|
|
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
|
|
|
* copies or substantial portions of the Software.
|
|
|
|
|
*
|
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
|
* SOFTWARE.
|
|
|
|
|
*/
|
|
|
|
|
|
2019-10-20 18:02:52 +02:00
|
|
|
import { apiClient } from "@scm-manager/ui-components";
|
|
|
|
|
import * as types from "../../modules/types";
|
2020-01-08 15:57:13 +01:00
|
|
|
import { Action, Config } from "@scm-manager/ui-types";
|
2019-10-20 18:02:52 +02:00
|
|
|
import { isPending } from "../../modules/pending";
|
|
|
|
|
import { getFailure } from "../../modules/failure";
|
|
|
|
|
import { Dispatch } from "redux";
|
|
|
|
|
|
|
|
|
|
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}`;
|
|
|
|
|
|
2019-10-20 18:02:52 +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
|
|
|
|
2019-10-20 18:02:52 +02:00
|
|
|
const CONTENT_TYPE_CONFIG = "application/vnd.scmm-config+json;v=2";
|
2018-08-09 11:36:10 +02:00
|
|
|
|
|
|
|
|
//fetch config
|
2018-10-09 16:17:25 +02:00
|
|
|
export function fetchConfig(link: string) {
|
|
|
|
|
return function(dispatch: any) {
|
2018-08-09 11:36:10 +02:00
|
|
|
dispatch(fetchConfigPending());
|
|
|
|
|
return apiClient
|
2018-10-09 16:17:25 +02:00
|
|
|
.get(link)
|
2018-08-09 11:36:10 +02:00
|
|
|
.then(response => {
|
|
|
|
|
return response.json();
|
|
|
|
|
})
|
|
|
|
|
.then(data => {
|
|
|
|
|
dispatch(fetchConfigSuccess(data));
|
|
|
|
|
})
|
2018-12-12 09:40:37 +01:00
|
|
|
.catch(err => {
|
|
|
|
|
dispatch(fetchConfigFailure(err));
|
2018-08-09 11:36:10 +02:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function fetchConfigPending(): Action {
|
|
|
|
|
return {
|
2019-10-20 18:02:52 +02:00
|
|
|
type: FETCH_CONFIG_PENDING
|
2018-08-09 11:36:10 +02:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
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,
|
2019-10-20 18:02:52 +02:00
|
|
|
payload: config
|
2018-08-09 11:36:10 +02:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function fetchConfigFailure(error: Error): Action {
|
|
|
|
|
return {
|
|
|
|
|
type: FETCH_CONFIG_FAILURE,
|
|
|
|
|
payload: {
|
2019-10-20 18:02:52 +02:00
|
|
|
error
|
|
|
|
|
}
|
2018-08-09 11:36:10 +02:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
})
|
2018-12-12 09:40:37 +01:00
|
|
|
.catch(err => {
|
|
|
|
|
dispatch(modifyConfigFailure(config, err));
|
2018-08-09 16:44:24 +02:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
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,
|
2019-10-20 18:02:52 +02:00
|
|
|
payload: config
|
2018-08-09 16:44:24 +02:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
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,
|
2019-10-20 18:02:52 +02:00
|
|
|
payload: config
|
2018-08-09 16:44:24 +02:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
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,
|
2019-10-20 18:02:52 +02:00
|
|
|
config
|
|
|
|
|
}
|
2018-08-09 16:44:24 +02:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-21 09:17:15 +02:00
|
|
|
export function modifyConfigReset() {
|
|
|
|
|
return {
|
2019-10-20 18:02:52 +02:00
|
|
|
type: MODIFY_CONFIG_RESET
|
2018-08-21 09:17:15 +02:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
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) {
|
2018-08-30 10:10:41 +02:00
|
|
|
case MODIFY_CONFIG_SUCCESS:
|
2018-08-09 11:36:10 +02:00
|
|
|
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,
|
2019-10-20 18:02:52 +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
|
|
|
|
|
|
2019-10-19 16:38:07 +02:00
|
|
|
export function isFetchConfigPending(state: object) {
|
2018-08-09 12:09:50 +02:00
|
|
|
return isPending(state, FETCH_CONFIG);
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-19 16:38:07 +02:00
|
|
|
export function getFetchConfigFailure(state: object) {
|
2018-08-09 12:09:50 +02:00
|
|
|
return getFailure(state, FETCH_CONFIG);
|
|
|
|
|
}
|
2018-08-13 08:33:44 +02:00
|
|
|
|
2019-10-19 16:38:07 +02:00
|
|
|
export function isModifyConfigPending(state: object) {
|
2018-08-13 08:43:13 +02:00
|
|
|
return isPending(state, MODIFY_CONFIG);
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-19 16:38:07 +02:00
|
|
|
export function getModifyConfigFailure(state: object) {
|
2018-08-13 08:43:13 +02:00
|
|
|
return getFailure(state, MODIFY_CONFIG);
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-19 16:38:07 +02:00
|
|
|
export function getConfig(state: object) {
|
2018-08-13 08:33:44 +02:00
|
|
|
if (state.config && state.config.entries) {
|
|
|
|
|
return state.config.entries;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-19 16:38:07 +02:00
|
|
|
export function getConfigUpdatePermission(state: object) {
|
2018-08-13 08:33:44 +02:00
|
|
|
if (state.config && state.config.configUpdatePermission) {
|
|
|
|
|
return state.config.configUpdatePermission;
|
|
|
|
|
}
|
|
|
|
|
}
|