From c4636cf8432bf37c7cbec30a75973b26227e9236 Mon Sep 17 00:00:00 2001 From: Gisle Aune Date: Sun, 2 Dec 2018 14:18:56 +0100 Subject: [PATCH] logs: Added log import form. --- .../components/import-log-modal/component.js | 83 +++++++++++++++++++ .../components/import-log-modal/index.marko | 44 ++++++++++ .../components/import-log-modal/style.less | 9 ++ .../logs/components/logs-menu/index.marko | 1 + marko/page/logs/components/page/index.marko | 1 + package-lock.json | 22 ++--- rpdata/api/Log.js | 47 +++++++++++ 7 files changed, 196 insertions(+), 11 deletions(-) create mode 100644 marko/page/logs/components/import-log-modal/component.js create mode 100644 marko/page/logs/components/import-log-modal/index.marko create mode 100644 marko/page/logs/components/import-log-modal/style.less diff --git a/marko/page/logs/components/import-log-modal/component.js b/marko/page/logs/components/import-log-modal/component.js new file mode 100644 index 0000000..ffabb81 --- /dev/null +++ b/marko/page/logs/components/import-log-modal/component.js @@ -0,0 +1,83 @@ +const {tz} = require("moment-timezone") + +const {logsApi} = require("../../../../../rpdata/api/Log") + +module.exports = class { + onCreate(input) { + this.state = { + error: null, + loading: false, + imported: null, + values: { + importer: "MircLike", + timezone: tz.guess() || "UTC", + channelName: "", + date: "", + data: "", + }, + timezones: tz.names(), + } + + this.first = false + } + + change(key, ev) { + this.state.values[key] = ev.target.value + this.state.values = Object.assign({}, this.state.values) + } + + open() { + this.state.loading = false + } + + close() { + this.emit("close") + + this.state.values = { + importer: "MircLike", + timezone: tz.guess() || "UTC", + channelName: "", + date: "", + data: "", + } + this.state.imported = null + this.state.loading = false + } + + save() { + if (this.state.loading) { + return + } + this.state.error = null; + + const input = Object.assign({}, this.state.values) + if (input.date != "" && input.importer !== "ForumLog") { + input.date = new Date(input.date) + if (Number.isNaN(input.date)) { + this.state.error = "Invalid date" + return + } + } else if (input.importer === "MircLike") { + this.state.error = "Date is required for mIRC-like logs" + return + } else { + input.date = null; + } + + if (input.channelName.length < 2 || input.channelName.includes(" ") || input.channelName.includes(" ") || input.channelName.charAt(0) !== "#") { + this.state.error = "A valid channelName name is required" + return + } + + this.state.loading = true + logsApi.import(input).then(logs => { + this.state.imported = logs + }).catch(errs => { + console.warn("Import failed:", errs) + + this.state.error = "Import failed: " + errs[0].message + }).then(() => { + this.state.loading = false + }) + } +} \ No newline at end of file diff --git a/marko/page/logs/components/import-log-modal/index.marko b/marko/page/logs/components/import-log-modal/index.marko new file mode 100644 index 0000000..4a7005b --- /dev/null +++ b/marko/page/logs/components/import-log-modal/index.marko @@ -0,0 +1,44 @@ +import moment from "moment" + + + +

Import Log

+ +

${state.error}

+ + + + + + + + + + + + + + + + +