forked from ProjectSegfault/website
v3
This commit is contained in:
@@ -1,8 +0,0 @@
|
||||
import type { LayoutServerLoad } from "./$types"
|
||||
import { redirect } from "@sveltejs/kit";
|
||||
|
||||
export const load: LayoutServerLoad = async ({ locals }) => {
|
||||
if (!await locals.getSession()) {
|
||||
throw redirect(302, "/login");
|
||||
}
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
<slot />
|
||||
|
||||
<style>
|
||||
:global(.col) {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
width: fit-content;
|
||||
gap: 10px;
|
||||
}
|
||||
</style>
|
||||
@@ -1,5 +1,8 @@
|
||||
<h1>Admin dashboard</h1>
|
||||
<script lang="ts">
|
||||
import type { PageData } from "./$types";
|
||||
|
||||
<div class="col">
|
||||
<a href="/admin/announcements">Announcements</a>
|
||||
</div>
|
||||
export let data: PageData;
|
||||
</script>
|
||||
|
||||
<h1>{data.title}</h1>
|
||||
<p>Nothing here yet.</p>
|
||||
7
src/routes/admin/+page.ts
Normal file
7
src/routes/admin/+page.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
import type { PageLoad } from "./$types";
|
||||
|
||||
export const load = (() => {
|
||||
return {
|
||||
title: "Admin dashboard"
|
||||
};
|
||||
}) satisfies PageLoad;
|
||||
@@ -1,51 +0,0 @@
|
||||
import type { Actions } from "./$types";
|
||||
import Joi from "joi";
|
||||
import { fail } from "@sveltejs/kit";
|
||||
import { db } from "$lib/server/db";
|
||||
|
||||
export const actions: Actions = {
|
||||
add: async ({ request, locals }) => {
|
||||
if (!await locals.getSession()) {
|
||||
return fail(401, { addError: true, addMessage: "You must be logged in to post an announcement." });
|
||||
} else {
|
||||
const formData = await request.formData();
|
||||
|
||||
const BodyTypeSchema = Joi.object({
|
||||
title: Joi.string().required(),
|
||||
severity: Joi.string().required(),
|
||||
author: Joi.string().required(),
|
||||
link: Joi.string().optional().allow("")
|
||||
});
|
||||
|
||||
if (BodyTypeSchema.validate(Object.fromEntries(formData.entries())).error) {
|
||||
return fail(400, { addError: true, addMessage: String(BodyTypeSchema.validate(Object.fromEntries(formData.entries())).error) });
|
||||
} else {
|
||||
const now = Math.floor(Date.now() / 1000);
|
||||
const data = {
|
||||
...Object.fromEntries(formData.entries()),
|
||||
created: now
|
||||
};
|
||||
|
||||
const collection = db.collection("announcements");
|
||||
|
||||
await collection.deleteMany({});
|
||||
|
||||
await collection.insertOne(data);
|
||||
|
||||
return { addSuccess: true, addMessage: "Your announcement has been posted." };
|
||||
}
|
||||
}
|
||||
|
||||
},
|
||||
delete: async ({ locals }) => {
|
||||
if (!await locals.getSession()) {
|
||||
return fail(401, { deleteError: true, deleteMessage: "You must be logged in to delete an announcement." });
|
||||
} else {
|
||||
const collection = db.collection("announcements");
|
||||
|
||||
await collection.deleteMany({});
|
||||
|
||||
return { deleteSuccess: true, deleteMessage: "Your announcement has been deleted." };
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,55 +0,0 @@
|
||||
<script lang="ts">
|
||||
import type { ActionData } from '.$/types';
|
||||
|
||||
export let form: ActionData;
|
||||
</script>
|
||||
|
||||
<div class="col">
|
||||
<h1>Post Announcement</h1>
|
||||
<form
|
||||
action="?/add"
|
||||
method="POST"
|
||||
class="col"
|
||||
>
|
||||
<select id="severity" name="severity" required>
|
||||
<option value="" selected disabled>
|
||||
Select severity of announcement
|
||||
</option>
|
||||
<option value="info">Information announcement</option>
|
||||
<option value="low">Low severity</option>
|
||||
<option value="medium">Medium severity</option>
|
||||
<option value="high">High severity</option>
|
||||
</select>
|
||||
<textarea
|
||||
name="title"
|
||||
rows="4"
|
||||
cols="25"
|
||||
placeholder="The announcement text"
|
||||
></textarea>
|
||||
<input
|
||||
type="text"
|
||||
name="link"
|
||||
placeholder="Your link for more details"
|
||||
/>
|
||||
<input type="text" name="author" placeholder="Your name" />
|
||||
{#if form?.addSuccess}
|
||||
{form.addMessage}
|
||||
{/if}
|
||||
|
||||
{#if form?.addError}
|
||||
{form.addMessage}
|
||||
{/if}
|
||||
<button type="submit">Submit</button>
|
||||
</form>
|
||||
<h1 style="margin-top: 20px">Delete Announcement</h1>
|
||||
<form
|
||||
action="?/delete"
|
||||
method="POST"
|
||||
class="col"
|
||||
>
|
||||
{#if form?.deleteSuccess}
|
||||
{form.deleteMessage}
|
||||
{/if}
|
||||
<button type="submit">Delete</button>
|
||||
</form>
|
||||
</div>
|
||||
Reference in New Issue
Block a user