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 {storyApi} = require("../../../../../rpdata/api/Story")
  3. module.exports = class {
  4. onCreate() {
  5. this.state = {
  6. error: null,
  7. values: {
  8. name: "",
  9. fictionalDate: "",
  10. category: "",
  11. open: "",
  12. listed: "",
  13. },
  14. loading: false,
  15. }
  16. this.filled = false
  17. }
  18. onInput(input) {
  19. if (!this.filled) {
  20. this.state.values = {
  21. name: input.story.name,
  22. category: input.story.category,
  23. open: input.story.open,
  24. listed: input.story.listed,
  25. }
  26. if (input.story.fictionalDate != null) {
  27. this.state.values.fictionalDate = moment.utc(input.story.fictionalDate).format("MMM D, YYYY")
  28. }
  29. this.filled = true
  30. }
  31. }
  32. change(key, ev) {
  33. this.state.values[key] = ev.target.value
  34. this.state.values = Object.assign({}, this.state.values)
  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.story.id, name: values.name, category: values.category, open: values.open, listed: values.listed}
  51. if (fictionalDate != null) {
  52. input.fictionalDate = fictionalDate
  53. } else {
  54. input.clearFictionalDate = true
  55. }
  56. this.state.loading = true
  57. storyApi.edit(input).then(data => {
  58. this.emit("edit", data)
  59. this.emit("close")
  60. }).catch(errs => {
  61. console.warn("Failed to edit:", errs)
  62. this.state.error = "Failed to edit: " + errs[0].message
  63. }).then(() => {
  64. this.state.loading = false
  65. })
  66. }
  67. close() {
  68. this.emit("close")
  69. }
  70. }