From 27bfae67aff341dabe7dd290ceaf9114355c412e Mon Sep 17 00:00:00 2001 From: Louis Lam Date: Thu, 17 Mar 2022 23:38:43 +0800 Subject: [PATCH] [Status Page] Add a new status page --- .../status-page-socket-handler.js | 50 +++++++++++++- src/mixins/socket.js | 8 +++ src/pages/AddStatusPage.vue | 65 ++++++++++++++----- 3 files changed, 104 insertions(+), 19 deletions(-) diff --git a/server/socket-handlers/status-page-socket-handler.js b/server/socket-handlers/status-page-socket-handler.js index c1d667978..7fe175604 100644 --- a/server/socket-handlers/status-page-socket-handler.js +++ b/server/socket-handlers/status-page-socket-handler.js @@ -185,7 +185,7 @@ module.exports.statusPageSocketHandler = (socket) => { }); } catch (error) { - console.log(error); + console.error(error); callback({ ok: false, @@ -194,4 +194,52 @@ module.exports.statusPageSocketHandler = (socket) => { } }); + // Add a new status page + socket.on("addStatusPage", async (title, slug, callback) => { + try { + checkLogin(socket); + + title = title?.trim(); + slug = slug?.trim(); + + // Check empty + if (!title || !slug) { + throw new Error("Please input all fields"); + } + + // Make sure slug is string + if (typeof slug !== "string") { + throw new Error("Slug -Accept string only"); + } + + // lower case only + slug = slug.toLowerCase(); + + // Check slug a-z, 0-9, - only + // Regex from: https://stackoverflow.com/questions/22454258/js-regex-string-validation-for-slug + if (!slug.match(/^[A-Za-z0-9]+(?:-[A-Za-z0-9]+)*$/)) { + throw new Error("Invalid Slug"); + } + + let statusPage = R.dispense("status_page"); + statusPage.slug = slug; + statusPage.title = title; + statusPage.theme = "light"; + statusPage.icon = ""; + await R.store(statusPage); + + callback({ + ok: true, + msg: "OK!" + }); + + } catch (error) { + console.error(error); + callback({ + ok: false, + msg: error.message, + }); + } + }); + }; diff --git a/src/mixins/socket.js b/src/mixins/socket.js index e092923ed..902bde31c 100644 --- a/src/mixins/socket.js +++ b/src/mixins/socket.js @@ -255,6 +255,14 @@ export default { } }, + toastSuccess(msg) { + toast.success(msg); + }, + + toastError(msg) { + toast.error(msg); + }, + login(username, password, token, callback) { socket.emit("login", { username, diff --git a/src/pages/AddStatusPage.vue b/src/pages/AddStatusPage.vue index f14ebfa6c..dd1099916 100644 --- a/src/pages/AddStatusPage.vue +++ b/src/pages/AddStatusPage.vue @@ -5,24 +5,33 @@ {{ $t("Add New Status Page") }} -
-
- - -
+
+
+
+ + +
-
- -
- /status/ - +
+ +
+ /status/ + +
+
+
    +
  • {{ $t("Accept characters:") }} a-z 0-9 -
  • +
  • {{ $t("Start or end with") }} a-z 0-9 only
  • +
  • {{ $t("No consecutive dashes") }} --
  • +
+
+
+ +
+
- -
- -
-
+
@@ -34,11 +43,31 @@ export default { }, data() { return { - statusPage: { - - }, + title: "", + slug: "", processing: false, }; + }, + methods: { + async submit() { + this.processing = true; + + this.$root.getSocket().emit("addStatusPage", this.title, this.slug, (res) => { + this.processing = false; + + if (res.ok) { + this.$router.push("/status/" + this.slug + "?edit"); + } else { + + if (res.msg.includes("UNIQUE constraint")) { + this.$root.toastError(this.$t("The slug is already taken. Please choose another slug.")); + } else { + this.$root.toastRes(res); + } + + } + }); + } } };