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.

82 lines
2.0 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.getTime())) {
  41. this.state.error = `Could not parse ${values.fictionalDate} as date.`
  42. if (values.fictionalDate.includes("th")) {
  43. this.state.error += " Try to remove the 'th'."
  44. }
  45. return
  46. }
  47. } else {
  48. fictionalDate = null
  49. }
  50. const input = {id: this.input.chapter.id, title: values.title, source: values.source, commentsLocked: values.commentsLocked, commentMode: values.commentMode}
  51. if (fictionalDate != null) {
  52. input.fictionalDate = fictionalDate
  53. } else {
  54. input.clearFictionalDate = true
  55. }
  56. chapterApi.editChapter(input).then(data => {
  57. this.emit("edit", data)
  58. this.emit("close")
  59. }).catch(errs => {
  60. console.warn("Failed to edit:", errs)
  61. this.state.error = "Failed to edit: " + errs[0].message
  62. }).then(() => {
  63. this.state.loading = false
  64. })
  65. }
  66. }