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.

130 lines
3.4 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. placeholder: "(Required)",
  19. }
  20. }
  21. onInput(input) {
  22. if (input.comment) {
  23. charactersApi.list({author: (input.user||{}).name}).then((characters) => {
  24. this.state.characters = characters.sort((a,b) => a.name.localeCompare(b.name))
  25. })
  26. const comment = input.comment
  27. this.state.values = {
  28. subject: comment.subject,
  29. characterId: comment.characterId,
  30. characterName: comment.characterName,
  31. fictionalDate: moment(comment.fictionalDate).format("MMM D, YYYY"),
  32. source: comment.source,
  33. }
  34. }
  35. }
  36. change(key, ev) {
  37. this.state.values = Object.assign({}, this.state.values, {[key]: ev.target.value})
  38. if (key === "characterId") {
  39. if (ev.target.value !== "") {
  40. const character = this.state.characters.find(c => c.id === this.state.values.characterId)
  41. if (character != null) {
  42. this.state.placeholder = character.name
  43. } else {
  44. this.state.placeholder = "(Required)"
  45. }
  46. } else {
  47. this.state.placeholder = "(Required)"
  48. }
  49. }
  50. }
  51. close() {
  52. this.emit("close")
  53. }
  54. save() {
  55. const values = Object.assign({}, this.state.values)
  56. let clearFictionalDate = false
  57. if (values.source.length == 0) {
  58. this.state.error = "The comment body is empty."
  59. console.warn(values)
  60. return
  61. }
  62. if (this.input.chapter.commentMode !== "Chat" && values.subject.trim().length == 0) {
  63. this.state.error = "The subject is empty."
  64. return
  65. }
  66. if (values.characterName.trim().length < 1) {
  67. const character = this.state.characters.find(c => c.id === this.state.values.characterId)
  68. if (character == null) {
  69. this.state.error = "You need to give a display name for an anonymous character."
  70. return
  71. }
  72. values.characterName = character.name
  73. }
  74. let fictionalDate = new Date(values.fictionalDate + " UTC")
  75. if (values.fictionalDate != "") {
  76. if (Number.isNaN(fictionalDate)) {
  77. this.state.error = `Could not parse ${values.fictionalDate} as date!`
  78. return
  79. }
  80. } else {
  81. fictionalDate = null
  82. clearFictionalDate = true
  83. }
  84. const input = {
  85. commentId: this.input.comment.id,
  86. subject: values.subject,
  87. source: values.source,
  88. characterName: values.characterName,
  89. characterId: values.characterId,
  90. fictionalDate: fictionalDate,
  91. clearFictionalDate: clearFictionalDate,
  92. }
  93. if (input.characterId == "") {
  94. input.characterId = null
  95. }
  96. commentApi.editComment(input).then(comment => {
  97. this.emit("edit", comment)
  98. this.close()
  99. this.state.values = {
  100. subject: "",
  101. source: "",
  102. characterName: "",
  103. characterId: "",
  104. fictionalDate: "",
  105. }
  106. }).catch(errs => {
  107. console.warn("Failed to add comemnt:", errs)
  108. this.state.error = "Failed to add comemnt: " + (errs[0]||errs||{}).message || errs
  109. }).then(() => {
  110. this.state.loading = false
  111. })
  112. }
  113. }