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.

87 lines
2.1 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.getTime())) {
  44. this.state.error = `Could not parse ${values.fictionalDate} as date.`
  45. if (values.fictionalDate.includes("th")) {
  46. this.state.error += " Try to remove the 'th'."
  47. }
  48. return
  49. }
  50. } else {
  51. fictionalDate = null
  52. }
  53. const input = {storyId: this.input.storyId, title: values.title, source: values.source, fictionalDate, commentMode: values.commentMode}
  54. chapterApi.addChapter(input).then(chapter => {
  55. this.emit("add", chapter)
  56. this.emit("close")
  57. this.state.values = {
  58. title: "",
  59. source: "",
  60. fictionalDate: "",
  61. commentMode: "Disabled",
  62. }
  63. }).catch(errs => {
  64. console.warn("Failed to edit:", errs)
  65. this.state.error = "Failed to edit: " + (errs[0]||errs||{}).message || errs
  66. }).then(() => {
  67. this.state.loading = false
  68. })
  69. }
  70. }