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.

52 lines
1.4 KiB

6 years ago
  1. const {query} = require("../client")
  2. class Chapter {
  3. /**
  4. * @param {string} id
  5. * @param {string} title
  6. * @param {string} author
  7. * @param {string} source
  8. * @param {string | Date} createdDate
  9. * @param {string | Date} fictionalDate
  10. * @param {string | Date} editedDate
  11. */
  12. constructor(id, title, author, source, createdDate, fictionalDate, editedDate) {
  13. this.id = id
  14. this.title = title
  15. this.author = author
  16. this.source = source
  17. this.createdDate = new Date(createdDate)
  18. this.fictionalDate = new Date(fictionalDate)
  19. this.editedDate = new Date(editedDate)
  20. }
  21. /**
  22. * Query the chapter's editable fields and update this object. This promise returns a boolean
  23. * whether there has been a change.
  24. *
  25. * @returns {Promise<boolean>} `true` if the story was edited in the meantime
  26. */
  27. update() {
  28. return query(`
  29. query UpdateChapter($id: String!) {
  30. update: chapter(id: $id) {
  31. title
  32. source
  33. fictionalDate
  34. editedDate
  35. }
  36. }
  37. `, {id: this.id}).then(({update}) => {
  38. const editedDate = new Date(udpate.editedDate)
  39. const wasEdited = editedDate.getTime() != this.editedDate.getTime()
  40. this.title = update.title
  41. this.source = update.source
  42. this.fictionalDate = new Date(update.fictionalDate)
  43. this.editedDate = editedDate
  44. return wasEdited
  45. })
  46. }
  47. }
  48. module.exports = {Chapter}