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.8 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)) {
  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.story.id, name: values.name, category: values.category, open: values.open, listed: values.listed}
  48. if (fictionalDate != null) {
  49. input.fictionalDate = fictionalDate
  50. } else {
  51. input.clearFictionalDate = true
  52. }
  53. this.state.loading = true
  54. storyApi.edit(input).then(data => {
  55. this.emit("edit", data)
  56. this.emit("close")
  57. }).catch(errs => {
  58. console.warn("Failed to edit:", errs)
  59. this.state.error = "Failed to edit: " + errs[0].message
  60. }).then(() => {
  61. this.state.loading = false
  62. })
  63. }
  64. close() {
  65. this.emit("close")
  66. }
  67. }