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.4 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. const {query} = require("../client")
  2. const {Comment} = require("./Comment")
  3. class Chapter {
  4. /**
  5. * @param {string} id
  6. * @param {string} title
  7. * @param {string} author
  8. * @param {string} source
  9. * @param {string | Date} createdDate
  10. * @param {string | Date} fictionalDate
  11. * @param {string | Date} editedDate
  12. * @param {boolean} canComment
  13. * @param {"Disabled"|"Article"|"Chat"|"Message"} commentMode
  14. * @param {boolean} commentsLocked
  15. * @param {any[]} comments
  16. */
  17. constructor(id, title, author, source, createdDate, fictionalDate, editedDate, canComment, commentMode, commentsLocked, comments) {
  18. this.id = id
  19. this.title = title
  20. this.author = author
  21. this.source = source
  22. this.createdDate = new Date(createdDate)
  23. this.fictionalDate = fictionalDate != null ? new Date(fictionalDate) : null
  24. this.editedDate = new Date(editedDate)
  25. this.canComment = canComment
  26. this.commentMode = commentMode
  27. this.commentsLocked = commentsLocked
  28. this.comments = (comments||[]).map(c => Comment.fromData(c))
  29. }
  30. }
  31. class ChapterAPI {
  32. /**
  33. * Call `addChapter(input)` mutation, returns the entire object.
  34. *
  35. * @param {{id:string, storyId:string, title:string, author?:string, fictionalDate?:Date, source: string}} input
  36. * @returns {Promise<Chapter>}
  37. */
  38. addChapter(input) {
  39. return query(`
  40. mutation AddChapter($input: ChapterAddInput!) {
  41. addChapter(input: $input) {
  42. id
  43. title
  44. author
  45. source
  46. createdDate
  47. fictionalDate
  48. editedDate
  49. }
  50. }
  51. `, {input}, {permissions: ["member", "story.add"]}).then((data) => {
  52. const ac = data.addChapter
  53. return new Chapter(ac.id, ac.title, ac.author, ac.source, ac.createdDate, ac.fictionalDate, ac.editedDate)
  54. })
  55. }
  56. /**
  57. * Call `editChapter(input)` mutation, returns editable fields
  58. *
  59. * @param {{id:string, title:string, fictionalDate:Date, source: string}} input
  60. * @returns {Promise<{title:string, fictionalDate:Date, source:string}>}
  61. */
  62. editChapter(input) {
  63. return query(`
  64. mutation EditChapter($input: ChapterEditInput!) {
  65. editChapter(input: $input) {
  66. title
  67. fictionalDate
  68. source
  69. }
  70. }
  71. `, {input}, {permissions: ["member", "story.edit"]}).then(({editChapter}) => {
  72. if (editChapter.fictionalDate != null) {
  73. editChapter.fictionalDate = new Date(editChapter.fictionalDate)
  74. }
  75. return editChapter
  76. })
  77. }
  78. /**
  79. * Call `moveChapter(input)` mutation, returns the ID.
  80. *
  81. * @param {{id:string, storyId:string}} input
  82. * @returns {Promise<{id:string}>}
  83. */
  84. moveChapter(input) {
  85. return query(`
  86. mutation MoveChapter($input: ChapterMoveInput!) {
  87. moveChapter(input: $input) {
  88. id
  89. }
  90. }
  91. `, {input}, {permissions: ["member", "story.move"]}).then(({moveChapter}) => {
  92. return moveChapter
  93. })
  94. }
  95. /**
  96. * Call `removeChapter(input)` mutation, returns only the id
  97. *
  98. * @param {{id:string}} input
  99. * @returns {Promise<{id: string}>}
  100. */
  101. removeChapter(input) {
  102. return query(`
  103. mutation RemoveChapter($input: ChapterRemoveInput!) {
  104. removeChapter(input: $input) {
  105. id
  106. }
  107. }
  108. `, {input}, {permissions: ["member", "story.edit"]}).then(({removeChapter}) => {
  109. return removeChapter
  110. })
  111. }
  112. }
  113. module.exports = {Chapter, chapterApi: new ChapterAPI}