@@ -0,0 +1,89 @@
|
||||
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
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
$ const selected = (state.selected||{id:null});
|
||||
|
||||
<modal class="modal color-text nolabel" key="modal" enabled=(input.enabled) closable on-close("close") on-open("open") >
|
||||
<h1>Move Chapter</h1>
|
||||
|
||||
<p key="error" class="color-error">${state.error}</p>
|
||||
|
||||
<label>Target story</label>
|
||||
<select key="story" on-change("change", "selected") value=selected.id>
|
||||
<option for(story in state.stories) value=story.id selected=(selected.id === story.id)>${story.name}</option>
|
||||
</select>
|
||||
|
||||
<if-permitted not permission="story.move">
|
||||
<p>Only stories you are allowed to move the chapter to appear in the above list.</p>
|
||||
</if-permitted>
|
||||
|
||||
<button disabled=state.loading on-click("save")>Move</button>
|
||||
</modal>
|
||||
Reference in New Issue
Block a user