164 lines
4.6 KiB
JavaScript
164 lines
4.6 KiB
JavaScript
const Dialogue = require('./dialogue')
|
|
const api = require('../api')
|
|
const Paginator = require('../paginator')
|
|
|
|
const ItemsPerPage = 20
|
|
|
|
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 api.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/' + article.path }, 'View')),
|
|
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),
|
|
m('td.right', m('button', { onclick: function() { vnode.state.removeArticle = article } }, 'Remove')),
|
|
]),
|
|
]
|
|
},
|
|
|
|
view: function(vnode) {
|
|
return [
|
|
m('div.wrapper.admin', [
|
|
m('div.inside', [
|
|
m('h2', 'All articles'),
|
|
m('div.actions', [
|
|
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),
|
|
this.loading
|
|
? m('div.loading-spinner.full')
|
|
: m('table', [
|
|
m('thead',
|
|
m('tr', [
|
|
m('th', 'Title'),
|
|
m('th', 'Path'),
|
|
m('th', 'Page'),
|
|
m('th.right', 'Publish'),
|
|
m('th.right', 'By'),
|
|
m('th.right', 'Actions'),
|
|
])
|
|
),
|
|
m('tbody', this.data.articles.map((article) => this.drawArticle(vnode, article))),
|
|
],
|
|
),
|
|
m(Paginator, {
|
|
base: '/admin/articles',
|
|
page: this.currentPage,
|
|
perPage: ItemsPerPage,
|
|
total: this.data.total_articles,
|
|
}),
|
|
]),
|
|
]),
|
|
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
|