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.

120 lines
3.0 KiB

  1. const {query} = require("../client")
  2. class Comment {
  3. /**
  4. * @param {string} id
  5. * @param {string} subject
  6. * @param {string} author
  7. * @param {string} characterName
  8. * @param {CommentCharacter} character
  9. * @param {string | number | Date} fictionalDate
  10. * @param {string | number | Date} createdDate
  11. * @param {string | number | Date} editedDate
  12. * @param {string} source
  13. */
  14. constructor(id, subject, author, characterName, character, fictionalDate, createdDate, editedDate, source) {
  15. this.id = id
  16. this.subject = subject
  17. this.author = author
  18. this.characterName = characterName
  19. this.character = character != null ? CommentCharacter.fromData(character) : null
  20. this.fictionalDate = fictionalDate != null ? new Date(fictionalDate) : null
  21. this.createdDate = new Date(createdDate)
  22. this.editedDate = new Date(editedDate)
  23. this.source = source
  24. }
  25. static fromData(data) {
  26. return new Comment(data.id, data.subject, data.author, data.characterName, data.character, data.fictionalDate, data.createdDate, data.editedDate, data.source)
  27. }
  28. }
  29. class CommentCharacter {
  30. /**
  31. * @param {string} id
  32. * @param {string} name
  33. * @param {string} author
  34. * @param {string} description
  35. */
  36. constructor(id, name, author, description) {
  37. this.id = id
  38. this.name = name
  39. this.author = author
  40. this.description = description
  41. }
  42. static fromData(data) {
  43. return new CommentCharacter(data.id, data.name, data.author, data.description)
  44. }
  45. }
  46. class CommentAPI {
  47. /**
  48. * @param {string} id
  49. * @returns {Promise<Comment>}
  50. */
  51. find(id) {
  52. return query(`
  53. query FindComment($id: String!) {
  54. comment(id: $id) {
  55. id
  56. subject
  57. author
  58. characterName
  59. character {
  60. id
  61. name
  62. author
  63. description
  64. }
  65. fictionalDate
  66. createdDate
  67. editedDate
  68. source
  69. }
  70. }
  71. `, {id}).then(({comment}) => {
  72. return new Comment(
  73. comment.id, comment.subject, comment.author,
  74. comment.characterName, comment.character,
  75. comment.fictionalDate, comment.createdDate,
  76. comment.editedDate, comment.source,
  77. )
  78. })
  79. }
  80. /**
  81. * @param {any} input
  82. * @returns {Promise<Comment>}
  83. */
  84. addComment(input) {
  85. return query(`
  86. mutation AddComment($input: CommentAddInput!) {
  87. addComment(input: $input) {
  88. id
  89. subject
  90. author
  91. characterName
  92. character {
  93. id
  94. name
  95. author
  96. description
  97. }
  98. fictionalDate
  99. createdDate
  100. editedDate
  101. source
  102. }
  103. }
  104. `, {input}).then(({comment}) => {
  105. return new Comment(
  106. comment.id, comment.subject, comment.author,
  107. comment.characterName, comment.character,
  108. comment.fictionalDate, comment.createdDate,
  109. comment.editedDate, comment.source,
  110. )
  111. })
  112. }
  113. }
  114. module.exports = {Comment, CommentCharacter, commentApi: new CommentAPI}