const moment = require("moment") const {storyApi} = require("../../../../../rpdata/api/Story") const ORDINAL_REGEX = /[0-9]{1,2}(st|nd|rd|th)/g 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.getTime())) { this.state.error = `Could not parse ${values.fictionalDate} as date.` const ordinal = values.fictionalDate.match(ORDINAL_REGEX)[0] if (ordinal != null) { this.state.error += " Try to remove the '"+ordinal.slice(-2)+"'." } 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") } }