nfp_sites/app/admin/editstaff.js

153 lines
4.1 KiB
JavaScript
Raw Normal View History

2019-10-01 11:35:00 +00:00
const Staff = require('../api/staff')
const EditStaff = {
oninit: function(vnode) {
this.fetchStaff(vnode)
},
onupdate: function(vnode) {
if (this.lastid !== m.route.param('id')) {
this.fetchStaff(vnode)
}
},
fetchStaff: function(vnode) {
this.lastid = m.route.param('id')
this.loading = this.lastid !== 'add'
this.creating = this.lastid === 'add'
this.error = ''
this.staff = {
2022-07-20 00:33:06 +00:00
name: '',
email: '',
password: '',
2022-07-20 00:33:06 +00:00
rank: 10,
}
if (this.lastid !== 'add') {
2019-10-01 11:35:00 +00:00
Staff.getStaff(this.lastid)
.then(function(result) {
vnode.state.editedPath = true
vnode.state.staff = result
2022-07-20 00:33:06 +00:00
document.title = 'Editing: ' + result.name + ' - Admin NFP Moe'
})
.catch(function(err) {
vnode.state.error = err.message
})
.then(function() {
vnode.state.loading = false
m.redraw()
})
} else {
document.title = 'Creating Staff Member - Admin NFP Moe'
}
},
2022-07-20 00:33:06 +00:00
updateValue: function(key, e) {
this.staff[key] = e.currentTarget.value
},
save: function(vnode, e) {
e.preventDefault()
2022-07-20 00:33:06 +00:00
if (!this.staff.name) {
this.error = 'Fullname is missing'
} else if (!this.staff.email) {
this.error = 'Email is missing'
} else {
this.error = ''
}
if (this.error) return
this.staff.description = vnode.state.froala && vnode.state.froala.html.get() || this.staff.description
this.loading = true
let promise
if (this.staff.id) {
2019-10-01 11:35:00 +00:00
promise = Staff.updateStaff(this.staff.id, {
2022-07-20 00:33:06 +00:00
name: this.staff.name,
email: this.staff.email,
2022-07-20 00:33:06 +00:00
rank: this.staff.rank,
password: this.staff.password,
})
} else {
2019-10-01 11:35:00 +00:00
promise = Staff.createStaff({
2022-07-20 00:33:06 +00:00
name: this.staff.name,
email: this.staff.email,
2022-07-20 00:33:06 +00:00
rank: this.staff.rank,
password: this.staff.password,
})
}
promise.then(function(res) {
m.route.set('/admin/staff')
})
.catch(function(err) {
vnode.state.error = err.message
})
.then(function() {
vnode.state.loading = false
m.redraw()
})
},
updateLevel: function(e) {
2022-07-20 00:33:06 +00:00
this.staff.rank = Number(e.currentTarget.value)
},
view: function(vnode) {
2022-07-20 00:33:06 +00:00
const ranks = [[10, 'Manager'], [100, 'Admin']]
return (
this.loading ?
m('div.loading-spinner')
: m('div.admin-wrapper', [
m('div.admin-actions', this.staff.id
? [
m('span', 'Actions:'),
m(m.route.Link, { href: '/admin/staff' }, 'Staff list'),
]
: null),
m('article.editstaff', [
2022-07-20 00:33:06 +00:00
m('header', m('h1', this.creating ? 'Create Staff' : 'Edit ' + (this.staff.name || '(untitled)'))),
m('div.error', {
hidden: !this.error,
onclick: function() { vnode.state.error = '' },
}, this.error),
m('form.editstaff.content', {
onsubmit: this.save.bind(this, vnode),
}, [
m('label', 'Level'),
m('select', {
onchange: this.updateLevel.bind(this),
2022-07-20 00:33:06 +00:00
}, ranks.map(function(rank) { return m('option', { value: rank[0], selected: rank[0] === vnode.state.staff.rank }, rank[1]) })),
m('label', 'Fullname'),
m('input', {
type: 'text',
2022-07-20 00:33:06 +00:00
value: this.staff.name,
oninput: this.updateValue.bind(this, 'name'),
}),
m('label', 'Email'),
m('input', {
type: 'text',
value: this.staff.email,
oninput: this.updateValue.bind(this, 'email'),
}),
m('label', 'Password (optional)'),
m('input', {
type: 'text',
value: this.staff.password,
oninput: this.updateValue.bind(this, 'password'),
}),
m('input', {
type: 'submit',
value: 'Save',
}),
]),
]),
])
)
},
}
module.exports = EditStaff