Skip to content

Commit d9373ec

Browse files
committed
🤷 do some stuff
1 parent 0dc59c5 commit d9373ec

File tree

3 files changed

+54
-38
lines changed

3 files changed

+54
-38
lines changed

‎src/components/Comments.svelte

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,41 @@
11
<script lang="ts">
22
import { Label } from "bits-ui";
3-
import { onMount } from "svelte";
43
import "iconify-icon";
4+
import { createEventDispatcher } from "svelte";
55
6-
export let slug: string;
7-
8-
function createComment() {
9-
10-
}
11-
12-
async function loadComments() {
13-
const comments = await fetch(`/api/data.json/?slug=${slug}`, {
14-
method: "GET",
15-
headers: { "Content-Type": "application/json" },
16-
}).then(response => response.json());
6+
const dispatch = createEventDispatcher<{
7+
slug: string;
8+
name: string;
9+
text: string;
10+
}>();
1711
18-
console.log(comments);
19-
}
12+
export let comments: {
13+
id: number;
14+
slug: string | null;
15+
name: string | null;
16+
created_at: Date | null;
17+
text: string | null;
18+
}[] = [];
19+
export let slug: string;
2020
21-
// onMount(loadComments);
21+
let name = "";
22+
let text = "";
2223
</script>
2324

2425
<section class="mb-xs" id="comments">
26+
{#each comments as comment}
27+
<div class="bg-neutral-100 dark:bg-neutral-800 p-xs rounded-lg mb-xs">
28+
<div class="flex justify-between items-center">
29+
<p class="text-sm font-semibold font-jost">{comment.name}</p>
30+
<p class="text-xs text-neutral-500 dark:text-neutral-400">{comment.created_at}</p>
31+
</div>
32+
<p class="text-sm text-neutral dark:text-neutral-300">{comment.text}</p>
33+
</div>
34+
{/each}
35+
2536
<form
2637
class="w-full space-y-xs"
27-
on:submit|preventDefault={createComment}
38+
on:submit|preventDefault={(() => dispatch("createComment", { slug, name, text }))}
2839
>
2940
<div class="space-y-xxs">
3041
<Label.Root for="body" class="uppercase tracking-widest font-semibold text-sm font-jost">
@@ -35,18 +46,20 @@
3546
class="block w-full outline-none p-2 bg-neutral-100 text-neutral"
3647
id="body"
3748
rows={5}
49+
bind:value={text}
3850
required
3951
/>
4052
</div>
4153

4254
<div class="space-y-xxs">
4355
<Label.Root for="author" class="uppercase tracking-widest font-semibold text-sm font-jost">
44-
Your Name*
56+
Your Name*
4557
</Label.Root>
4658
<input
4759
type="text"
4860
class="block w-full outline-none p-2 bg-neutral-100 text-neutral"
4961
id="author"
62+
bind:value={name}
5063
required
5164
/>
5265
</div>

‎src/pages/api/data.json.ts

Lines changed: 0 additions & 19 deletions
This file was deleted.

‎src/pages/posts/[slug].astro

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { formatDate, image } from "$lib/utils";
66
import { documentToHtmlString } from "@contentful/rich-text-html-renderer";
77
import type { InferGetStaticPropsType } from "astro";
88
import { BLOCKS } from "@contentful/rich-text-types";
9+
import { PrismaClient } from "@prisma/client";
910
1011
import PostContent from "$components/PostContent.svelte";
1112
import Separator from "$components/Separator.svelte";
@@ -62,6 +63,16 @@ export async function getStaticPaths() {
6263
6364
const { slug } = Astro.params;
6465
const { content, title, date, cover, destination, podcast } = Astro.props;
66+
67+
const prisma = new PrismaClient();
68+
69+
async function createComment(e: CustomEvent) {
70+
return await prisma.comments.create({ ...e.detail });
71+
}
72+
73+
const comments = await prisma.comments.findMany({
74+
where: { slug }
75+
});
6576
---
6677

6778
<Layout {title} padding={false}>
@@ -78,9 +89,20 @@ const { content, title, date, cover, destination, podcast } = Astro.props;
7889
</PageCover>
7990

8091
<div class="lg:mx-lg xl:mx-xl 2xl:mx-xxl m-xs">
81-
<PostContent client:load {content} {podcast} />
92+
<PostContent
93+
client:load
94+
{content}
95+
{podcast}
96+
/>
97+
8298
<Separator class="w-auto" />
83-
<Comments client:visible {slug} />
99+
100+
<Comments
101+
client:visible
102+
{slug}
103+
{comments}
104+
on:createComment={createComment}
105+
/>
84106
</div>
85107

86108
<PostNavigation {slug} />

0 commit comments

Comments
 (0)