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

define more general active/inactive submit button

parent cebdf2f4
No related branches found
No related tags found
No related merge requests found
import { ItemView } from './views/itemView'; import { ItemView } from './views/itemView';
import { EditView, inputGroup, selectGroup } from './views/editView'; import { EditView, inputGroup, selectGroup, submitButton } from './views/editView';
import TableView from './views/tableView'; import TableView from './views/tableView';
const m = require('mithril'); const m = require('mithril');
...@@ -105,18 +105,17 @@ class UserEdit extends EditView { ...@@ -105,18 +105,17 @@ class UserEdit extends EditView {
// do not render anything if there is no data yet // do not render anything if there is no data yet
if (!this.data) return m.trust(''); if (!this.data) return m.trust('');
// UPDATE button is inactive if form is not valid
const buttonArgs = this.patchOnClick([
'lastname', 'firstname', 'email', 'membership', 'gender']);
const updateButton = m(
'div.btn.btn-warning',
this.valid ? buttonArgs : { disabled: 'disabled' },
'Update',
);
return m('form', [ return m('form', [
this.getForm(), this.getForm(),
updateButton, m(submitButton, {
active: this.valid,
args: {
onclick: this.submit('PATCH', [
'lastname', 'firstname', 'email', 'membership', 'gender']),
class: 'btn-warning',
},
text: 'Update',
}),
]); ]);
} }
} }
...@@ -135,18 +134,17 @@ export class NewUser extends UserEdit { ...@@ -135,18 +134,17 @@ export class NewUser extends UserEdit {
} }
view() { view() {
// UPDATE button is inactive if form is not valid
const buttonArgs = this.createOnClick([
'lastname', 'firstname', 'email', 'membership', 'gender']);
const postButton = m(
'div.btn.btn-warning',
this.valid ? buttonArgs : { disabled: 'disabled' },
'Create',
);
return m('form', [ return m('form', [
this.getForm(), this.getForm(),
postButton, m(submitButton, {
active: this.valid,
args: {
onclick: this.submit('POST', [
'lastname', 'firstname', 'email', 'membership', 'gender']),
class: 'btn-warning',
},
text: 'Create',
}),
]); ]);
} }
} }
......
...@@ -99,46 +99,37 @@ export class EditView extends ItemView { ...@@ -99,46 +99,37 @@ export class EditView extends ItemView {
return boundFormelement; return boundFormelement;
} }
patchOnClick(patchableFields) { submit(method, fields) {
return { return () => {
onclick: () => { if (this.changed) {
if (this.changed) { getSession().then((apiSession) => {
getSession().then((apiSession) => { // build request
// fields like `_id` are not patchable and would lead to an error const request = { method };
if (method === 'POST' || method === 'PATCH') {
// fields like `_id` are not post/patchable
// We therefore only send patchable fields // We therefore only send patchable fields
const patchData = {}; const submitData = {};
patchableFields.forEach((key) => { fields.forEach((key) => {
patchData[key] = this.data[key]; submitData[key] = this.data[key];
});
apiSession.patch(`${this.resource}/${this.id}`, patchData, {
headers: { 'If-Match': this.data._etag },
}).then((response) => {
this.callback(response);
}); });
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}`;
} else {
request.url = this.resource;
}
apiSession(request).then((response) => {
this.callback(response);
}); });
} else {
this.callback();
}
},
};
}
createOnClick(fields) {
return {
onclick: () => {
getSession().then((apiSession) => {
// fields like `_id` are not patchable and would lead to an error
// We therefore only send patchable fields
const postData = {};
fields.forEach((key) => {
postData[key] = this.data[key];
});
apiSession.post(this.resource, postData)
.then((response) => { this.callback(response); });
}); });
}, } else {
this.callback();
}
}; };
} }
} }
...@@ -184,3 +175,13 @@ export class selectGroup { ...@@ -184,3 +175,13 @@ export class selectGroup {
]); ]);
} }
} }
export class submitButton {
view(vnode) {
const args = vnode.attrs.args;
if (!vnode.attrs.active) {
args.disabled = 'disabled';
}
return m('div.btn', args, vnode.attrs.text);
}
}
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