Replace data by complete entity object in Entity component
authorFrank Bessou <frank.bessou@logilab.fr>
Mon, 03 Apr 2017 10:54:29 +0200
changeset 64 3e2e3705bd25
parent 63 4ab0deb4a9f4
child 65 14ee1973f839
Replace data by complete entity object in Entity component
src/components/Entity.js
src/model.js
--- a/src/components/Entity.js	Mon Apr 03 10:32:37 2017 +0200
+++ b/src/components/Entity.js	Mon Apr 03 10:54:29 2017 +0200
@@ -6,6 +6,7 @@
 
 import Api from '../Api';
 import {wrapEntityData, PropTypeJsonaryWrapper} from '../jsonaryutils'
+import {PropTypesEntityModel} from '../model';
 import {buildFormData} from '../utils';
 import {ActionLink, DropDownButton, OneLineView} from './BaseViews';
 import {WorkflowActions} from './Workflow';
@@ -180,7 +181,7 @@
         super(props);
         this.etype = props.params.etype;
         this.eid = props.params.eid;
-        this.state = {schema: null};
+        this.state = {entity: null};
         this.getEditionSchema = this.getEditionSchema.bind(this);
         this.updateEntity = this.updateEntity.bind(this);
     }
@@ -189,14 +190,14 @@
         Api.getEntity(this.etype, this.eid)
             .then(
                 (entity) => {
-                    this.setState({schema: entity.data.schemas()[0].data.value(), data: entity.data});
+                    this.setState({entity: entity});
                 }
             );
     }
 
     updateEntity(entity) {
-        const wrappedData = wrapEntityData(entity, this.state.schema);
-        this.setState({data: wrappedData});
+        const wrappedData = wrapEntityData(entity, this.state.entity.data.schemas()[0].data.value());
+        this.setState({entity: {data: wrappedData}});
     }
 
     getEditionSchema() {
@@ -204,23 +205,21 @@
     }
 
     render() {
-        if (this.state.schema === null) {
+        if (this.state.entity === null) {
             return <div>loading...</div>;
         }
         if (this.props.params.view === 'edit') {
             const redirectPath = `/${this.etype}/${this.eid}`;
             return (
                 <EntityEditForm {...this.props}
-                    etype={this.etype}
-                    eid={this.eid}
-                    data={this.state.data}
+                    entity={this.state.entity}
                     getSchema={this.getEditionSchema}
                     updateEntity={this.updateEntity}
                     redirectPath={ redirectPath }
                 />
             );
         }
-        return <EntityView data={this.state.data} {...this.props.params} />;
+        return <EntityView entity={this.state.entity}/>;
     }
 
 }
@@ -237,12 +236,12 @@
 export class EntityView extends React.Component {
 
     renderTitle() {
-        const {etype, eid} = this.props;
+        const {etype, eid} = this.props.entity;
         return `${etype} #${eid}`;
     }
 
     renderActions() {
-        const hyperLinks = Array.from(this.props.data.links());
+        const hyperLinks = Array.from(this.props.entity.data.links());
         if (isEmpty(hyperLinks)) {
             return null;
         }
@@ -254,16 +253,16 @@
     }
 
     renderAttributes() {
-        return <EntityAttributes data={this.props.data} />;
+        return <EntityAttributes data={this.props.entity.data} />;
     }
 
     renderFooter() {
-        return <EntityMeta data={this.props.data} />;
+        return <EntityMeta data={this.props.entity.data} />;
     }
 
     render() {
-        const {data} = this.props;
-        if (data === null) {
+        const {entity} = this.props;
+        if (entity === null) {
             return <span>loading...</span>;
         }
         return (
@@ -278,9 +277,7 @@
     }
 }
 EntityView.propTypes = {
-    etype: React.PropTypes.string.isRequired,
-    eid: React.PropTypes.string.isRequired,
-    data: PropTypeJsonaryWrapper.isRequired,
+    entity: PropTypesEntityModel.isRequired,
 };
 
 export class WorkflowableEntityView extends EntityView {
@@ -433,7 +430,7 @@
         this.props.getSchema()
             .then(
                 (schema) => {
-                    const {data} = this.props;
+                    const {data} = this.props.entity;
                     const wrappedEntity = wrapEntityData(data.value(), schema);
                     const formData = buildFormData(wrappedEntity);
                     this.setState({schema: schema, formData: formData});
@@ -442,7 +439,7 @@
     }
 
     onSubmit({formData}) {
-        const {etype, eid} = this.props;
+        const {etype, eid} = this.props.entity;
         Api.updateEntity(etype, eid, formData)
             .then(entity => {
                 this.props.updateEntity(entity);
@@ -453,9 +450,7 @@
 }
 
 EntityEditForm.propTypes = {
-    etype: React.PropTypes.string.isRequired,
-    eid: React.PropTypes.number.isRequired,
-    data: PropTypeJsonaryWrapper.isRequired,
+    entity: PropTypesEntityModel.isRequired,
     redirectPath: React.PropTypes.string.isRequired,
     getSchema: React.PropTypes.func.isRequired,
     updateEntity: React.PropTypes.func.isRequired,
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/model.js	Mon Apr 03 10:54:29 2017 +0200
@@ -0,0 +1,9 @@
+import React from 'react';
+import {PropTypeJsonaryWrapper} from './jsonaryutils';
+
+export const PropTypesEntityModel = React.PropTypes.shape({
+    url: React.PropTypes.string.isRequired,
+    etype: React.PropTypes.string.isRequired,
+    eid: React.PropTypes.string.isRequired,
+    data: PropTypeJsonaryWrapper.isRequired,
+});