nfp_sites/app/admin/articles.js

145 lines
4.3 KiB
JavaScript
Raw Normal View History

2019-10-01 11:35:00 +00:00
const Article = require('../api/article')
const pagination = require('../api/pagination')
2019-09-13 13:33:10 +00:00
const Dialogue = require('../widgets/dialogue')
2019-09-14 19:03:38 +00:00
const Pages = require('../widgets/pages')
2019-09-13 13:33:10 +00:00
const AdminArticles = {
oninit: function(vnode) {
this.error = ''
2019-09-14 19:03:38 +00:00
this.lastpage = m.route.param('page') || '1'
2019-09-13 13:33:10 +00:00
this.articles = []
this.removeArticle = null
2019-09-14 19:03:38 +00:00
this.fetchArticles(vnode)
},
onupdate: function(vnode) {
if (m.route.param('page') && m.route.param('page') !== this.lastpage) {
this.fetchArticles(vnode)
}
},
fetchArticles: function(vnode) {
this.loading = true
this.links = null
this.lastpage = m.route.param('page') || '1'
document.title = 'Articles Page ' + this.lastpage + ' - Admin NFP Moe'
2019-10-01 11:35:00 +00:00
return pagination.fetchPage(Article.getAllArticlesPagination({
2019-09-14 19:03:38 +00:00
per_page: 10,
page: this.lastpage,
includes: ['parent'],
}))
2019-09-13 13:33:10 +00:00
.then(function(result) {
2019-09-14 19:03:38 +00:00
vnode.state.articles = result.data
vnode.state.links = result.links
2019-09-13 13:33:10 +00:00
})
.catch(function(err) {
vnode.state.error = err.message
})
.then(function() {
vnode.state.loading = false
m.redraw()
})
},
confirmRemoveArticle: function(vnode) {
let removingArticle = this.removeArticle
this.removeArticle = null
this.loading = true
2019-10-01 11:35:00 +00:00
Article.removeArticle(removingArticle, removingArticle.id)
2019-09-13 13:33:10 +00:00
.then(this.oninit.bind(this, vnode))
.catch(function(err) {
vnode.state.error = err.message
vnode.state.loading = false
m.redraw()
})
},
drawArticle: function(vnode, article) {
let parent
if (article.parent) {
parent = {
path: '/page/' + article.parent.path,
name: article.parent.name,
}
} else {
parent = {
path: '/',
name: '-- Frontpage --',
}
}
2019-10-02 00:16:11 +00:00
let other = ''
let className = ''
if (new Date() < new Date(article.published_at)) {
other = '(hidden)'
className = 'rowhidden'
} else if (article.is_featured) {
other = '(featured)'
className = 'rowfeatured'
}
2019-09-13 13:33:10 +00:00
return [
2019-10-02 00:16:11 +00:00
m('tr', { class: className }, [
2019-09-13 13:33:10 +00:00
m('td', m(m.route.Link, { href: '/admin/articles/' + article.id }, article.name)),
m('td', m(m.route.Link, { href: parent.path }, parent.name)),
m('td', m(m.route.Link, { href: '/article/' + article.path }, '/article/' + article.path)),
2019-10-02 00:16:11 +00:00
m('td.right', article.published_at.replace('T', ' ').split('.')[0]),
m('td.right', other),
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 [
2019-09-14 19:03:38 +00:00
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', [
2019-09-13 13:33:10 +00:00
m('thead',
m('tr', [
m('th', 'Title'),
m('th', 'Page'),
m('th', 'Path'),
2019-10-02 00:16:11 +00:00
m('th.right', 'Publish'),
m('th.right', 'Other'),
2019-09-13 13:33:10 +00:00
m('th.right', 'Actions'),
])
),
m('tbody', this.articles.map(AdminArticles.drawArticle.bind(this, vnode))),
2019-09-14 19:03:38 +00:00
])
),
m(Pages, {
base: '/admin/articles',
links: this.links,
}),
]),
]),
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