nfp_sites/app/admin/articles.js

157 lines
4.7 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')
2022-07-20 00:33:06 +00:00
const common = require('../api/common')
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
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
}
return common.sendRequest({
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) => {
console.log(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
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) {
return [
2022-07-20 00:33:06 +00:00
m('tr', {
class: article.hidden
? rowhidden
: article.is_featured
? 'rowfeatured'
: ''
}, [
m('td', m(m.route.Link, { href: '/admin/articles/' + article.path }, article.name)),
m('td', m(m.route.Link, { href: article.page_path }, article.page_name)),
2019-09-13 13:33:10 +00:00
m('td', m(m.route.Link, { href: '/article/' + article.path }, '/article/' + article.path)),
2022-07-20 00:33:06 +00:00
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 [
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),
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', '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))),
],
2019-09-14 19:03:38 +00:00
),
2022-07-20 00:33:06 +00:00
/*m(Pages, {
2019-09-14 19:03:38 +00:00
base: '/admin/articles',
links: this.links,
2022-07-20 00:33:06 +00:00
}),*/
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