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.

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