163 lines
4.7 KiB
JavaScript
163 lines
4.7 KiB
JavaScript
|
const Article = require('../api/article')
|
||
|
const pagination = require('../api/pagination')
|
||
|
const Dialogue = require('../widgets/dialogue')
|
||
|
const Pages = require('../widgets/pages')
|
||
|
const common = require('../api/common')
|
||
|
|
||
|
const AdminArticles = {
|
||
|
oninit: function(vnode) {
|
||
|
this.error = ''
|
||
|
this.loading = false
|
||
|
this.showLoading = null
|
||
|
this.data = {
|
||
|
articles: [],
|
||
|
total_articles: 0,
|
||
|
}
|
||
|
this.removeArticle = null
|
||
|
this.currentPage = Number(m.route.param('page')) || 1
|
||
|
|
||
|
this.fetchArticles(vnode)
|
||
|
},
|
||
|
|
||
|
onbeforeupdate: function(vnode) {
|
||
|
this.currentPage = Number(m.route.param('page')) || 1
|
||
|
|
||
|
if (this.currentPage !== this.lastpage) {
|
||
|
this.fetchArticles(vnode)
|
||
|
}
|
||
|
},
|
||
|
|
||
|
fetchArticles: function(vnode) {
|
||
|
this.error = ''
|
||
|
this.lastpage = this.currentPage
|
||
|
|
||
|
document.title = 'Articles Page ' + this.lastpage + ' - Admin NFP Moe'
|
||
|
|
||
|
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
|
||
|
}
|
||
|
|
||
|
return common.sendRequest({
|
||
|
method: 'GET',
|
||
|
url: '/api/auth/articles?page=' + (this.lastpage || 1),
|
||
|
})
|
||
|
.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
|
||
|
})
|
||
|
.then(() => {
|
||
|
clearTimeout(this.showLoading)
|
||
|
this.showLoading = null
|
||
|
this.loading = false
|
||
|
m.redraw()
|
||
|
})
|
||
|
},
|
||
|
|
||
|
confirmRemoveArticle: function(vnode) {
|
||
|
let removingArticle = this.removeArticle
|
||
|
this.removeArticle = null
|
||
|
this.loading = true
|
||
|
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
|
||
|
m.redraw()
|
||
|
}
|
||
|
)
|
||
|
},
|
||
|
|
||
|
drawArticle: function(vnode, article) {
|
||
|
return [
|
||
|
m('tr', {
|
||
|
class: article.hidden
|
||
|
? rowhidden
|
||
|
: article.is_featured
|
||
|
? 'rowfeatured'
|
||
|
: ''
|
||
|
}, [
|
||
|
m('td', m(m.route.Link, { href: '/admin/articles/' + article.id }, article.name)),
|
||
|
m('td', m(m.route.Link, { href: article.page_path }, article.page_name)),
|
||
|
m('td', m(m.route.Link, { href: '/article/' + article.path }, '/article/' + article.path)),
|
||
|
m('td.right', article.publish_at.replace('T', ' ').split('.')[0]),
|
||
|
m('td.right', article.admin_name),
|
||
|
m('td.right', m('button', { onclick: function() { vnode.state.removeArticle = article } }, 'Remove')),
|
||
|
]),
|
||
|
]
|
||
|
},
|
||
|
|
||
|
view: function(vnode) {
|
||
|
return [
|
||
|
m('div.admin-wrapper', [
|
||
|
m('div.admin-actions', [
|
||
|
m('span', 'Actions:'),
|
||
|
m(m.route.Link, { href: '/admin/articles/add' }, 'Create new article'),
|
||
|
]),
|
||
|
m('article.editarticle', [
|
||
|
m('header', m('h1', 'All articles')),
|
||
|
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', 'Title'),
|
||
|
m('th', 'Page'),
|
||
|
m('th', 'Path'),
|
||
|
m('th.right', 'Publish'),
|
||
|
m('th.right', 'By'),
|
||
|
m('th.right', 'Actions'),
|
||
|
])
|
||
|
),
|
||
|
m('tbody', this.data.articles.map((article) => this.drawArticle(vnode, article))),
|
||
|
],
|
||
|
),
|
||
|
/*m(Pages, {
|
||
|
base: '/admin/articles',
|
||
|
links: this.links,
|
||
|
}),*/
|
||
|
]),
|
||
|
]),
|
||
|
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
|