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.

146 lines
3.9 KiB

6 years ago
  1. const {query} = require("../client")
  2. const {Tag} = require("./Tag")
  3. const {Chapter} = require("./Chapter")
  4. class Story {
  5. /**
  6. * Construct a story object. You should use the API instead of calling this.
  7. *
  8. * @param {string} id
  9. * @param {string} name
  10. * @param {string} author
  11. * @param {"Info"|"News"|"Document"|"Background"|"Story"} category Story's category
  12. * @param {string} createdDate
  13. * @param {string} updatedDate
  14. * @param {string} fictionalDate
  15. * @param {{kind:string, name:string}[]} tags
  16. * @param {Chapter[]} chapters
  17. */
  18. constructor(id, name, author, category, createdDate, updatedDate, fictionalDate, tags, chapters) {
  19. this.id = id
  20. this.name = name
  21. this.author = author
  22. this.category = category
  23. this.createdDate = new Date(createdDate)
  24. this.updatedDate = new Date(updatedDate)
  25. this.fictionalDate = new Date(fictionalDate)
  26. this.tags = tags.map(dt => new Tag(dt.kind, dt.name))
  27. this.chapters = chapters != null ? chapters.map(c => new Chapter(c.id, c.title, c.author, c.source, c.createdDate, c.fictionalDate, c.editedDate)) : null
  28. }
  29. }
  30. class StoryCategory {
  31. constructor(name, description) {
  32. this.name = name
  33. this.description = description
  34. }
  35. }
  36. const data = {
  37. categories: null
  38. }
  39. /**
  40. * storyApi contains the API queries
  41. */
  42. const storyApi = {
  43. /**
  44. * Call `stories(filter)` query
  45. *
  46. * @param {{author:string, category:string, tags:Tag[]|Tag, unlisted: boolean, open: boolean, earliestFictionalDate:Date|string, latestFictionalDate:Date|string, limit:number}} filter
  47. * @returns {Promise<Story[]>}
  48. */
  49. list(filter = {}) {
  50. if (filter.earliestFictionalDate != null && typeof(filter.earliestFictionalDate) !== "string") {
  51. filter.earliestFictionalDate = filter.earliestFictionalDate.toISOString()
  52. }
  53. if (filter.latestFictionalDate != null && typeof(filter.latestFictionalDate) !== "string") {
  54. filter.latestFictionalDate = filter.latestFictionalDate.toISOString()
  55. }
  56. return query(`
  57. query ListStories($filter: StoriesFilter) {
  58. stories(filter: $filter) {
  59. id
  60. name
  61. author
  62. category
  63. tags {
  64. kind
  65. name
  66. }
  67. createdDate
  68. fictionalDate
  69. updatedDate
  70. }
  71. }
  72. `, {filter}).then(({stories}) => {
  73. return stories.map(d => new Story(d.id, d.name, d.author, d.category, d.createdDate, d.updatedDate, d.fictionalDate, d.tags))
  74. })
  75. },
  76. /**
  77. * @param {string} id
  78. * @returns {Promise<Story>}
  79. */
  80. find(id) {
  81. return query(`
  82. query FindStory($id: String!) {
  83. story(id: $id) {
  84. id
  85. name
  86. author
  87. category
  88. tags {
  89. kind
  90. name
  91. }
  92. createdDate
  93. fictionalDate
  94. updatedDate
  95. chapters {
  96. id
  97. title
  98. author
  99. source
  100. createdDate
  101. fictionalDate
  102. editedDate
  103. }
  104. }
  105. }
  106. `, {id}).then(({story}) => {
  107. return new Story(
  108. story.id, story.name, story.author, story.category,
  109. story.createdDate, story.updatedDate, story.fictionalDate,
  110. story.tags, story.chapters
  111. )
  112. })
  113. },
  114. /**
  115. * Call `__type(name: "StoryCategory")` query and extracts the catogires from it
  116. *
  117. * @returns {Promise<StoryCategory[]>}
  118. */
  119. categories() {
  120. if (data.categories !== null) {
  121. return data.categories
  122. }
  123. return query(`
  124. query ListStoryCategories {
  125. categoryType: __type(name: "StoryCategory") {
  126. enumValues {
  127. name
  128. description
  129. }
  130. }
  131. }
  132. `, {}).then(({categoryType}) => {
  133. data.categories = categoryType.enumValues.map(d => new StoryCategory(d.name, d.description))
  134. return data.categories
  135. })
  136. },
  137. }
  138. module.exports = {storyApi, Story}