diff --git a/scm-ui/src/config/modules/config.js b/scm-ui/src/config/modules/config.js index e7a8e87e98..2e51693719 100644 --- a/scm-ui/src/config/modules/config.js +++ b/scm-ui/src/config/modules/config.js @@ -4,13 +4,20 @@ import * as types from "../../modules/types"; import type { Action } from "../../types/Action"; import { isPending } from "../../modules/pending"; import { getFailure } from "../../modules/failure"; +import { Dispatch } from "redux"; export const FETCH_CONFIG = "scm/config/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}`; +export const MODIFY_CONFIG = "scm/config/FETCH_CONFIG"; +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}`; + const CONFIG_URL = "config"; +const CONTENT_TYPE_CONFIG = "application/vnd.scmm-config+json;v=2"; //fetch config export function fetchConfig() { @@ -53,6 +60,53 @@ export function fetchConfigFailure(error: Error): Action { }; } +// modify config +export function modifyConfig(config: any, callback?: () => void) { + return function(dispatch: Dispatch) { + dispatch(modifyConfigPending(config)); + return apiClient + .put(config._links.update.href, config, CONTENT_TYPE_CONFIG) + .then(() => { + dispatch(modifyConfigSuccess(config)); + if (callback) { + callback(); + } + }) + .catch(cause => { + dispatch( + modifyConfigFailure( + config, + new Error(`could not modify config: ${cause.message}`) + ) + ); + }); + }; +} + +export function modifyConfigPending(config: any): Action { + return { + type: MODIFY_CONFIG_PENDING, + payload: config + }; +} + +export function modifyConfigSuccess(config: any): Action { + return { + type: MODIFY_CONFIG_SUCCESS, + payload: config + }; +} + +export function modifyConfigFailure(config: any, error: Error): Action { + return { + type: MODIFY_CONFIG_FAILURE, + payload: { + error, + config + } + }; +} + //reducer function reducer(state: any = {}, action: any = {}) { diff --git a/scm-ui/src/config/modules/config.test.js b/scm-ui/src/config/modules/config.test.js index 837e7d126f..b62ed50bf8 100644 --- a/scm-ui/src/config/modules/config.test.js +++ b/scm-ui/src/config/modules/config.test.js @@ -8,10 +8,14 @@ import reducer, { FETCH_CONFIG_PENDING, FETCH_CONFIG_SUCCESS, FETCH_CONFIG_FAILURE, + MODIFY_CONFIG_PENDING, + MODIFY_CONFIG_SUCCESS, + MODIFY_CONFIG_FAILURE, fetchConfig, fetchConfigSuccess, getFetchConfigFailure, - isFetchConfigPending + isFetchConfigPending, + modifyConfig } from "./config"; const CONFIG_URL = "/scm/api/rest/v2/config"; @@ -94,6 +98,55 @@ describe("config fetch()", () => { expect(actions[1].payload).toBeDefined(); }); }); + + it("should successfully modify config", () => { + fetchMock.putOnce("http://localhost:8081/scm/api/rest/v2/config", { + status: 204 + }); + + const store = mockStore({}); + + return store.dispatch(modifyConfig(config)).then(() => { + const actions = store.getActions(); + expect(actions[0].type).toEqual(MODIFY_CONFIG_PENDING); + expect(actions[1].type).toEqual(MODIFY_CONFIG_SUCCESS); + expect(actions[1].payload).toEqual(config); + }); + }); + + it("should call the callback after modifying config", () => { + fetchMock.putOnce("http://localhost:8081/scm/api/rest/v2/config", { + status: 204 + }); + + let called = false; + const callback = () => { + called = true; + }; + const store = mockStore({}); + + return store.dispatch(modifyConfig(config, callback)).then(() => { + const actions = store.getActions(); + expect(actions[0].type).toEqual(MODIFY_CONFIG_PENDING); + expect(actions[1].type).toEqual(MODIFY_CONFIG_SUCCESS); + expect(called).toBe(true); + }); + }); + + it("should fail modifying config on HTTP 500", () => { + fetchMock.putOnce("http://localhost:8081/scm/api/rest/v2/config", { + status: 500 + }); + + const store = mockStore({}); + + return store.dispatch(modifyConfig(config)).then(() => { + const actions = store.getActions(); + expect(actions[0].type).toEqual(MODIFY_CONFIG_PENDING); + expect(actions[1].type).toEqual(MODIFY_CONFIG_FAILURE); + expect(actions[1].payload).toBeDefined(); + }); + }); }); describe("config reducer", () => {