Let Api.getSchema take the schema route as argument
authorFrank Bessou <frank.bessou@logilab.fr>
Thu, 13 Apr 2017 10:39:45 +0200
changeset 109 f9fbff23f4a9
parent 108 37f79608e7a3
child 110 432a59a73c7d
Let Api.getSchema take the schema route as argument The Api should not know where is located the schema of a resource.
src/Api.js
src/components/Entity.js
--- a/src/Api.js	Thu Apr 13 12:23:30 2017 +0200
+++ b/src/Api.js	Thu Apr 13 10:39:45 2017 +0200
@@ -44,7 +44,7 @@
         return this.jsonFetchResponse(url, options).then(response => response.json());
     }
 
-    jsonSchemaFetch(url, options = {}) {
+    getSchema(url, options = {}) {
         defaultsDeep(options, {headers: {Accept: 'application/schema+json'}});
         return this.jsonFetch(url, options);
     }
@@ -61,18 +61,10 @@
             });
     }
 
-    getSchema(resourceUrl, role=null) {
-        let url = resourceUrl + '/schema';
-        if (role !== null) {
-            url += `?role=${role}`;
-        }
-        return this.jsonSchemaFetch(url);
-    }
-
     getResource(resourceRoute) {
         const resource = {};
         // Fetch resource schema
-        const schemaPromise = this.getSchema(resourceRoute);
+        const schemaPromise = this.getSchema(appendPath(resourceRoute, '/schema'));
         const resourcePromise = schemaPromise.then(schema => {
             const wrappedSchema = Jsonary.createSchema(schema);
             const selfLink = wrappedSchema.getLink('self');
--- a/src/components/Entity.js	Thu Apr 13 12:23:30 2017 +0200
+++ b/src/components/Entity.js	Thu Apr 13 10:39:45 2017 +0200
@@ -8,7 +8,7 @@
 import Api from '../Api';
 import {wrapEntityData, PropTypeJsonaryWrapper} from '../jsonaryutils'
 import {PropTypesEntityModel} from '../model';
-import {buildFormData} from '../utils';
+import {buildFormData, appendPath} from '../utils';
 import {ActionsDropDown, CollectionItemLink} from './BaseViews';
 import {WorkflowActions} from './Workflow';
 
@@ -211,7 +211,7 @@
     }
 
     getEditionSchema() {
-        return Api.getSchema(this.state.entity.url, 'edition');
+        return Api.getSchema(appendPath(this.state.entity.url,'/schema?role=edition'));
     }
 
     navigateToSelf() {
@@ -394,7 +394,7 @@
 
     componentDidMount() {
         const {etype} = this.props.params;
-        Api.getSchema(`/${etype}`, 'creation')
+        Api.getSchema(appendPath(`/${etype}`, '/schema?role=creation'))
             .then(schema => this.setState({schema: schema}));
     }