Skip to content
This repository has been archived by the owner on Dec 13, 2020. It is now read-only.

Commit

Permalink
refactor createPatchRequestPayload
Browse files Browse the repository at this point in the history
  • Loading branch information
teosarca committed Dec 3, 2017
1 parent 1f4e3d6 commit abe0a5a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 45 deletions.
46 changes: 23 additions & 23 deletions src/actions/GenericActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand All @@ -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 +
Expand Down
26 changes: 4 additions & 22 deletions src/actions/ViewAttributesActions.js
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -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'+
Expand All @@ -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,
Expand Down

0 comments on commit abe0a5a

Please sign in to comment.