diff --git a/marko/page/logs-content/components/add-post-modal/component.js b/marko/page/logs-content/components/add-post-modal/component.js new file mode 100644 index 0000000..f8fccd7 --- /dev/null +++ b/marko/page/logs-content/components/add-post-modal/component.js @@ -0,0 +1,59 @@ +const moment = require("moment") + +const {postApi} = require("../../../../../rpdata/api/Post") + +module.exports = class { + onCreate(input) { + this.state = { + error: null, + loading: false, + values: { + time: moment.utc(new Date()).format("YYYY-MM-DD HH:mm:ss"), + kind: "text", + nick: "", + text: "", + }, + } + + this.first = false + } + + change(key, ev) { + this.state.values[key] = ev.target.value + } + + open() { + } + + close() { + this.first = false + this.emit("close") + } + + save() { + if (this.state.loading) { + return + } + + const values = this.state.values + + let time = new Date(values.time + " UTC") + if (Number.isNaN(time)) { + this.state.error = `Could not parse ${values.time} as date` + return + } + + const input = {logId: this.input.logId, time, kind: this.state.values.kind, nick: this.state.values.nick, text: this.state.values.text} + + this.state.loading = true + postApi.add(input).then(data => { + this.emit("added", data) + this.emit("close") + }).catch(errs => { + console.warn("Failed to add post:", errs) + this.state.error = "Failed to add post: " + errs[0].message + }).then(() => { + this.state.loading = false + }) + } +} \ No newline at end of file diff --git a/marko/page/logs-content/components/add-post-modal/index.marko b/marko/page/logs-content/components/add-post-modal/index.marko new file mode 100644 index 0000000..4a6d6c7 --- /dev/null +++ b/marko/page/logs-content/components/add-post-modal/index.marko @@ -0,0 +1,26 @@ + +

Add Post

+ +

${state.error}

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