2019-10-01 11:35:00 +00:00
|
|
|
const Staff = require('../api/staff')
|
2019-09-15 01:53:38 +00:00
|
|
|
|
|
|
|
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: '',
|
2019-09-15 01:53:38 +00:00
|
|
|
email: '',
|
|
|
|
password: '',
|
2022-07-20 00:33:06 +00:00
|
|
|
rank: 10,
|
2019-09-15 01:53:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (this.lastid !== 'add') {
|
2019-10-01 11:35:00 +00:00
|
|
|
Staff.getStaff(this.lastid)
|
2019-09-15 01:53:38 +00:00
|
|
|
.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'
|
2019-09-15 01:53:38 +00:00
|
|
|
})
|
|
|
|
.catch(function(err) {
|
|
|
|
vnode.state.error = err.message
|
|
|
|
})
|
|
|
|
.then(function() {
|
|
|
|
vnode.state.loading = false
|
|
|
|
m.redraw()
|
|
|
|
})
|
2019-10-01 17:18:20 +00:00
|
|
|
} else {
|
|
|
|
document.title = 'Creating Staff Member - Admin NFP Moe'
|
2019-09-15 01:53:38 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2022-07-20 00:33:06 +00:00
|
|
|
updateValue: function(key, e) {
|
|
|
|
this.staff[key] = e.currentTarget.value
|
2019-09-15 01:53:38 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
save: function(vnode, e) {
|
|
|
|
e.preventDefault()
|
2022-07-20 00:33:06 +00:00
|
|
|
if (!this.staff.name) {
|
2019-09-15 01:53:38 +00:00
|
|
|
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,
|
2019-09-15 01:53:38 +00:00
|
|
|
email: this.staff.email,
|
2022-07-20 00:33:06 +00:00
|
|
|
rank: this.staff.rank,
|
2019-09-15 01:53:38 +00:00
|
|
|
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,
|
2019-09-15 01:53:38 +00:00
|
|
|
email: this.staff.email,
|
2022-07-20 00:33:06 +00:00
|
|
|
rank: this.staff.rank,
|
2019-09-15 01:53:38 +00:00
|
|
|
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)
|
2019-09-15 01:53:38 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
view: function(vnode) {
|
2022-07-20 00:33:06 +00:00
|
|
|
const ranks = [[10, 'Manager'], [100, 'Admin']]
|
2019-09-15 01:53:38 +00:00
|
|
|
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)'))),
|
2019-09-15 01:53:38 +00:00
|
|
|
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]) })),
|
2019-09-15 01:53:38 +00:00
|
|
|
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'),
|
2019-09-15 01:53:38 +00:00
|
|
|
}),
|
|
|
|
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
|