const PageTree = require('../page_tree') const Dialogue = require('./dialogue') const api = require('../api') const AdminPages = { oninit: function(vnode) { this.error = '' this.pages = [] this.removePage = null document.title = 'Pages - Admin NFP Moe' this.fetchPages(vnode) }, fetchPages: function(vnode) { this.loading = true this.error = '' return api.sendRequest({ method: 'GET', url: '/api/auth/pages', }) .then((result) => { this.pages = result.tree }, (err) => { this.error = err.message }) .then(() => { this.loading = false m.redraw() }) }, confirmRemovePage: function(vnode) { let removingPage = this.removePage this.removePage = null this.loading = true m.redraw() return api.sendRequest({ method: 'DELETE', url: '/api/auth/pages/' + removingPage.id, }) .then(() => PageTree.refreshTree()) .then( () => this.fetchPages(vnode), (err) => { this.error = err.message this.loading = false m.redraw() } ) }, drawPage: function(vnode, page) { return [ m('tr', [ m('td', [ page.parent_id ? m('span.subpage', ' - ') : null, m(m.route.Link, { href: '/admin/pages/' + page.id }, page.name), ]), m('td', m(m.route.Link, { href: '/page/' + page.path }, '/page/' + page.path)), m('td.right', page.updated_at.replace('T', ' ').split('.')[0]), m('td.right', m('button', { onclick: function() { vnode.state.removePage = page } }, 'Remove')), ]), ].concat(page.children ? page.children.map(AdminPages.drawPage.bind(this, vnode)) : []) }, view: function(vnode) { return [ m('div.wrapper.admin', [ m('div.inside', [ m('h2', 'All pages'), m('div.actions', [ m('span', 'Actions:'), m(m.route.Link, { href: '/admin/pages/add' }, 'Create new page'), ]), 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.right', 'Updated'), m('th.right', 'Actions'), ]) ), m('tbody', this.pages.map(AdminPages.drawPage.bind(this, vnode))), ], ), /*m(Pages, { base: '/admin/articles', links: this.links, }),*/ ]), ]), m(Dialogue, { hidden: vnode.state.removePage === null, title: 'Delete ' + (vnode.state.removePage ? vnode.state.removePage.name : ''), message: 'Are you sure you want to remove "' + (vnode.state.removePage ? vnode.state.removePage.name : '') + '" (' + (vnode.state.removePage ? vnode.state.removePage.path : '') + ')', yes: 'Remove', yesclass: 'alert', no: 'Cancel', noclass: 'cancel', onyes: this.confirmRemovePage.bind(this, vnode), onno: function() { vnode.state.removePage = null }, }), ] }, } module.exports = AdminPages