The frontend/UI server, written in JS using the MarkoJS library
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

88 lines
1.9 KiB

  1. const {storyApi} = require("../../../../../rpdata/api/Story")
  2. const {chapterApi} = require("../../../../../rpdata/api/Chapter")
  3. module.exports = class {
  4. onCreate(input) {
  5. this.state = {
  6. error: null,
  7. loading: false,
  8. loadingStories: true,
  9. stories: [],
  10. selected: null,
  11. }
  12. this.first = false
  13. }
  14. change(key, ev) {
  15. const value = ev.target.value
  16. switch (key) {
  17. case "selected": {
  18. this.state.selected = Object.assign({}, this.state.stories.find(s => s.id === value))
  19. break
  20. }
  21. }
  22. }
  23. open() {
  24. const promises = []
  25. if ((this.input.user.permissions || []).includes("chapter.move")) {
  26. promises.push(storyApi.list())
  27. } else {
  28. promises.push(storyApi.list({author: this.input.user.name}))
  29. promises.push(storyApi.list({open: true}))
  30. }
  31. this.state.loadingStories = true
  32. Promise.all(promises).then(lists => {
  33. this.state.stories = unique(Array.prototype.concat.call(...lists))
  34. this.state.selected = this.state.stories[0]
  35. }).catch(err => {
  36. console.warn(err)
  37. }).then(() => {
  38. this.state.loadingStories = false
  39. })
  40. }
  41. close() {
  42. this.first = false
  43. this.emit("close")
  44. }
  45. save() {
  46. if (this.state.loading) {
  47. return
  48. }
  49. const input = {id: this.input.chapter.id, storyId: this.state.selected.id}
  50. this.state.loading = true
  51. chapterApi.moveChapter(input).then(data => {
  52. this.emit("move", input)
  53. this.emit("close")
  54. }).catch(errs => {
  55. console.warn("Failed to edit:", errs)
  56. this.state.error = "Failed to edit: " + errs[0].message
  57. }).then(() => {
  58. this.state.loading = false
  59. })
  60. }
  61. }
  62. function unique(stories) {
  63. const ids = {}
  64. const results = []
  65. for (const story of stories) {
  66. if (ids[story.id]) {
  67. continue
  68. }
  69. ids[story.id] = true
  70. results.push(story)
  71. }
  72. return results
  73. }