Skip to content
Snippets Groups Projects
Commit c3c2b922 authored by Hermann's avatar Hermann
Browse files

update editview to handler syntax

parent 9946c7b9
No related branches found
No related tags found
No related merge requests found
...@@ -186,7 +186,9 @@ export class ResourceHandler { ...@@ -186,7 +186,9 @@ export class ResourceHandler {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
getSession().then((api) => { getSession().then((api) => {
api.post(this.resource, item).then((response) => { api.post(this.resource, item).then((response) => {
if (response.status >= 400) { if (response.status === 422) {
reject(response.data);
} else if (response.status >= 400) {
resetSession(); resetSession();
reject(); reject();
} else { } else {
...@@ -212,7 +214,9 @@ export class ResourceHandler { ...@@ -212,7 +214,9 @@ export class ResourceHandler {
headers: { 'If-Match': item._etag }, headers: { 'If-Match': item._etag },
data: submitData, data: submitData,
}).then((response) => { }).then((response) => {
if (response.status >= 400) { if (response.status === 422) {
reject(response.data);
} else if (response.status >= 400) {
resetSession(); resetSession();
reject(); reject();
} else { } else {
......
...@@ -46,10 +46,9 @@ export default class EditView extends ItemView { ...@@ -46,10 +46,9 @@ export default class EditView extends ItemView {
oninit() { oninit() {
if (this.id) { if (this.id) {
// load data for item // load data for item
getSession().then((apiSession) => { this.handler.getItem(this.id, this.embedded).then((item) => {
this.loadItemData(apiSession); this.data = item;
}).catch(() => { m.redraw();
m.route.set('/login');
}); });
} }
// load schema // load schema
...@@ -102,45 +101,30 @@ export default class EditView extends ItemView { ...@@ -102,45 +101,30 @@ export default class EditView extends ItemView {
submit(method, fields) { submit(method, fields) {
return () => { return () => {
if (this.changed) { if (this.changed) {
getSession().then((apiSession) => { let request;
// build request if (method === 'POST') {
const request = { method }; request = this.handler.post(this.data);
if (method === 'POST' || method === 'PATCH') { } else if (method === 'PATCH') {
// fields like `_id` are not post/patchable request = this.handler.patch(this.data);
// We therefore only send patchable fields }
const submitData = {}; request.then((response) => {
fields.forEach((key) => { this.callback(response);
submitData[key] = this.data[key]; }).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; m.redraw();
} } else if (response.status === 403) {
// Unauthorized
// if request is PATCH or DELETE, add If-Match header and set url m.route.set('/login');
if (method === 'PATCH' || method === 'DELETE') {
request.headers = { 'If-Match': this.data._etag };
request.url = `${this.resource}/${this.id}`;
} else { } 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 { } else {
this.callback(); this.callback();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment