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

Commit

Permalink
Exchanging fields with fieldsByName #2
Browse files Browse the repository at this point in the history
  • Loading branch information
damianprzygodzki committed May 25, 2017
1 parent 5902b6f commit 4ffcebb
Show file tree
Hide file tree
Showing 13 changed files with 74 additions and 118 deletions.
34 changes: 19 additions & 15 deletions src/actions/WindowActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ export function getTab(tabId, windowType, docId) {
if(res.data){
let tab = {};
res.data.map(row => {
row.fields = parseToDisplay(row.fields);
row.fieldsByName = parseToDisplay(row.fieldsByName);
tab[row.rowId] = row;
});
return tab;
Expand Down Expand Up @@ -427,10 +427,10 @@ export function patch(
function updateData(doc, scope){
return dispatch => {
Object.keys(doc).map(key => {
if(key === 'fields'){
doc.fields.map(field => {
if(key === 'fieldsByName'){
Object.keys(doc.fieldsByName).map((fieldName) => {
dispatch(updateDataFieldProperty(
field.field, field, scope
fieldName, doc.fieldsByName[fieldName], scope
))
})
}else if(key === 'includedTabsInfo'){
Expand Down Expand Up @@ -759,20 +759,24 @@ export function parseToDisplay(obj) {

function parseDateToReadable(obj) {
const dateParse = ['Date', 'DateTime', 'Time'];
return Object.keys(obj).map(key =>
(dateParse.indexOf(obj[key].widgetType) > -1 && obj[key].value) ?
Object.assign({}, obj[key], {
value: obj[key].value ? new Date(obj[key].value) : ''
}) : obj[key]
)

return Object.keys(obj).reduce((acc, key) => {
acc[key] =
(dateParse.indexOf(obj[key].widgetType) > -1 && obj[key].value) ?
Object.assign({}, obj[key], {
value: obj[key].value ? new Date(obj[key].value) : ''
}) : obj[key];
return acc;
}, {});
}

function nullToEmptyStrings(obj) {
return Object.keys(obj).map(key =>
(obj[key].value === null) ?
Object.assign({}, obj[key], { value: '' }) :
obj[key]
)
return Object.keys(obj).reduce((acc, key) => {
acc[key] = (obj[key].value === null) ?
Object.assign({}, obj[key], { value: '' }) :
obj[key];
return acc;
}, {})
}

export function findRowByPropName(arr, name) {
Expand Down
8 changes: 1 addition & 7 deletions src/components/Process.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@ import {connect} from 'react-redux';

import MasterWidget from './widget/MasterWidget';

import {
findRowByPropName
} from '../actions/WindowActions';

export class Process extends Component {
constructor(props){
super(props);
Expand All @@ -17,9 +13,7 @@ export class Process extends Component {
const {disabled} = this.props;
const elements = layout.elements;
return elements.map((elem, id) => {
const widgetData = elem.fields.map(item =>
findRowByPropName(data, item.field)
);
const widgetData = elem.fields.map(item => data[item.field]);
return (
<MasterWidget
entity="process"
Expand Down
12 changes: 3 additions & 9 deletions src/components/Window.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@ import React, { Component } from 'react';

import Dropzone from './Dropzone';

import {
findRowByPropName
} from '../actions/WindowActions';

import MasterWidget from '../components/widget/MasterWidget';
import Tabs from '../components/tabs/Tabs';
import Table from '../components/table/Table';
Expand Down Expand Up @@ -44,7 +40,7 @@ class Window extends Component {
const {type} = this.props.layout;
const {data, rowData, newRow, tabsInfo} = this.props;
const {fullScreen} = this.state;
const dataId = findRowByPropName(data, 'ID').value;
const dataId = data.ID.value;

return(
<Tabs
Expand Down Expand Up @@ -160,10 +156,8 @@ class Window extends Component {

return elements.map((elem, id)=> {
const autoFocus = isFocused && (id === 0);
const widgetData = elem.fields.map(item =>
findRowByPropName(data, item.field)
);
const relativeDocId = findRowByPropName(data, 'ID').value;
const widgetData = elem.fields.map(item => data[item.field]);
const relativeDocId = data.ID.value;
return (
<MasterWidget
entity="window"
Expand Down
1 change: 0 additions & 1 deletion src/components/app/DocumentList.js
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,6 @@ class DocumentList extends Component {
return dispatch(browseViewRequest(
id, page, this.pageLength, sortingQuery, windowType
)).then(response => {

this.mounted && this.setState(Object.assign({}, {
data: response.data,
filters: response.data.filters
Expand Down
8 changes: 1 addition & 7 deletions src/components/app/OverlayField.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@ import React, { Component } from 'react';
import MasterWidget from '../widget/MasterWidget';
import RawWidget from '../widget/RawWidget';

import {
findRowByPropName
} from '../../actions/WindowActions';

class OverlayField extends Component {
constructor(props) {
super(props);
Expand All @@ -28,9 +24,7 @@ class OverlayField extends Component {
const {disabled} = this.props;
const elements = layout.elements;
return elements.map((elem, id) => {
const widgetData = elem.fields.map(item =>
findRowByPropName(data, item.field)
);
const widgetData = elem.fields.map(item => data, item.field);
return (
<MasterWidget
entity="process"
Expand Down
11 changes: 3 additions & 8 deletions src/components/app/SelectionAttributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@ import {
getData
} from '../../actions/GenericActions';

import {
findRowByPropName
} from '../../actions/WindowActions';

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

class SelectionAttributes extends Component {
Expand Down Expand Up @@ -105,7 +101,7 @@ class SelectionAttributes extends Component {
windowType={windowType}
viewId={viewId}
widgetData={item.fields.map(elem =>
findRowByPropName(DLWrapperData, elem.field)
DLWrapperData[elem.field]
)}
gridAlign={item.gridAlign}
key={id}
Expand All @@ -116,9 +112,8 @@ class SelectionAttributes extends Component {
handlePatch={DLWrapperHandlePatch}
handleChange={DLWrapperHandleChange}
tabIndex={this.getTabId(
item.fields.map(elem =>
findRowByPropName(
DLWrapperData, elem.field)
item.fields.map(elem =>
DLWrapperData[elem.field]
)
)}
/>
Expand Down
2 changes: 1 addition & 1 deletion src/components/table/Table.js
Original file line number Diff line number Diff line change
Expand Up @@ -675,7 +675,7 @@ class Table extends Component {
odd={i & 1}
item={item[key]}
entity={entity}
fields={item[key].fields}
fields={item[key].fieldsByName}
rowId={item[key][keyProp]}
tabId={tabid}
cols={cols}
Expand Down
9 changes: 2 additions & 7 deletions src/components/table/TableItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@ import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import TableCell from './TableCell';

import {
findRowByPropName
} from '../../actions/WindowActions';

class TableItem extends Component {
constructor(props) {
super(props);
Expand Down Expand Up @@ -113,9 +109,8 @@ class TableItem extends Component {
// Iterate over layout settings
return cols && cols.map((item, index) => {
const property = item.fields[0].field;
const widgetData = item.fields.map(property =>
findRowByPropName(cells, property.field)
);
const widgetData =
item.fields.map(prop => cells[prop.field]);
const {supportZoomInto} = item.fields[0];

return (
Expand Down
65 changes: 27 additions & 38 deletions src/components/table/TableQuickInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { connect } from 'react-redux';
import RawWidget from '../widget/RawWidget';

import {
findRowByPropName,
parseToDisplay,
addNewRow
} from '../../actions/WindowActions';
Expand Down Expand Up @@ -44,9 +43,8 @@ class TableQuickInput extends Component {
const {data, layout, editedField} = this.state;
if(data && layout){
for(let i = 0; i < layout.length; i++){
const item = layout[i].fields.map(elem =>
findRowByPropName(data, elem.field)
);
const item = layout[i].fields.map(elem => data[elem.field]);

if(!item[0].value){
if(editedField !== i){
this.setState({
Expand All @@ -71,7 +69,7 @@ class TableQuickInput extends Component {
createInstance('window', docType, docId, tabId, 'quickInput')
).then(instance => {
this.setState({
data: parseToDisplay(instance.data.fields),
data: parseToDisplay(instance.data.fieldsByName),
id: instance.data.id,
editedField: 0
});
Expand All @@ -95,17 +93,11 @@ class TableQuickInput extends Component {
}

handleChange = (field, value) => {
const {data} = this.state;

this.setState(Object.assign({}, this.state, {
data: data.map(item => {
if(field === 'all' || item.field === field){
return Object.assign({}, item, {
value: value
})
}else{
return item;
}
this.setState(prevState => ({
data: Object.assign({}, prevState.data, {
[field]: Object.assign({}, prevState.data[field], {
value
})
})
}))
}
Expand All @@ -119,24 +111,22 @@ class TableQuickInput extends Component {
'window', docType, docId, tabId, null, prop, value,
'quickInput', id
)).then(response => {
response.data[0] && response.data[0].fields &&
response.data[0].fields.map(item => {
this.setState({
data: this.state.data.map(field => {
if (field.field !== item.field){
return field;
}

if(callback){
callback();
}

resolve();
return {
...field,
...item
};
const fields = response.data[0] && response.data[0].fieldsByName

fields && Object.keys(fields).map(fieldName => {

this.setState(prevState => ({
data: Object.assign({}, prevState.data, {
[fieldName]: Object.assign({},
prevState.data[fieldName],
fields[fieldName]
)
})
}), () => {
if(callback){
callback();
}
resolve();
});
})
})
Expand All @@ -146,11 +136,9 @@ class TableQuickInput extends Component {
renderFields = (layout, data, dataId, attributeType, quickInputId) => {
const {tabId, docType} = this.props;

if(layout){
if(data && layout){
return layout.map((item, id) => {
const widgetData = item.fields.map(elem =>
findRowByPropName(data, elem.field)
);
const widgetData = item.fields.map(elem => data[elem.field]);
return (<RawWidget
entity={attributeType}
subentity="quickInput"
Expand Down Expand Up @@ -203,7 +191,8 @@ class TableQuickInput extends Component {
}

validateForm = (data) => {
return !data.filter(item => item.mandatory && !item.value).length;
return !Object.keys(data).filter(key =>
data[key].mandatory && !data[key].value).length;
}

render() {
Expand Down
5 changes: 2 additions & 3 deletions src/components/widget/Attributes/Attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
} from '../../../actions/AppActions';

import {
findRowByPropName,
parseToDisplay
} from '../../../actions/WindowActions';

Expand Down Expand Up @@ -120,7 +119,7 @@ class Attributes extends Component {
handleCompletion = () => {
const {attributeType, dispatch, patch} = this.props;
const {data} = this.state;
const attrId = findRowByPropName(data, 'ID').value;
const attrId = data.ID.value;

const mandatory = data.filter(field => field.mandatory);
const valid = !mandatory.filter(field => !field.value).length;
Expand Down Expand Up @@ -160,7 +159,7 @@ class Attributes extends Component {
const {value} = widgetData;
const tmpId = Object.keys(value)[0];
const label = value[tmpId];
const attrId = findRowByPropName(data, 'ID').value;
const attrId = data.ID.value;

return (
<div
Expand Down
8 changes: 1 addition & 7 deletions src/components/widget/Attributes/AttributesDropdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ import onClickOutside from 'react-onclickoutside';

import RawWidget from '../RawWidget';

import {
findRowByPropName
} from '../../../actions/WindowActions'

class AttributesDropdown extends Component {
constructor(props) {
super(props);
Expand Down Expand Up @@ -83,9 +79,7 @@ class AttributesDropdown extends Component {

if(layout){
return layout.map((item, id) => {
const widgetData = item.fields.map(elem =>
findRowByPropName(data, elem.field)
);
const widgetData = item.fields.map(elem => data[elem.field]);
return (<RawWidget
entity={attributeType}
widgetType={item.widgetType}
Expand Down
Loading

0 comments on commit 4ffcebb

Please sign in to comment.