update homepage draft and other pages
This commit is contained in:
26
src/routes/blog/+page.server.ts
Normal file
26
src/routes/blog/+page.server.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
import { db } from "$lib/db"
|
||||
import { blogArticles } from "$lib/db/schema";
|
||||
import { redirect } from "@sveltejs/kit";
|
||||
import type { PageServerLoad } from "./$types.js";
|
||||
|
||||
export const load: PageServerLoad = async () => {
|
||||
const articles = await db.select().from(blogArticles)
|
||||
|
||||
return {
|
||||
blogArticles: articles
|
||||
}
|
||||
}
|
||||
|
||||
export const actions = {
|
||||
default: async ({ request }): Promise<Response> => {
|
||||
const formData = await request.formData();
|
||||
|
||||
const blogArticle = await db.insert(blogArticles).values({
|
||||
title: formData.get('title')?.toString(),
|
||||
slug: formData.get('slug')?.toString(),
|
||||
content: formData.get('content')?.toString()
|
||||
}).returning();
|
||||
|
||||
return redirect(303, `/blog/${blogArticle[0].slug}`);
|
||||
}
|
||||
}
|
||||
28
src/routes/blog/+page.svelte
Normal file
28
src/routes/blog/+page.svelte
Normal file
@@ -0,0 +1,28 @@
|
||||
<script lang="ts">
|
||||
import { enhance } from '$app/forms';
|
||||
|
||||
let { data } = $props();
|
||||
|
||||
let createNewArticlePopup = $state(false);
|
||||
</script>
|
||||
|
||||
<div class="text-center py-8 gap-2 flex align-center justify-center">
|
||||
<h1 class="text-2xl">Blog (Stan's yapping corner)</h1>
|
||||
<button onclick={() => createNewArticlePopup = true} class="bg-gray-800 rounded px-4 py-2 text-white font-bold">Write new article</button>
|
||||
</div>
|
||||
|
||||
|
||||
{#each data.blogArticles as article}
|
||||
<div class="flex">
|
||||
<a href="/blog/${article.slug}">{article.title}</a>
|
||||
<p>{article.createdAt?.toDateString()}</p>
|
||||
</div>
|
||||
{/each}
|
||||
|
||||
{#if createNewArticlePopup}
|
||||
<form method="POST" use:enhance>
|
||||
<div>
|
||||
<h2>Create New Article</h2>
|
||||
</div>
|
||||
</form>
|
||||
{/if}
|
||||
22
src/routes/blog/{slug}/+page.server.ts
Normal file
22
src/routes/blog/{slug}/+page.server.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
import { db } from "$lib/db"
|
||||
import { blogArticles } from "$lib/db/schema";
|
||||
import { eq } from "drizzle-orm";
|
||||
|
||||
import type { PageServerLoad } from "./$types.js";
|
||||
import { fail } from "@sveltejs/kit";
|
||||
|
||||
export const load: PageServerLoad = async ({params }) => {
|
||||
const article = await db.select().from(blogArticles).where(eq(blogArticles.slug, params.slug))
|
||||
|
||||
if (article.length === 0) {
|
||||
return fail(404, { message: "Article not found" });
|
||||
}
|
||||
|
||||
return {
|
||||
blogArticle: article[0]
|
||||
}
|
||||
}
|
||||
|
||||
export const actions = {
|
||||
|
||||
}
|
||||
2
src/routes/blog/{slug}/+page.svelte
Normal file
2
src/routes/blog/{slug}/+page.svelte
Normal file
@@ -0,0 +1,2 @@
|
||||
|
||||
l
|
||||
Reference in New Issue
Block a user