nfp_sites/filadelfia_web/app/page_upload.js
2023-11-09 09:45:37 +00:00

91 lines
2.2 KiB
JavaScript

const m = require('mithril')
const Authentication = require('./authentication')
const api = require('./api')
const Login = {
oninit: function(vnode) {
this.redirect = vnode.attrs.redirect || ''
if (!Authentication.currentUser) return m.route.set('/')
this.error = ''
this.loading = false
this.body = {
year: new Date().getFullYear(),
month: new Date().getMonth() + 1,
title: '',
file: null,
}
},
uploadfile: function(vnode, e) {
e.preventDefault()
this.error = ''
if (!this.password) this.error = 'Password is missing'
if (!this.username) this.error = 'Email is missing'
if (this.error) return false
this.loading = true
api.sendRequest({
method: 'POST',
url: '/api/authentication/login',
body: {
email: this.username,
password: this.password,
},
})
.then((result) => {
if (!result.token) {
return Promise.reject(new Error('Server authentication down.'))
}
Authentication.updateToken(result.token)
m.route.set(this.redirect || '/browse')
})
.catch((error) => {
this.error = 'Error while logging in! ' + error.message
vnode.state.password = ''
})
.then(() => {
this.loading = false
m.redraw()
})
return false
},
view: function(vnode) {
return [
m('div.modal', [
m('form', {
onsubmit: this.uploadfile.bind(this, vnode),
}, [
m('h3', 'Filadelfia archival center'),
this.error ? m('p.error', this.error) : null,
m('label', 'Email or name'),
m('input', {
type: 'text',
value: this.username,
oninput: (e) => { this.username = e.currentTarget.value },
}),
m('label', 'Password'),
m('input', {
type: 'password',
value: this.password,
oninput: (e) => { this.password = e.currentTarget.value },
}),
m('input.spinner', {
hidden: this.loading,
type: 'submit',
value: 'Log in',
}),
this.loading ? m('div.loading-spinner') : null,
]),
m('div.login--asuna.spritesheet'),
]),
]
},
}
module.exports = Login