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.

78 lines
1.9 KiB

  1. const moment = require("moment")
  2. const {chapterApi} = require("../../../../../rpdata/api/Chapter")
  3. module.exports = class {
  4. onCreate(input) {
  5. this.state = {
  6. error: null,
  7. loading: false,
  8. values: {
  9. title: "",
  10. source: "",
  11. fictionalDate: "",
  12. commentMode: "Article",
  13. commentsLocked: false,
  14. },
  15. }
  16. this.first = false
  17. }
  18. onInput(input) {
  19. if (input.chapter && !this.first) {
  20. let {fictionalDate, title, source, commentMode, commentsLocked} = input.chapter
  21. if (fictionalDate != null) {
  22. fictionalDate = moment.utc(fictionalDate).format("MMM D, YYYY")
  23. }
  24. this.state.values = {fictionalDate, title, source, commentMode, commentsLocked}
  25. }
  26. }
  27. change(key, ev) {
  28. this.state.values = Object.assign({}, this.state.values, {[key]: ev.target.value})
  29. }
  30. open() {
  31. }
  32. close() {
  33. this.first = false
  34. this.emit("close")
  35. }
  36. save() {
  37. const values = this.state.values
  38. let fictionalDate = new Date(values.fictionalDate + " UTC")
  39. if (values.fictionalDate != "") {
  40. if (Number.isNaN(fictionalDate)) {
  41. this.state.error = `Could not parse ${values.fictionalDate} as date`
  42. return
  43. }
  44. } else {
  45. fictionalDate = null
  46. }
  47. const input = {id: this.input.chapter.id, title: values.title, source: values.source, commentsLocked: values.commentsLocked, commentMode: values.commentMode}
  48. if (fictionalDate != null) {
  49. input.fictionalDate = fictionalDate
  50. } else {
  51. input.clearFictionalDate = true
  52. }
  53. chapterApi.editChapter(input).then(data => {
  54. this.emit("edit", data)
  55. this.emit("close")
  56. }).catch(errs => {
  57. console.warn("Failed to edit:", errs)
  58. this.state.error = "Failed to edit: " + errs[0].message
  59. }).then(() => {
  60. this.state.loading = false
  61. })
  62. }
  63. }