nfp_sites/filadelfia_web/app/page_article.js

134 lines
3.3 KiB
JavaScript
Raw Normal View History

2023-11-20 07:12:08 +00:00
const m = require('mithril')
const api = require('./api')
const Authentication = require('./authentication')
2023-11-29 19:19:41 +00:00
const Input = require('./input')
2023-11-20 07:12:08 +00:00
const lang = require('./lang')
const Article = {
oninit: function(vnode) {
Authentication.requiresLogin()
this.error = ''
this.path = ''
this.data = null
2023-11-29 19:19:41 +00:00
this.editing = false
this.form = {
title: 'Sunnudagssamkoma',
date: new Date(),
banner: null,
metadata: {
speaker: '',
},
}
2023-11-20 07:12:08 +00:00
this.onbeforeupdate(vnode)
},
onbeforeupdate: function(vnode) {
let path = m.route.param('id')
if (this.path === path) return
this.fetchArticle(vnode, path)
},
fetchArticle: function(vnode, path) {
this.error = ''
this.data = null
this.path = path
api.sendRequest({
method: 'GET',
url: '/api/auth/articles/' + this.path,
})
.then((result) => {
this.data = result.article
2023-11-29 19:19:41 +00:00
this.gotArticle(vnode)
2023-11-20 07:12:08 +00:00
}, (err) => {
this.error = err.message
})
},
2023-11-29 19:19:41 +00:00
gotArticle: function(vnode) {
2023-11-20 07:12:08 +00:00
if (!this.data) {
2023-11-29 19:19:41 +00:00
return this.error = 'Article not found'
2023-11-20 07:12:08 +00:00
}
2023-11-29 19:19:41 +00:00
this.form.title = this.data.name
this.form.date = new Date(this.data.publish_at)
this.form.banner = this.data.banner_path
this.form.metadata.speaker = this.data.content.speaker
},
updatevideo: function(vnode, e) {
this.error = ''
if (this.error) return false
2023-11-20 07:12:08 +00:00
},
view: function(vnode) {
console.log(this.data)
return [
api.loading ? m('div.loading-spinner') : null,
this.error
? m('div.full-error', { onclick: this.fetchArticle.bind(this, vnode, this.path) }, [
this.error, m('br'), 'Click here to try again'
])
: null,
this.data?.media_path
? [
m('.player', [
m('video', {
crossorigin: '',
controls: true,
preload: 'none',
2023-11-29 19:19:41 +00:00
poster: this.data.banner_path || '/assets/placeholder.avif',
2023-11-20 07:12:08 +00:00
}, [
m('source', {
src: this.data.media_path
})
]),
]),
]
: null,
2023-11-29 19:19:41 +00:00
this.editing
? m('form.article', {
onsubmit: this.updatevideo.bind(this, vnode),
}, [
m('div.form-row', [
m('div.form-columns', [
m(Input, {
label: 'Mynd',
type: 'file',
accept: 'image/*',
utility: 'image',
form: this.form,
formKey: 'banner',
}),
]),
m('div.form-columns', [
m(Input, {
label: 'Title',
form: this.form,
formKey: 'title',
}),
m(Input, {
label: 'Date (dd.mm.yyyy)',
type: 'text',
utility: 'datetime',
form: this.form,
formKey: 'date',
}),
]),
]),
m('p.separator', 'Optional'),
m(Input, {
label: 'Speaker',
form: this.form.metadata,
formKey: 'speaker',
}),
])
: m('div.article', [
m('h1', this.data.name)
]),
2023-11-20 07:12:08 +00:00
]
},
}
module.exports = Article