[Status Page] Add a new status page

This commit is contained in:
Louis Lam 2022-03-17 23:38:43 +08:00
parent 719a136d1e
commit 27bfae67af
3 changed files with 104 additions and 19 deletions

View file

@ -185,7 +185,7 @@ module.exports.statusPageSocketHandler = (socket) => {
}); });
} catch (error) { } catch (error) {
console.log(error); console.error(error);
callback({ callback({
ok: false, 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,
});
}
});
}; };

View file

@ -255,6 +255,14 @@ export default {
} }
}, },
toastSuccess(msg) {
toast.success(msg);
},
toastError(msg) {
toast.error(msg);
},
login(username, password, token, callback) { login(username, password, token, callback) {
socket.emit("login", { socket.emit("login", {
username, username,

View file

@ -5,17 +5,25 @@
{{ $t("Add New Status Page") }} {{ $t("Add New Status Page") }}
</h1> </h1>
<form @submit.prevent="submit">
<div class="shadow-box"> <div class="shadow-box">
<div class="mb-3"> <div class="mb-3">
<label for="name" class="form-label">{{ $t("Name") }}</label> <label for="name" class="form-label">{{ $t("Name") }}</label>
<input id="name" v-model="statusPage.title" type="text" class="form-control"> <input id="name" v-model="title" type="text" class="form-control" required>
</div> </div>
<div class="mb-4"> <div class="mb-4">
<label for="slug" class="form-label">{{ $t("Slug") }}</label> <label for="slug" class="form-label">{{ $t("Slug") }}</label>
<div class="input-group"> <div class="input-group">
<span id="basic-addon3" class="input-group-text">/status/</span> <span id="basic-addon3" class="input-group-text">/status/</span>
<input id="slug" v-model="statusPage.slug" type="text" class="form-control"> <input id="slug" v-model="slug" type="text" class="form-control" required>
</div>
<div class="form-text">
<ul>
<li>{{ $t("Accept characters:") }} <mark>a-z</mark> <mark>0-9</mark> <mark>-</mark></li>
<li>{{ $t("Start or end with") }} <mark>a-z</mark> <mark>0-9</mark> only</li>
<li>{{ $t("No consecutive dashes") }} <mark>--</mark></li>
</ul>
</div> </div>
</div> </div>
@ -23,6 +31,7 @@
<button id="monitor-submit-btn" class="btn btn-primary w-100" type="submit" :disabled="processing">{{ $t("Next") }}</button> <button id="monitor-submit-btn" class="btn btn-primary w-100" type="submit" :disabled="processing">{{ $t("Next") }}</button>
</div> </div>
</div> </div>
</form>
</div> </div>
</transition> </transition>
</template> </template>
@ -34,11 +43,31 @@ export default {
}, },
data() { data() {
return { return {
statusPage: { title: "",
slug: "",
},
processing: false, 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);
}
}
});
}
} }
}; };
</script> </script>