2022-08-05 14:26:29 +00:00
const Dialogue = require ( './dialogue' )
const Pages = require ( '../paginator' )
2019-09-15 01:53:38 +00:00
const AdminStaffList = {
2022-08-05 14:26:29 +00:00
/ *
2019-09-15 01:53:38 +00:00
oninit : function ( vnode ) {
this . error = ''
this . lastpage = m . route . param ( 'page' ) || '1'
this . staff = [ ]
this . removeStaff = null
this . fetchStaffs ( vnode )
} ,
fetchStaffs : function ( vnode ) {
this . loading = true
2019-10-01 17:18:20 +00:00
document . title = 'Staff members - Admin NFP Moe'
2019-10-01 11:35:00 +00:00
return Staff . getAllStaff ( )
2019-09-15 01:53:38 +00:00
. then ( function ( result ) {
vnode . state . staff = result
} )
. catch ( function ( err ) {
vnode . state . error = err . message
} )
. then ( function ( ) {
vnode . state . loading = false
m . redraw ( )
} )
} ,
confirmRemoveStaff : function ( vnode ) {
let removingStaff = this . removeStaff
this . removeStaff = null
this . loading = true
2019-10-01 11:35:00 +00:00
Staff . removeStaff ( removingStaff . id )
2019-09-15 01:53:38 +00:00
. then ( this . oninit . bind ( this , vnode ) )
. catch ( function ( err ) {
vnode . state . error = err . message
vnode . state . loading = false
m . redraw ( )
} )
} ,
getLevel : function ( level ) {
if ( level === 100 ) {
return 'Admin'
}
return 'Manager'
} ,
view : function ( vnode ) {
return [
m ( 'div.admin-wrapper' , [
m ( 'div.admin-actions' , [
m ( 'span' , 'Actions:' ) ,
m ( m . route . Link , { href : '/admin/staff/add' } , 'Create new staff' ) ,
] ) ,
m ( 'article.editarticle' , [
m ( 'header' , m ( 'h1' , 'All staff' ) ) ,
m ( 'div.error' , {
hidden : ! this . error ,
onclick : function ( ) { vnode . state . error = '' } ,
} , this . error ) ,
( this . loading
? m ( 'div.loading-spinner.full' )
: m ( 'table' , [
m ( 'thead' ,
m ( 'tr' , [
m ( 'th' , 'Fullname' ) ,
m ( 'th' , 'Email' ) ,
m ( 'th' , 'Level' ) ,
m ( 'th.right' , 'Updated' ) ,
m ( 'th.right' , 'Actions' ) ,
] )
) ,
m ( 'tbody' , this . staff . map ( function ( item ) {
return m ( 'tr' , [
2022-07-20 00:33:06 +00:00
m ( 'td' , m ( m . route . Link , { href : '/admin/staff/' + item . id } , item . name ) ) ,
2019-09-15 01:53:38 +00:00
m ( 'td' , item . email ) ,
2022-07-20 00:33:06 +00:00
m ( 'td.right' , AdminStaffList . getLevel ( item . rank ) ) ,
2019-09-15 01:53:38 +00:00
m ( 'td.right' , ( item . updated _at || '---' ) . replace ( 'T' , ' ' ) . split ( '.' ) [ 0 ] ) ,
m ( 'td.right' , m ( 'button' , { onclick : function ( ) { vnode . state . removeStaff = item } } , 'Remove' ) ) ,
] )
} ) ) ,
] )
) ,
m ( Pages , {
base : '/admin/staff' ,
links : this . links ,
} ) ,
] ) ,
] ) ,
m ( Dialogue , {
hidden : vnode . state . removeStaff === null ,
title : 'Delete ' + ( vnode . state . removeStaff ? vnode . state . removeStaff . name : '' ) ,
2022-07-20 00:33:06 +00:00
message : 'Are you sure you want to remove "' + ( vnode . state . removeStaff ? vnode . state . removeStaff . name : '' ) + '" (' + ( vnode . state . removeStaff ? vnode . state . removeStaff . email : '' ) + ')' ,
2019-09-15 01:53:38 +00:00
yes : 'Remove' ,
yesclass : 'alert' ,
no : 'Cancel' ,
noclass : 'cancel' ,
onyes : this . confirmRemoveStaff . bind ( this , vnode ) ,
onno : function ( ) { vnode . state . removeStaff = null } ,
} ) ,
]
2022-08-05 14:26:29 +00:00
} , * /
2019-09-15 01:53:38 +00:00
}
module . exports = AdminStaffList