const moment = require("moment") const {chapterApi} = require("../../../../../rpdata/api/Chapter") module.exports = class { onCreate(input) { this.state = { error: null, loading: false, values: { title: "", source: "", fictionalDate: "", commentMode: "Disabled", }, } this.first = false } onInput(input) { if (input.chapter && !this.first) { let {fictionalDate, title, source, commentMode, commentsLocked} = input.chapter if (fictionalDate != null) { fictionalDate = moment.utc(fictionalDate).format("MMM D, YYYY") } this.state.values = {fictionalDate, title, source, commentMode, commentsLocked} } } change(key, ev) { this.state.values = Object.assign({}, this.state.values, {[key]: ev.target.value}) } open() { } close() { this.first = false this.emit("close") } save() { const values = this.state.values if (values.source.length < 1) { this.state.error = "You cannot post an empty chapter." 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 = {storyId: this.input.storyId, title: values.title, source: values.source, fictionalDate, commentMode: values.commentMode} chapterApi.addChapter(input).then(chapter => { this.emit("add", chapter) this.emit("close") this.state.values = { title: "", source: "", fictionalDate: "", commentMode: "Disabled", } }).catch(errs => { console.warn("Failed to edit:", errs) this.state.error = "Failed to edit: " + (errs[0]||errs||{}).message || errs }).then(() => { this.state.loading = false }) } }