2022-07-27 08:41:18 +00:00
const Page = require ( '../api/page.p' )
2019-09-13 13:33:10 +00:00
const Dialogue = require ( '../widgets/dialogue' )
2022-07-27 08:41:18 +00:00
const common = require ( '../api/common' )
2019-09-13 13:33:10 +00:00
const AdminPages = {
oninit : function ( vnode ) {
this . error = ''
this . pages = [ ]
this . removePage = null
2019-10-01 17:18:20 +00:00
document . title = 'Pages - Admin NFP Moe'
2022-07-27 08:41:18 +00:00
this . fetchPages ( vnode )
} ,
2019-10-01 17:18:20 +00:00
2022-07-27 08:41:18 +00:00
fetchPages : function ( vnode ) {
this . loading = true
this . error = ''
return common . sendRequest ( {
method : 'GET' ,
url : '/api/auth/pages' ,
2019-09-13 13:33:10 +00:00
} )
2022-07-27 08:41:18 +00:00
. then ( ( result ) => {
this . pages = result . tree
} , ( err ) => {
this . error = err . message
2019-09-13 13:33:10 +00:00
} )
2022-07-27 08:41:18 +00:00
. then ( ( ) => {
this . loading = false
2019-09-13 13:33:10 +00:00
m . redraw ( )
} )
} ,
confirmRemovePage : function ( vnode ) {
let removingPage = this . removePage
this . removePage = null
this . loading = true
2022-07-27 08:41:18 +00:00
m . redraw ( )
return common . sendRequest ( {
method : 'DELETE' ,
url : '/api/auth/pages/' + removingPage . id ,
} )
. then ( ( ) => Page . refreshTree ( ) )
. then (
( ) => this . fetchPages ( vnode ) ,
( err ) => {
this . error = err . message
this . loading = false
2019-09-13 13:33:10 +00:00
m . redraw ( )
2022-07-27 08:41:18 +00:00
}
)
2019-09-13 13:33:10 +00:00
} ,
drawPage : function ( vnode , page ) {
return [
m ( 'tr' , [
m ( 'td' , [
2019-10-02 17:40:32 +00:00
page . parent _id ? m ( 'span.subpage' , ' - ' ) : null ,
2019-09-13 13:33:10 +00:00
m ( m . route . Link , { href : '/admin/pages/' + page . id } , page . name ) ,
] ) ,
m ( 'td' , m ( m . route . Link , { href : '/page/' + page . path } , '/page/' + page . path ) ) ,
m ( 'td.right' , page . updated _at . replace ( 'T' , ' ' ) . split ( '.' ) [ 0 ] ) ,
m ( 'td.right' , m ( 'button' , { onclick : function ( ) { vnode . state . removePage = page } } , 'Remove' ) ) ,
2019-09-14 19:03:38 +00:00
] ) ,
2022-07-27 08:41:18 +00:00
] . concat ( page . children ? page . children . map ( AdminPages . drawPage . bind ( this , vnode ) ) : [ ] )
2019-09-13 13:33:10 +00:00
} ,
view : function ( vnode ) {
return [
( this . loading ?
m ( 'div.loading-spinner' )
: m ( 'div.admin-wrapper' , [
m ( 'div.admin-actions' , [
m ( 'span' , 'Actions:' ) ,
m ( m . route . Link , { href : '/admin/pages/add' } , 'Create new page' ) ,
] ) ,
m ( 'article.editpage' , [
m ( 'header' , m ( 'h1' , 'All pages' ) ) ,
m ( 'div.error' , {
hidden : ! this . error ,
2022-07-27 08:41:18 +00:00
onclick : ( ) => { this . fetchPages ( vnode ) } ,
2019-09-13 13:33:10 +00:00
} , this . error ) ,
m ( 'table' , [
m ( 'thead' ,
m ( 'tr' , [
m ( 'th' , 'Title' ) ,
m ( 'th' , 'Path' ) ,
m ( 'th.right' , 'Updated' ) ,
m ( 'th.right' , 'Actions' ) ,
] )
) ,
m ( 'tbody' , this . pages . map ( AdminPages . drawPage . bind ( this , vnode ) ) ) ,
] ) ,
] ) ,
] )
) ,
m ( Dialogue , {
hidden : vnode . state . removePage === null ,
title : 'Delete ' + ( vnode . state . removePage ? vnode . state . removePage . name : '' ) ,
message : 'Are you sure you want to remove "' + ( vnode . state . removePage ? vnode . state . removePage . name : '' ) + '" (' + ( vnode . state . removePage ? vnode . state . removePage . path : '' ) + ')' ,
yes : 'Remove' ,
yesclass : 'alert' ,
no : 'Cancel' ,
noclass : 'cancel' ,
onyes : this . confirmRemovePage . bind ( this , vnode ) ,
onno : function ( ) { vnode . state . removePage = null } ,
} ) ,
]
} ,
}
module . exports = AdminPages