Build entity model in Api.getEntity
authorFrank Bessou <frank.bessou@logilab.fr>
Mon, 03 Apr 2017 10:32:37 +0200
changeset 63 4ab0deb4a9f4
parent 62 7708e602ffde
child 64 3e2e3705bd25
Build entity model in Api.getEntity
src/Api.js
src/components/Entity.js
--- a/src/Api.js	Mon Apr 03 13:42:11 2017 +0200
+++ b/src/Api.js	Mon Apr 03 10:32:37 2017 +0200
@@ -3,6 +3,7 @@
 import 'whatwg-fetch';
 import {isEmpty} from 'lodash/lang';
 import {get, defaultsDeep} from 'lodash/object';
+import {wrapEntityData} from './jsonaryutils';
 
 export class Api {
 
@@ -104,7 +105,19 @@
 
     getEntity(etype, eid) {
         const url = `/${etype}/${eid}`;
-        return this.jsonFetch(url);
+        const schemaPromise = this.getSchema(etype, eid);
+        const fetchPromise = this.jsonFetchResponse(url);
+        const dataPromise = fetchPromise.then( response => response.json() );
+        const entityPromise = Promise.all([schemaPromise, dataPromise]).then(([schema, data]) => {
+            const wrappedData = wrapEntityData(data, schema);
+            return {
+                url: url,
+                etype: etype,
+                eid: eid,
+                data: wrappedData,
+            };
+        });
+        return entityPromise;
     }
 
     createEntity(etype, attributes, ...files) {
--- a/src/components/Entity.js	Mon Apr 03 13:42:11 2017 +0200
+++ b/src/components/Entity.js	Mon Apr 03 10:32:37 2017 +0200
@@ -186,12 +186,10 @@
     }
 
     componentDidMount() {
-        Promise.all([Api.getSchema(this.etype, this.eid),
-                     Api.getEntity(this.etype, this.eid)])
+        Api.getEntity(this.etype, this.eid)
             .then(
-                ([schema, entity]) => {
-                    const wrappedData = wrapEntityData(entity, schema);
-                    this.setState({schema: schema, data: wrappedData});
+                (entity) => {
+                    this.setState({schema: entity.data.schemas()[0].data.value(), data: entity.data});
                 }
             );
     }