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.

95 lines
2.3 KiB

  1. const {tz} = require("moment-timezone")
  2. const {logsApi} = require("../../../../../rpdata/api/Log")
  3. module.exports = class {
  4. onCreate(input) {
  5. this.state = {
  6. error: null,
  7. loading: false,
  8. imported: null,
  9. values: {
  10. importer: "MircLike",
  11. timezone: tz.guess() || "UTC",
  12. channelName: "",
  13. date: "",
  14. data: "",
  15. sessionThresholdHours: 12,
  16. },
  17. timezones: tz.names(),
  18. }
  19. this.first = false
  20. }
  21. change(key, ev) {
  22. this.state.values[key] = ev.target.value
  23. this.state.values = Object.assign({}, this.state.values)
  24. }
  25. open() {
  26. this.state.loading = false
  27. }
  28. close() {
  29. this.emit("close")
  30. this.state.values = {
  31. importer: "MircLike",
  32. timezone: tz.guess() || "UTC",
  33. channelName: "",
  34. date: "",
  35. data: "",
  36. }
  37. this.state.imported = null
  38. this.state.loading = false
  39. }
  40. save() {
  41. if (this.state.loading) {
  42. return
  43. }
  44. this.state.error = null;
  45. const input = Object.assign({}, this.state.values)
  46. if (input.date != "" && input.importer !== "ForumLog") {
  47. input.date = new Date(input.date)
  48. if (Number.isNaN(input.date)) {
  49. this.state.error = "Invalid date"
  50. return
  51. }
  52. } else if (input.importer === "MircLike") {
  53. this.state.error = "Date is required for mIRC-like logs."
  54. return
  55. } else {
  56. input.date = null;
  57. }
  58. if (input.importer === "IRCCloudLog") {
  59. const hours = parseFloat(input.sessionThresholdHours)
  60. if (Number.isNaN(hours)) {
  61. this.state.error = "The session change threshold must be a number."
  62. return
  63. }
  64. input.sessionThresholdMs = Math.floor(hours * 3600000)
  65. }
  66. delete input.sessionThresholdHours
  67. if (input.channelName.length < 2 || input.channelName.includes(" ") || input.channelName.includes(" ") || input.channelName.charAt(0) !== "#") {
  68. this.state.error = "A valid channelName name is required"
  69. return
  70. }
  71. this.state.loading = true
  72. logsApi.import(input).then(logs => {
  73. this.state.imported = logs
  74. }).catch(errs => {
  75. console.warn("Import failed:", errs)
  76. this.state.error = "Import failed: " + errs[0].message
  77. }).then(() => {
  78. this.state.loading = false
  79. })
  80. }
  81. }