2022-08-05 14:26:29 +00:00
const Dialogue = require ( './dialogue' )
const api = require ( '../api' )
const Paginator = require ( '../paginator' )
const ItemsPerPage = 20
2019-09-13 13:33:10 +00:00
const AdminArticles = {
oninit : function ( vnode ) {
this . error = ''
2022-07-20 00:33:06 +00:00
this . loading = false
this . showLoading = null
this . data = {
articles : [ ] ,
total _articles : 0 ,
}
2019-09-13 13:33:10 +00:00
this . removeArticle = null
2022-07-20 00:33:06 +00:00
this . currentPage = Number ( m . route . param ( 'page' ) ) || 1
2019-09-13 13:33:10 +00:00
2019-09-14 19:03:38 +00:00
this . fetchArticles ( vnode )
} ,
2022-07-20 00:33:06 +00:00
onbeforeupdate : function ( vnode ) {
this . currentPage = Number ( m . route . param ( 'page' ) ) || 1
if ( this . currentPage !== this . lastpage ) {
2019-09-14 19:03:38 +00:00
this . fetchArticles ( vnode )
}
} ,
fetchArticles : function ( vnode ) {
2022-07-20 00:33:06 +00:00
this . error = ''
this . lastpage = this . currentPage
2019-09-14 19:03:38 +00:00
2019-10-01 17:18:20 +00:00
document . title = 'Articles Page ' + this . lastpage + ' - Admin NFP Moe'
2022-07-20 00:33:06 +00:00
if ( this . showLoading ) {
clearTimeout ( this . showLoading )
}
if ( this . data . articles . length ) {
this . showLoading = setTimeout ( ( ) => {
this . showLoading = null
this . loading = true
m . redraw ( )
} , 150 )
} else {
this . loading = true
}
2022-08-05 14:26:29 +00:00
return api . sendRequest ( {
2022-07-20 00:33:06 +00:00
method : 'GET' ,
url : '/api/auth/articles?page=' + ( this . lastpage || 1 ) ,
2019-09-13 13:33:10 +00:00
} )
2022-07-20 00:33:06 +00:00
. then ( ( result ) => {
this . data = result
this . data . articles . forEach ( ( article ) => {
article . hidden = new Date ( ) < new Date ( article . publish _at )
article . page _path = article . page _path ? '/page/' + article . page _path : '/'
article . page _name = article . page _name || 'Frontpage'
} )
} , ( err ) => {
this . error = err . message
2019-09-13 13:33:10 +00:00
} )
2022-07-20 00:33:06 +00:00
. then ( ( ) => {
clearTimeout ( this . showLoading )
this . showLoading = null
this . loading = false
2019-09-13 13:33:10 +00:00
m . redraw ( )
} )
} ,
confirmRemoveArticle : function ( vnode ) {
let removingArticle = this . removeArticle
this . removeArticle = null
this . loading = true
2022-07-27 08:41:18 +00:00
m . redraw ( )
return common . sendRequest ( {
method : 'DELETE' ,
url : '/api/auth/articles/' + removingArticle . id ,
} )
. then (
( ) => this . fetchArticles ( 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
} ,
drawArticle : function ( vnode , article ) {
return [
2022-07-20 00:33:06 +00:00
m ( 'tr' , {
class : article . hidden
? rowhidden
: article . is _featured
? 'rowfeatured'
: ''
} , [
2022-07-27 08:41:18 +00:00
m ( 'td' , m ( m . route . Link , { href : '/admin/articles/' + article . id } , article . name ) ) ,
2022-08-05 14:26:29 +00:00
m ( 'td' , m ( m . route . Link , { href : '/article/' + article . path } , 'View' ) ) ,
2022-07-20 00:33:06 +00:00
m ( 'td' , m ( m . route . Link , { href : article . page _path } , article . page _name ) ) ,
m ( 'td.right' , article . publish _at . replace ( 'T' , ' ' ) . split ( '.' ) [ 0 ] ) ,
m ( 'td.right' , article . admin _name ) ,
2019-09-13 13:33:10 +00:00
m ( 'td.right' , m ( 'button' , { onclick : function ( ) { vnode . state . removeArticle = article } } , 'Remove' ) ) ,
2019-09-14 19:03:38 +00:00
] ) ,
2019-09-13 13:33:10 +00:00
]
} ,
view : function ( vnode ) {
return [
2022-08-05 14:26:29 +00:00
m ( 'div.wrapper.admin' , [
m ( 'div.inside' , [
m ( 'h2' , 'All articles' ) ,
m ( 'div.actions' , [
2019-09-14 19:03:38 +00:00
m ( 'span' , 'Actions:' ) ,
m ( m . route . Link , { href : '/admin/articles/add' } , 'Create new article' ) ,
] ) ,
m ( 'div.error' , {
hidden : ! this . error ,
onclick : function ( ) { vnode . state . error = '' } ,
} , this . error ) ,
2022-07-20 00:33:06 +00:00
this . loading
2019-09-14 19:03:38 +00:00
? m ( 'div.loading-spinner.full' )
: m ( 'table' , [
2022-07-20 00:33:06 +00:00
m ( 'thead' ,
m ( 'tr' , [
m ( 'th' , 'Title' ) ,
m ( 'th' , 'Path' ) ,
2022-08-05 14:26:29 +00:00
m ( 'th' , 'Page' ) ,
2022-07-20 00:33:06 +00:00
m ( 'th.right' , 'Publish' ) ,
m ( 'th.right' , 'By' ) ,
m ( 'th.right' , 'Actions' ) ,
] )
) ,
m ( 'tbody' , this . data . articles . map ( ( article ) => this . drawArticle ( vnode , article ) ) ) ,
] ,
2019-09-14 19:03:38 +00:00
) ,
2022-08-05 14:26:29 +00:00
m ( Paginator , {
2019-09-14 19:03:38 +00:00
base : '/admin/articles' ,
2022-08-05 14:26:29 +00:00
page : this . currentPage ,
perPage : ItemsPerPage ,
total : this . data . total _articles ,
} ) ,
2019-09-14 19:03:38 +00:00
] ) ,
] ) ,
2019-09-13 13:33:10 +00:00
m ( Dialogue , {
hidden : vnode . state . removeArticle === null ,
title : 'Delete ' + ( vnode . state . removeArticle ? vnode . state . removeArticle . name : '' ) ,
message : 'Are you sure you want to remove "' + ( vnode . state . removeArticle ? vnode . state . removeArticle . name : '' ) + '" (' + ( vnode . state . removeArticle ? vnode . state . removeArticle . path : '' ) + ')' ,
yes : 'Remove' ,
yesclass : 'alert' ,
no : 'Cancel' ,
noclass : 'cancel' ,
onyes : this . confirmRemoveArticle . bind ( this , vnode ) ,
onno : function ( ) { vnode . state . removeArticle = null } ,
} ) ,
]
} ,
}
module . exports = AdminArticles