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.

56 lines
1.3 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: moment(new Date()).format("YYYY-MM-DD HH:mm:ss"),
  10. kind: "text",
  11. nick: "",
  12. text: "",
  13. },
  14. }
  15. }
  16. change(key, ev) {
  17. this.state.values[key] = ev.target.value
  18. }
  19. open() {
  20. this.state.values.time = moment(new Date()).format("YYYY-MM-DD HH:mm:ss")
  21. }
  22. close() {
  23. this.emit("close")
  24. }
  25. save() {
  26. if (this.state.loading) {
  27. return
  28. }
  29. const values = this.state.values
  30. let time = new Date(values.time)
  31. if (Number.isNaN(time)) {
  32. this.state.error = `Could not parse ${values.time} as date`
  33. return
  34. }
  35. const input = {logId: this.input.logId, time, kind: this.state.values.kind, nick: this.state.values.nick, text: this.state.values.text}
  36. this.state.loading = true
  37. postApi.add(input).then(data => {
  38. this.emit("added", data)
  39. this.emit("close")
  40. }).catch(errs => {
  41. console.warn("Failed to add post:", errs)
  42. this.state.error = "Failed to add post: " + errs[0].message
  43. }).then(() => {
  44. this.state.loading = false
  45. })
  46. }
  47. }