const moment = require("moment") const {storyApi} = require("../../../../../rpdata/api/Story") module.exports = class { onCreate() { this.state = { error: null, values: { name: "", fictionalDate: "", category: "", open: "", listed: "", }, loading: false, } this.filled = false } onInput(input) { if (!this.filled) { this.state.values = { name: input.story.name, category: input.story.category, open: input.story.open, listed: input.story.listed, } if (input.story.fictionalDate != null) { this.state.values.fictionalDate = moment.utc(input.story.fictionalDate).format("MMM D, YYYY") } this.filled = true } } change(key, ev) { this.state.values[key] = ev.target.value this.state.values = Object.assign({}, this.state.values) } save() { const values = this.state.values 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 = {id: this.input.story.id, name: values.name, category: values.category, open: values.open, listed: values.listed} if (fictionalDate != null) { input.fictionalDate = fictionalDate } else { input.clearFictionalDate = true } this.state.loading = true storyApi.edit(input).then(data => { this.emit("edit", data) this.emit("close") }).catch(errs => { console.warn("Failed to edit:", errs) this.state.error = "Failed to edit: " + errs[0].message }).then(() => { this.state.loading = false }) } close() { this.emit("close") } }