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.

67 lines
1.4 KiB

  1. const moment = require("moment")
  2. const {postApi} = require("../../../../../rpdata/api/Post")
  3. module.exports = class {
  4. onCreate(input) {
  5. this.state = {
  6. error: null,
  7. loading: false,
  8. values: {
  9. time: "",
  10. kind: "",
  11. nick: "",
  12. text: "",
  13. },
  14. }
  15. this.first = false
  16. }
  17. onInput(input) {
  18. if (input.post) {
  19. const {kind, nick, text} = input.post
  20. const time = moment(input.post.time).format("YYYY-MM-DD HH:mm:ss")
  21. this.state.values = {time, kind, nick, text}
  22. }
  23. }
  24. change(key, ev) {
  25. this.state.values[key] = ev.target.value
  26. }
  27. open() {
  28. }
  29. close() {
  30. this.first = false
  31. this.emit("close")
  32. }
  33. save() {
  34. if (this.state.loading) {
  35. return
  36. }
  37. const values = this.state.values
  38. let time = new Date(values.time)
  39. if (Number.isNaN(time)) {
  40. this.state.error = `Could not parse ${values.time} as date`
  41. return
  42. }
  43. const input = {id: this.input.post.id, time, kind: this.state.values.kind, nick: this.state.values.nick, text: this.state.values.text}
  44. this.state.loading = true
  45. postApi.edit(input).then(data => {
  46. this.emit("edited", data)
  47. this.emit("close")
  48. }).catch(errs => {
  49. console.warn("Failed to edit:", errs)
  50. this.state.error = "Failed to edit: " + errs[0].message
  51. }).then(() => {
  52. this.state.loading = false
  53. })
  54. }
  55. }