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

Commit

Permalink
Merge pull request #1402 from metasfresh/dev-1357
Browse files Browse the repository at this point in the history
Update attributes & dropdown api #1357
  • Loading branch information
teosarca authored Dec 3, 2017
2 parents 8649ed2 + abe0a5a commit ec9f92d
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 72 deletions.
76 changes: 23 additions & 53 deletions src/actions/GenericActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,6 @@ export function initLayout(
);
}

export function getViewAttributesLayout(windowId, viewId, rowId) {
return axios.get(
config.API_URL +
'/documentView'+
'/' + windowId +
'/' + viewId +
'/' + rowId +
'/attributes/layout'
);
}

export function getData(
entity, docType, docId, tabId, rowId, subentity, subentityId, isAdvanced,
orderBy, viewId
Expand All @@ -49,25 +38,6 @@ export function getData(
);
}

/**
*
* @param {*} windowId
* @param {*} viewId
* @param {*} rowId
*/
export function getViewAttributes (
windowId, viewId, rowId
) {
return axios.get(
config.API_URL +
'/documentView'+
'/' + windowId +
'/' + viewId +
'/' + rowId +
'/attributes'
);
}

export function createInstance(entity, docType, docId, tabId, subentity) {
return axios.post(
config.API_URL +
Expand All @@ -79,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 @@ -96,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
67 changes: 67 additions & 0 deletions src/actions/ViewAttributesActions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
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)
// Endpoints: /rest/api/documentView/{windowId}/{viewId}/{rowId}/attributes/**
//

export function getViewAttributesLayout(windowId, viewId, rowId) {
return axios.get(
config.API_URL +
'/documentView'+
'/' + windowId +
'/' + viewId +
'/' + rowId +
'/attributes/layout'
);
}

export function getViewAttributeDropdown(windowId, viewId, rowId, attribute) {
return axios.get(
config.API_URL +
'/documentView'+
'/' + windowId +
'/' + viewId +
'/' + rowId +
'/attributes/attribute/' + attribute +
'/dropdown'
);
}

/**
*
* @param {*} windowId
* @param {*} viewId
* @param {*} rowId
*/
export function getViewAttributes (windowId, viewId, rowId) {
return axios.get(
config.API_URL +
'/documentView'+
'/' + windowId +
'/' + viewId +
'/' + rowId +
'/attributes'
);
}

export function patchViewAttributes(
windowId,
viewId,
rowId,
property,
value
) {
const payload = createPatchRequestPayload(property, value);

return axios.patch(
config.API_URL +
'/documentView'+
'/' + windowId +
'/' + viewId +
'/' + rowId +
'/attributes', payload);
}
25 changes: 12 additions & 13 deletions src/components/DataLayoutWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import React, { Component, cloneElement } from 'react';
import {connect} from 'react-redux';

import {
patchRequest
} from '../actions/GenericActions';
patchViewAttributes
} from '../actions/ViewAttributesActions';

import {
parseToDisplay
Expand Down Expand Up @@ -39,17 +39,16 @@ class DataLayoutWrapper extends Component {
}

handlePatch = (prop, value, cb) => {
const {entity, windowType, viewId} = this.props;
const {dataId} = this.state;

patchRequest({
entity,
docType: windowType,
docId: dataId,
property: prop,
value,
viewId
}).then(response => {
const {windowType : windowId, viewId} = this.props;
const {dataId : rowId} = this.state;

patchViewAttributes(
windowId,
viewId,
rowId,
prop,
value
).then(response => {
const preparedData = parseToDisplay(response.data[0].fieldsByName);
preparedData && Object.keys(preparedData).map(key => {
this.setState(prevState => ({
Expand Down
2 changes: 1 addition & 1 deletion src/components/app/SelectionAttributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import counterpart from 'counterpart';
import {
getViewAttributesLayout,
getViewAttributes
} from '../../actions/GenericActions';
} from '../../actions/ViewAttributesActions';

import RawWidget from '../widget/RawWidget';

Expand Down
22 changes: 17 additions & 5 deletions src/components/widget/List/List.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import {
dropdownRequest
} from '../../../actions/GenericActions';

import {
getViewAttributeDropdown
} from '../../../actions/ViewAttributesActions';

class List extends Component {
constructor(props) {
super(props);
Expand Down Expand Up @@ -56,20 +60,28 @@ class List extends Component {
loading: true
});

dropdownRequest({
const propertyName = (filterWidget ?
properties[0].parameterName : properties[0].field
);

const request = attribute ? getViewAttributeDropdown(
windowType,
viewId,
dataId,
propertyName) : dropdownRequest({
attribute,
docId: dataId,
docType: windowType,
entity,
propertyName: (filterWidget ?
properties[0].parameterName : properties[0].field
),
propertyName,
rowId,
subentity,
subentityId,
tabId,
viewId
}).then(res => {
});

request.then(res => {
let values = res.data.values || [];
let singleOption = values && (values.length === 1);

Expand Down

0 comments on commit ec9f92d

Please sign in to comment.