const {storyApi} = require("../../../../../rpdata/api/Story") const {chapterApi} = require("../../../../../rpdata/api/Chapter") module.exports = class { onCreate(input) { this.state = { error: null, loading: false, loadingStories: true, stories: [], selected: null, } this.first = false } change(key, ev) { const value = ev.target.value switch (key) { case "selected": { this.state.selected = Object.assign({}, this.state.stories.find(s => s.id === value)) break } } } open() { const promises = [] if ((this.input.user.permissions || []).includes("chapter.move")) { promises.push(storyApi.list()) } else { promises.push(storyApi.list({author: this.input.user.name})) promises.push(storyApi.list({open: true})) } this.state.loadingStories = true Promise.all(promises).then(lists => { this.state.stories = unique(Array.prototype.concat.call(...lists)) this.state.selected = this.state.stories[0] }).catch(err => { console.warn(err) }).then(() => { this.state.loadingStories = false }) } close() { this.first = false this.emit("close") } save() { if (this.state.loading) { return } const input = {id: this.input.chapter.id, storyId: this.state.selected.id} this.state.loading = true chapterApi.moveChapter(input).then(data => { this.emit("move", input) 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 }) } } function unique(stories) { const ids = {} const results = [] for (const story of stories) { if (ids[story.id]) { continue } ids[story.id] = true results.push(story) } return results }