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.6 KiB

  1. const moment = require("moment")
  2. const {charactersApi} = require("../../../../../rpdata/api/Character")
  3. module.exports = class {
  4. onCreate(input) {
  5. this.state = {
  6. error: null,
  7. loading: false,
  8. values: {
  9. name: "",
  10. nick: "",
  11. shortName: "",
  12. author: "",
  13. description: "",
  14. }
  15. }
  16. }
  17. onInput(input) {
  18. this.state.values = Object.assign({}, this.state.values, {author: input.user.name})
  19. }
  20. change(key, ev) {
  21. this.state.values[key] = ev.target.value
  22. }
  23. open() {
  24. }
  25. close() {
  26. this.emit("close")
  27. }
  28. selectUnknown(nick) {
  29. this.state.values = Object.assign({}, this.state.values, {
  30. nick: nick,
  31. name: nick.replace(/_/g, " ").replace(/`/g, "'"),
  32. shortName: (!nick.includes("_") && nick.includes("`")) ? nick.split("`")[0] : "",
  33. })
  34. }
  35. save() {
  36. if (this.state.loading) {
  37. return
  38. }
  39. const values = Object.assign({}, this.state.values)
  40. if (values.name.length < 2) {
  41. this.state.error = "Name is too short (min: 2)"
  42. return
  43. }
  44. if (values.author == "") {
  45. delete values.author
  46. }
  47. this.state.loading = true
  48. charactersApi.add(this.state.values).then((res) => {
  49. this.emit("added", res)
  50. this.emit("close")
  51. this.state.values = {
  52. name: "",
  53. nick: "",
  54. shortName: "",
  55. author: this.input.user.name,
  56. description: "",
  57. }
  58. }).catch(errs => {
  59. console.warn("Failed to edit:", errs)
  60. this.state.error = "Failed to edit: " + errs[0].message
  61. }).then(() => {
  62. this.state.loading = false
  63. })
  64. }
  65. }