Add HttpHypermediaClient.getSubmissionSchema method
authorFrank Bessou <frank.bessou@logilab.fr>
Thu, 11 May 2017 16:09:33 +0200
changeset 199 ed0d8132c38b
parent 198 5b1b7472669e
child 200 024c7f53a589
Add HttpHypermediaClient.getSubmissionSchema method This method is used by form views to retrieve the edition and creation schema.
src/components/Resource.js
src/services/hypermedia.js
--- a/src/components/Resource.js	Thu May 11 18:05:05 2017 +0200
+++ b/src/components/Resource.js	Thu May 11 16:09:33 2017 +0200
@@ -3,7 +3,7 @@
 import {PropTypes} from 'prop-types';
 import {merge} from 'lodash/object';
 
-import {appendPath, buildFormData} from '../utils';
+import {buildFormData} from '../utils';
 import {EntityAttributes, EntityForm, EntityMeta, RelatedResources} from './Entity';
 import {PropTypesResourceModel} from '../model';
 import {PropTypeJsonaryWrapper, mapToSchema} from '../jsonaryutils';
@@ -76,7 +76,7 @@
     }
 
     componentDidMount() {
-        hypermediaClient.getSchema(appendPath(this.props.resource.url, '/schema?role=creation'))
+        hypermediaClient.getSubmissionSchema(this.props.resource)
             .then(schema => this.setState({schema: schema}));
     }
 
@@ -221,7 +221,7 @@
     }
 
     componentDidMount() {
-        return hypermediaClient.getSchema(appendPath(this.props.resource.url, '/schema?role=edition'))
+        return hypermediaClient.getSubmissionSchema(this.props.resource)
             .then(
                 (schema) => {
                     const {data} = this.props.resource;
--- a/src/services/hypermedia.js	Thu May 11 18:05:05 2017 +0200
+++ b/src/services/hypermedia.js	Thu May 11 16:09:33 2017 +0200
@@ -45,6 +45,16 @@
         return this.jsonFetch(url, options);
     }
 
+    getSubmissionSchema(resource) {
+        const selfLink = resource.data.getLink('self').rawLink;
+        const schema = selfLink.schema;
+        if (schema.hasOwnProperty('$ref')) {
+            return this.getSchema(schema.$ref);
+        } else {
+            return Promise.resolve(schema);
+        }
+    }
+
     extractAllowedActions(response) {
         if (!response.headers.has('Allow')) {
             return ['view'];