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.

84 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. } else {
  24. fictionalDate = ""
  25. }
  26. this.state.values = {fictionalDate, title, source, commentMode, commentsLocked}
  27. }
  28. }
  29. change(key, ev) {
  30. this.state.values = Object.assign({}, this.state.values, {[key]: ev.target.value})
  31. }
  32. open() {
  33. }
  34. close() {
  35. this.first = false
  36. this.emit("close")
  37. }
  38. save() {
  39. const values = this.state.values
  40. let fictionalDate = new Date(values.fictionalDate + " UTC")
  41. if (values.fictionalDate != "") {
  42. if (Number.isNaN(fictionalDate.getTime())) {
  43. this.state.error = `Could not parse ${values.fictionalDate} as date.`
  44. if (values.fictionalDate.includes("th")) {
  45. this.state.error += " Try to remove the 'th'."
  46. }
  47. return
  48. }
  49. } else {
  50. fictionalDate = null
  51. }
  52. const input = {id: this.input.chapter.id, title: values.title, source: values.source, commentsLocked: values.commentsLocked, commentMode: values.commentMode}
  53. if (fictionalDate != null) {
  54. input.fictionalDate = fictionalDate
  55. } else {
  56. input.clearFictionalDate = true
  57. }
  58. chapterApi.editChapter(input).then(data => {
  59. this.emit("edit", data)
  60. this.emit("close")
  61. }).catch(errs => {
  62. console.warn("Failed to edit:", errs)
  63. this.state.error = "Failed to edit: " + errs[0].message
  64. }).then(() => {
  65. this.state.loading = false
  66. })
  67. }
  68. }