diff --git a/src/auth.js b/src/auth.js index 67c2e8de33d136ac65fa4a402bff495b8c1a523f..8222c1c845ecfd9a8270220e2e0f8758847a029f 100644 --- a/src/auth.js +++ b/src/auth.js @@ -186,7 +186,9 @@ export class ResourceHandler { return new Promise((resolve, reject) => { getSession().then((api) => { api.post(this.resource, item).then((response) => { - if (response.status >= 400) { + if (response.status === 422) { + reject(response.data); + } else if (response.status >= 400) { resetSession(); reject(); } else { @@ -212,7 +214,9 @@ export class ResourceHandler { headers: { 'If-Match': item._etag }, data: submitData, }).then((response) => { - if (response.status >= 400) { + if (response.status === 422) { + reject(response.data); + } else if (response.status >= 400) { resetSession(); reject(); } else { diff --git a/src/views/editView.js b/src/views/editView.js index 8f1de7f5d2d2f930c8b826ca2e26fa897ba00c05..86cf3dd9a9c87875bfb2b4d5a202e5bdc14ad123 100644 --- a/src/views/editView.js +++ b/src/views/editView.js @@ -46,10 +46,9 @@ export default class EditView extends ItemView { oninit() { if (this.id) { // load data for item - getSession().then((apiSession) => { - this.loadItemData(apiSession); - }).catch(() => { - m.route.set('/login'); + this.handler.getItem(this.id, this.embedded).then((item) => { + this.data = item; + m.redraw(); }); } // load schema @@ -102,45 +101,30 @@ export default class EditView extends ItemView { submit(method, fields) { return () => { if (this.changed) { - getSession().then((apiSession) => { - // build request - const request = { method }; - if (method === 'POST' || method === 'PATCH') { - // fields like `_id` are not post/patchable - // We therefore only send patchable fields - const submitData = {}; - fields.forEach((key) => { - submitData[key] = this.data[key]; + let request; + if (method === 'POST') { + request = this.handler.post(this.data); + } else if (method === 'PATCH') { + request = this.handler.patch(this.data); + } + request.then((response) => { + this.callback(response); + }).catch((error) => { + console.log(error); + // Process the API error + const { response } = error; + if (response.status === 422) { + // there are problems with some fields, display them + Object.keys(response.data._issues).forEach((field) => { + this.errors[field] = [response.data._issues[field]]; }); - request.data = submitData; - } - - // if request is PATCH or DELETE, add If-Match header and set url - if (method === 'PATCH' || method === 'DELETE') { - request.headers = { 'If-Match': this.data._etag }; - request.url = `${this.resource}/${this.id}`; + m.redraw(); + } else if (response.status === 403) { + // Unauthorized + m.route.set('/login'); } else { - request.url = this.resource; + console.log(error); } - - apiSession(request).then((response) => { - this.callback(response); - }).catch((error) => { - // Process the API error - const { response } = error; - if (response.status === 422) { - // there are problems with some fields, display them - Object.keys(response.data._issues).forEach((field) => { - this.errors[field] = [response.data._issues[field]]; - }); - m.redraw(); - } else if (response.status === 403) { - // Unauthorized - m.route.set('/login'); - } else { - console.log(error); - } - }); }); } else { this.callback();