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.

80 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: "Info",
  11. open: false,
  12. listed: false,
  13. },
  14. tags: [],
  15. loading: false,
  16. }
  17. this.filled = false
  18. }
  19. addTag(tag) {
  20. if (tag.name == "" || tag.kind == "") {
  21. return
  22. }
  23. this.state.tags = this.state.tags.filter(t => t.kind !== tag.kind || t.name !== tag.name).concat([tag])
  24. }
  25. removeTag(tag) {
  26. this.state.tags = this.state.tags.filter(t => t.kind !== tag.kind || t.name !== tag.name)
  27. }
  28. change(key, ev) {
  29. this.state.values[key] = ev.target.value
  30. this.state.values = Object.assign({}, this.state.values)
  31. }
  32. save() {
  33. const values = this.state.values
  34. let fictionalDate = new Date(values.fictionalDate + " UTC")
  35. if (values.fictionalDate != "") {
  36. if (Number.isNaN(fictionalDate.getTime())) {
  37. this.state.error = `Could not parse ${values.fictionalDate} as date.`
  38. if (values.fictionalDate.includes("th")) {
  39. this.state.error += " Try to remove the 'th'."
  40. }
  41. return
  42. }
  43. } else {
  44. fictionalDate = null
  45. }
  46. const input = {name: values.name, category: values.category, open: values.open, listed: values.listed, tags: this.state.tags}
  47. if (fictionalDate != null) {
  48. input.fictionalDate = fictionalDate
  49. }
  50. this.state.loading = true
  51. storyApi.add(input).then(data => {
  52. this.emit("add", data)
  53. this.emit("close")
  54. this.state.tags = []
  55. this.state.values = {name: "", fictionalDate: "", category: "", open: "", listed: ""}
  56. window.location = `/story/${data.id}/`
  57. }).catch(errs => {
  58. console.warn("Failed to edit:", errs)
  59. this.state.error = "Failed to edit: " + errs[0].message
  60. this.state.loading = false
  61. })
  62. }
  63. close() {
  64. this.emit("close")
  65. }
  66. }