HyperMediaClient: merge createResource and updateResource in submitResource
authorPhilippe Pepiot <philippe.pepiot@logilab.fr>
Tue, 04 Jul 2017 11:58:08 +0200
changeset 247 307dc63312aa
parent 246 0bc5f2d1871a
child 248 3d18fb4a7444
HyperMediaClient: merge createResource and updateResource in submitResource Only HTTP method change here.
src/components/Resource.js
src/services/hypermedia.js
--- a/src/components/Resource.js	Mon Jul 03 12:02:06 2017 +0200
+++ b/src/components/Resource.js	Tue Jul 04 11:58:08 2017 +0200
@@ -86,7 +86,7 @@
 
     onSubmit({formData}) {
         const path = this.props.resource.url;
-        hypermediaClient.createResource(path, formData)
+        hypermediaClient.submitResource('POST', path, formData)
             .then(data => {
                 if (data.hasOwnProperty('errors')) {
                     // XXX better update formData to `.addError` inline...
@@ -224,7 +224,7 @@
 
     onSubmit({formData}) {
         const url = this.props.resource.url;
-        hypermediaClient.updateResource(url, formData)
+        hypermediaClient.submitResource('PUT', url, formData)
             .then(data => {
                 if (data.hasOwnProperty('errors')) {
                     // XXX better update formData to `.addError` inline...
--- a/src/services/hypermedia.js	Mon Jul 03 12:02:06 2017 +0200
+++ b/src/services/hypermedia.js	Tue Jul 04 11:58:08 2017 +0200
@@ -118,20 +118,9 @@
             });
     }
 
-    createResource(url, data) {
+    submitResource(method, url, data) {
         const options = {
-            method: 'POST',
-            headers: {
-                'Content-Type': 'application/json',
-            },
-            body: JSON.stringify(data),
-        };
-        return this.jsonFetch(url, options);
-    }
-
-    updateResource(url, data) {
-        const options = {
-            method: 'PUT',
+            method: method,
             headers: {
                 'Content-Type': 'application/json',
             },