const moment = require("moment") const {charactersApi} = require("../../../../../rpdata/api/Character") module.exports = class { onCreate(input) { this.state = { error: null, loading: false, values: { name: "", nick: "", shortName: "", author: "", description: "", } } } onInput(input) { this.state.values = Object.assign({}, this.state.values, {author: input.user.name}) } change(key, ev) { this.state.values[key] = ev.target.value } open() { } close() { this.emit("close") } selectUnknown(nick) { this.state.values = Object.assign({}, this.state.values, { nick: nick, name: nick.replace(/_/g, " ").replace(/`/g, "'"), shortName: (!nick.includes("_") && nick.includes("`")) ? nick.split("`")[0] : "", }) } save() { if (this.state.loading) { return } const values = Object.assign({}, this.state.values) if (values.name.length < 2) { this.state.error = "Name is too short (min: 2)" return } if (values.author == "") { delete values.author } this.state.loading = true charactersApi.add(this.state.values).then((res) => { this.emit("added", res) this.emit("close") this.state.values = { name: "", nick: "", shortName: "", author: this.input.user.name, description: "", } }).catch(errs => { console.warn("Failed to edit:", errs) this.state.error = "Failed to edit: " + errs[0].message }).then(() => { this.state.loading = false }) } }