const moment = require("moment") const {postApi} = require("../../../../../rpdata/api/Post") module.exports = class { onCreate(input) { this.state = { error: null, loading: false, values: { time: moment.utc(new Date()).format("YYYY-MM-DD HH:mm:ss"), kind: "text", nick: "", text: "", }, } this.first = false } change(key, ev) { this.state.values[key] = ev.target.value } open() { } close() { this.first = false this.emit("close") } save() { if (this.state.loading) { return } const values = this.state.values let time = new Date(values.time + " UTC") 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 }) } }