Files
frontend/marko/page/logs-content/components/add-post-modal/component.js
T

57 lines
1.3 KiB
JavaScript

const moment = require("moment")
const {postApi} = require("../../../../../rpdata/api/Post")
module.exports = class {
onCreate(input) {
this.state = {
error: null,
loading: false,
values: {
time: moment(new Date()).format("YYYY-MM-DD HH:mm:ss"),
kind: "text",
nick: "",
text: "",
},
}
}
change(key, ev) {
this.state.values[key] = ev.target.value
}
open() {
this.state.values.time = moment(new Date()).format("YYYY-MM-DD HH:mm:ss")
}
close() {
this.emit("close")
}
save() {
if (this.state.loading) {
return
}
const values = this.state.values
let time = new Date(values.time)
if (Number.isNaN(time)) {
this.state.error = `Could not parse ${values.time} as date`
return
}
const input = {logId: this.input.logId, time, kind: this.state.values.kind, nick: this.state.values.nick, text: this.state.values.text}
this.state.loading = true
postApi.add(input).then(data => {
this.emit("added", data)
this.emit("close")
}).catch(errs => {
console.warn("Failed to add post:", errs)
this.state.error = "Failed to add post: " + errs[0].message
}).then(() => {
this.state.loading = false
})
}
}