const moment = require("moment") const {commentApi} = require("../../../../../rpdata/api/Comment") const {charactersApi} = require("../../../../../rpdata/api/Character") module.exports = class { onCreate(input) { this.state = { error: null, loading: false, values: { subject: "", source: "", characterName: "", characterId: "", fictionalDate: "", }, characters: [], opened: false, } this.first = false } onMount() { charactersApi.list({author: (this.input.user||{}).name}).then((characters) => { this.state.characters = characters.sort((a,b) => a.name.localeCompare(b.name)) }) } change(key, ev) { this.state.values = Object.assign({}, this.state.values, {[key]: ev.target.value}) } open() { if (!this.state.opened) { charactersApi.list({author: (this.input.user||{}).name}).then((characters) => { this.state.characters = characters.sort((a,b) => a.name.localeCompare(b.name)) }) } } close() { this.first = false this.state.opened = false this.emit("close") } save() { const values = this.state.values if (values.source.length < 1) { this.state.error = "You cannot post an empty comment." console.warn(values) return } let fictionalDate = new Date(values.fictionalDate + " UTC") if (values.fictionalDate != "") { if (Number.isNaN(fictionalDate)) { this.state.error = `Could not parse ${values.fictionalDate} as date!` return } } else { fictionalDate = null } const input = { chapterId: this.input.chapter.id, subject: values.subject, source: values.source, characterName: values.characterName, characterId: values.characterId, fictionalDate: fictionalDate, } commentApi.addComment(input).then(chapter => { this.emit("add", chapter) this.emit("close") this.state.values = { subject: "", source: "", characterName: "", characterId: "", fictionalDate: "", } }).catch(errs => { console.warn("Failed to add comemnt:", errs) this.state.error = "Failed to add comemnt: " + (errs[0]||errs||{}).message || errs }).then(() => { this.state.loading = false }) } }