|
1 | 1 | <script lang="ts">
|
2 | 2 | import { Label } from "bits-ui";
|
3 |
| - import { onMount } from "svelte"; |
4 | 3 | import "iconify-icon";
|
| 4 | + import { createEventDispatcher } from "svelte"; |
5 | 5 |
|
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 | + }>(); |
17 | 11 |
|
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; |
20 | 20 |
|
21 |
| - // onMount(loadComments); |
| 21 | + let name = ""; |
| 22 | + let text = ""; |
22 | 23 | </script>
|
23 | 24 |
|
24 | 25 | <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 | + |
25 | 36 | <form
|
26 | 37 | class="w-full space-y-xs"
|
27 |
| - on:submit|preventDefault={createComment} |
| 38 | + on:submit|preventDefault={(() => dispatch("createComment", { slug, name, text }))} |
28 | 39 | >
|
29 | 40 | <div class="space-y-xxs">
|
30 | 41 | <Label.Root for="body" class="uppercase tracking-widest font-semibold text-sm font-jost">
|
|
35 | 46 | class="block w-full outline-none p-2 bg-neutral-100 text-neutral"
|
36 | 47 | id="body"
|
37 | 48 | rows={5}
|
| 49 | + bind:value={text} |
38 | 50 | required
|
39 | 51 | />
|
40 | 52 | </div>
|
41 | 53 |
|
42 | 54 | <div class="space-y-xxs">
|
43 | 55 | <Label.Root for="author" class="uppercase tracking-widest font-semibold text-sm font-jost">
|
44 |
| - Your Name* |
| 56 | + Your Name* |
45 | 57 | </Label.Root>
|
46 | 58 | <input
|
47 | 59 | type="text"
|
48 | 60 | class="block w-full outline-none p-2 bg-neutral-100 text-neutral"
|
49 | 61 | id="author"
|
| 62 | + bind:value={name} |
50 | 63 | required
|
51 | 64 | />
|
52 | 65 | </div>
|
|
0 commit comments