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.

76 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: "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)) {
  37. this.state.error = `Could not parse ${values.fictionalDate} as date`
  38. return
  39. }
  40. } else {
  41. fictionalDate = null
  42. }
  43. const input = {name: values.name, category: values.category, open: values.open, listed: values.listed, tags: this.state.tags}
  44. if (fictionalDate != null) {
  45. input.fictionalDate = fictionalDate
  46. }
  47. this.state.loading = true
  48. storyApi.add(input).then(data => {
  49. this.emit("add", data)
  50. this.emit("close")
  51. this.state.tags = []
  52. this.state.values = {name: "", fictionalDate: "", category: "", open: "", listed: ""}
  53. window.location = `/story/${data.id}/`
  54. }).catch(errs => {
  55. console.warn("Failed to edit:", errs)
  56. this.state.error = "Failed to edit: " + errs[0].message
  57. this.state.loading = false
  58. })
  59. }
  60. close() {
  61. this.emit("close")
  62. }
  63. }