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.

95 lines
2.3 KiB

  1. const moment = require("moment")
  2. const {commentApi} = require("../../../../../rpdata/api/Comment")
  3. const {charactersApi} = require("../../../../../rpdata/api/Character")
  4. module.exports = class {
  5. onCreate(input) {
  6. this.state = {
  7. error: null,
  8. loading: false,
  9. values: {
  10. subject: "",
  11. source: "",
  12. characterName: "",
  13. characterId: "",
  14. fictionalDate: "",
  15. },
  16. characters: [],
  17. opened: false,
  18. }
  19. this.first = false
  20. }
  21. onMount() {
  22. charactersApi.list({author: (this.input.user||{}).name}).then((characters) => {
  23. this.state.characters = characters.sort((a,b) => a.name.localeCompare(b.name))
  24. })
  25. }
  26. change(key, ev) {
  27. this.state.values = Object.assign({}, this.state.values, {[key]: ev.target.value})
  28. }
  29. open() {
  30. if (!this.state.opened) {
  31. charactersApi.list({author: (this.input.user||{}).name}).then((characters) => {
  32. this.state.characters = characters.sort((a,b) => a.name.localeCompare(b.name))
  33. })
  34. }
  35. }
  36. close() {
  37. this.first = false
  38. this.state.opened = false
  39. this.emit("close")
  40. }
  41. save() {
  42. const values = this.state.values
  43. if (values.source.length < 1) {
  44. this.state.error = "You cannot post an empty comment."
  45. console.warn(values)
  46. return
  47. }
  48. let fictionalDate = new Date(values.fictionalDate + " UTC")
  49. if (values.fictionalDate != "") {
  50. if (Number.isNaN(fictionalDate)) {
  51. this.state.error = `Could not parse ${values.fictionalDate} as date!`
  52. return
  53. }
  54. } else {
  55. fictionalDate = null
  56. }
  57. const input = {
  58. chapterId: this.input.chapter.id,
  59. subject: values.subject,
  60. source: values.source,
  61. characterName: values.characterName,
  62. characterId: values.characterId,
  63. fictionalDate: fictionalDate,
  64. }
  65. commentApi.addComment(input).then(chapter => {
  66. this.emit("add", chapter)
  67. this.emit("close")
  68. this.state.values = {
  69. subject: "",
  70. source: "",
  71. characterName: "",
  72. characterId: "",
  73. fictionalDate: "",
  74. }
  75. }).catch(errs => {
  76. console.warn("Failed to add comemnt:", errs)
  77. this.state.error = "Failed to add comemnt: " + (errs[0]||errs||{}).message || errs
  78. }).then(() => {
  79. this.state.loading = false
  80. })
  81. }
  82. }