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.

81 lines
1.7 KiB

  1. const moment = require("moment")
  2. const {chapterApi} = require("../../../../../rpdata/api/Chapter")
  3. module.exports = class {
  4. onCreate(input) {
  5. this.state = {
  6. error: null,
  7. loading: false,
  8. values: {
  9. title: "",
  10. source: "",
  11. fictionalDate: "",
  12. },
  13. }
  14. this.first = false
  15. }
  16. onInput(input) {
  17. if (input.chapter && !this.first) {
  18. let {fictionalDate, title, source} = input.chapter
  19. if (fictionalDate != null) {
  20. fictionalDate = moment.utc(fictionalDate).format("MMM D, YYYY")
  21. }
  22. this.state.values = {fictionalDate, title, source}
  23. }
  24. }
  25. change(key, ev) {
  26. this.state.values[key] = ev.target.value
  27. }
  28. open() {
  29. }
  30. close() {
  31. this.first = false
  32. this.emit("close")
  33. }
  34. save() {
  35. const values = this.state.values
  36. if (values.source.length < 1) {
  37. this.state.error = "You cannot post an empty chapter."
  38. return
  39. }
  40. let fictionalDate = new Date(values.fictionalDate + " UTC")
  41. if (values.fictionalDate != "") {
  42. if (Number.isNaN(fictionalDate)) {
  43. this.state.error = `Could not parse ${values.fictionalDate} as date!`
  44. return
  45. }
  46. } else {
  47. fictionalDate = null
  48. }
  49. const input = {storyId: this.input.storyId, title: values.title, source: values.source, fictionalDate}
  50. chapterApi.addChapter(input).then(chapter => {
  51. this.emit("add", chapter)
  52. this.emit("close")
  53. this.state.values = {
  54. title: "",
  55. source: "",
  56. fictionalDate: "",
  57. }
  58. }).catch(errs => {
  59. console.warn("Failed to edit:", errs)
  60. this.state.error = "Failed to edit: " + errs[0].message
  61. }).then(() => {
  62. this.state.loading = false
  63. })
  64. }
  65. }