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.

86 lines
2.1 KiB

  1. const moment = require("moment")
  2. const {storyApi} = require("../../../../../rpdata/api/Story")
  3. const ORDINAL_REGEX = /[0-9]{1,2}(st|nd|rd|th)/g
  4. module.exports = class {
  5. onCreate() {
  6. this.state = {
  7. error: null,
  8. values: {
  9. name: "",
  10. fictionalDate: "",
  11. category: "",
  12. open: "",
  13. listed: "",
  14. },
  15. loading: false,
  16. }
  17. this.filled = false
  18. }
  19. onInput(input) {
  20. if (!this.filled) {
  21. this.state.values = {
  22. name: input.story.name,
  23. category: input.story.category,
  24. open: input.story.open,
  25. listed: input.story.listed,
  26. }
  27. if (input.story.fictionalDate != null) {
  28. this.state.values.fictionalDate = moment.utc(input.story.fictionalDate).format("MMM D, YYYY")
  29. }
  30. this.filled = true
  31. }
  32. }
  33. change(key, ev) {
  34. this.state.values[key] = ev.target.value
  35. this.state.values = Object.assign({}, this.state.values)
  36. }
  37. save() {
  38. const values = this.state.values
  39. let fictionalDate = new Date(values.fictionalDate + " UTC")
  40. if (values.fictionalDate) {
  41. if (Number.isNaN(fictionalDate.getTime())) {
  42. this.state.error = `Could not parse ${values.fictionalDate} as date.`
  43. const ordinal = values.fictionalDate.match(ORDINAL_REGEX)[0]
  44. if (ordinal != null) {
  45. this.state.error += " Try to remove the '"+ordinal.slice(-2)+"'."
  46. }
  47. return
  48. }
  49. } else {
  50. fictionalDate = null
  51. }
  52. const input = {id: this.input.story.id, name: values.name, category: values.category, open: values.open, listed: values.listed}
  53. if (fictionalDate != null) {
  54. input.fictionalDate = fictionalDate
  55. } else {
  56. input.clearFictionalDate = true
  57. }
  58. this.state.loading = true
  59. storyApi.edit(input).then(data => {
  60. this.emit("edit", data)
  61. this.emit("close")
  62. }).catch(errs => {
  63. console.warn("Failed to edit:", errs)
  64. this.state.error = "Failed to edit: " + errs[0].message
  65. }).then(() => {
  66. this.state.loading = false
  67. })
  68. }
  69. close() {
  70. this.emit("close")
  71. }
  72. }