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

194 lines
5.2 KiB
TypeScript
Raw Normal View History

/*
* 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.
*/
import { apiClient } from "@scm-manager/ui-components";
import * as types from "../../modules/types";
import { Action, Config } from "@scm-manager/ui-types";
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}`;
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
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));
})
.catch(err => {
dispatch(fetchConfigFailure(err));
2018-08-09 11:36:10 +02:00
});
};
}
export function fetchConfigPending(): Action {
return {
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,
payload: config
2018-08-09 11:36:10 +02:00
};
}
export function fetchConfigFailure(error: Error): Action {
return {
type: FETCH_CONFIG_FAILURE,
payload: {
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();
}
})
.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,
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,
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,
config
}
2018-08-09 16:44:24 +02:00
};
}
2018-08-21 09:17:15 +02:00
export function modifyConfigReset() {
return {
type: MODIFY_CONFIG_RESET
2018-08-21 09:17:15 +02:00
};
}
2018-08-09 11:36:10 +02:00
//reducer
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:
const config = removeNullValues(action.payload);
2018-08-09 11:36:10 +02:00
return {
...state,
entries: config,
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) {
2018-08-09 12:09:50 +02:00
return isPending(state, FETCH_CONFIG);
}
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
export function isModifyConfigPending(state: object) {
return isPending(state, MODIFY_CONFIG);
}
export function getModifyConfigFailure(state: object) {
return getFailure(state, MODIFY_CONFIG);
}
export function getConfig(state: object) {
2018-08-13 08:33:44 +02:00
if (state.config && state.config.entries) {
return state.config.entries;
}
}
export function getConfigUpdatePermission(state: object) {
2018-08-13 08:33:44 +02:00
if (state.config && state.config.configUpdatePermission) {
return state.config.configUpdatePermission;
}
}