From abe0a5a0a68526f6a9ac915caed51c9ba4c1a93b Mon Sep 17 00:00:00 2001 From: Teo Sarca Date: Sun, 3 Dec 2017 12:44:44 +0200 Subject: [PATCH] refactor createPatchRequestPayload https://github.com/metasfresh/metasfresh-webui-frontend/issues/1357 --- src/actions/GenericActions.js | 46 ++++++++++++++-------------- src/actions/ViewAttributesActions.js | 26 +++------------- 2 files changed, 27 insertions(+), 45 deletions(-) diff --git a/src/actions/GenericActions.js b/src/actions/GenericActions.js index 30cd2a0e0..4b7140cc7 100644 --- a/src/actions/GenericActions.js +++ b/src/actions/GenericActions.js @@ -49,6 +49,28 @@ export function createInstance(entity, docType, docId, tabId, subentity) { ); } +export function createPatchRequestPayload(property, value) { + if (Array.isArray(property) && Array.isArray(value)) { + return property.map((item, index) => ({ + op: 'replace', + path: item, + value: value[index] + })); + } else if (Array.isArray(property) && value !== undefined) { + return property.map(item => ({ + op: 'replace', + path: item.field, + value + })); + } else if (property && value !== undefined) { + return [{ + op: 'replace', + path: property, + value + }]; + } +} + export function patchRequest({ // HOTFIX: before refactoring all calls explicity set docId to `null` // instead of `undefined` so default value 'NEW' was never used! @@ -66,29 +88,7 @@ export function patchRequest({ viewId, isEdit }) { - let payload = []; - - if (docId !== 'NEW') { - if (Array.isArray(property) && Array.isArray(value)) { - payload = property.map((item, index) => ({ - op: 'replace', - path: item, - value: value[index] - })); - } else if (Array.isArray(property) && value !== undefined) { - payload = property.map(item => ({ - op: 'replace', - path: item.field, - value - })); - } else if (property && value !== undefined) { - payload = [{ - op: 'replace', - path: property, - value - }]; - } - } + let payload = docId !== 'NEW' ? createPatchRequestPayload(property, value) : []; return axios.patch( config.API_URL + diff --git a/src/actions/ViewAttributesActions.js b/src/actions/ViewAttributesActions.js index 50edd591f..e38a85690 100644 --- a/src/actions/ViewAttributesActions.js +++ b/src/actions/ViewAttributesActions.js @@ -1,4 +1,7 @@ import axios from 'axios'; +import { + createPatchRequestPayload +} from './GenericActions'; // // Handles view attributes (the panel which is displayed on the right side of a view, if view supports attributes) @@ -34,9 +37,7 @@ export function getViewAttributeDropdown(windowId, viewId, rowId, attribute) { * @param {*} viewId * @param {*} rowId */ -export function getViewAttributes ( - windowId, viewId, rowId -) { +export function getViewAttributes (windowId, viewId, rowId) { return axios.get( config.API_URL + '/documentView'+ @@ -47,25 +48,6 @@ export function getViewAttributes ( ); } -function createPatchRequestPayload(property, value) { - let payload = []; - if (Array.isArray(property) && value !== undefined) { - payload = property.map(item => ({ - op: 'replace', - path: item.field, - value - })); - } else if (property && value !== undefined) { - payload = [{ - op: 'replace', - path: property, - value - }]; - } - - return payload; -} - export function patchViewAttributes( windowId, viewId,