Skip to content

Commit b7ef304

Browse files
committed
fetch vs actions video final commit
1 parent 8cc23f7 commit b7ef304

File tree

8 files changed

+77
-56
lines changed

8 files changed

+77
-56
lines changed

local.db-shm

0 Bytes
Binary file not shown.

local.db-wal

88.5 KB
Binary file not shown.

src/lib/components/Todo.svelte

+23-12
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,41 @@
11
<script lang="ts">
22
import { invalidate } from '$app/navigation';
33
import { todosTable } from '$lib/db/schema';
4+
import type { deleteSchema, toggleSchema } from '$lib/zod';
5+
import { superForm, type Infer, type SuperValidated } from 'sveltekit-superforms';
6+
47
export let todo: typeof todosTable.$inferSelect;
8+
export let toggleFormData: SuperValidated<Infer<typeof toggleSchema>>;
9+
export let deleteFormData: SuperValidated<Infer<typeof deleteSchema>>;
510
6-
const deleteTodo = async () => {
7-
await fetch(`/api/delete?id=${todo.id}`);
8-
invalidate('query:todos');
9-
};
11+
const { enhance: toggleEnhance } = superForm(toggleFormData, {
12+
onSubmit: ({ formData }) => {
13+
formData.set('id', todo.id.toString());
14+
formData.set('state', String(todo.completed));
15+
},
16+
invalidateAll: true
17+
});
1018
11-
const toggleComplete = async () => {
12-
await fetch(`/api/toggle?id=${todo.id}`);
13-
invalidate('query:todos');
14-
};
19+
const { enhance: deleteEnhance } = superForm(deleteFormData, {
20+
onSubmit: ({ formData }) => {
21+
formData.set('id', todo.id.toString());
22+
},
23+
invalidateAll: true
24+
});
1525
</script>
1626

1727
<li class="flex card justify-between p-4 items-center">
1828
<div class="flex gap-4">
19-
<button class="btn" on:click={() => toggleComplete()}>
29+
<button class="btn" form="toggleForm">
2030
<i class={`${todo.completed ? 'fa-solid' : 'fa-regular'} fa-circle-check text-2xl`}></i>
2131
</button>
32+
2233
<div class={`my-auto`}>
2334
<h2 class={`h2 ${todo.completed && 'line-through'}`}>{todo.title}</h2>
2435
<p class="text-lg">{todo.content}</p>
2536
</div>
2637
</div>
27-
<button on:click={() => deleteTodo()} class="btn"
28-
><i class="fa-solid fa-trash text-xl"></i></button
29-
>
38+
<button class="btn" form="deleteForm"><i class="fa-solid fa-trash text-xl"></i></button>
3039
</li>
40+
<form class="hidden" action="?/toggle" method="post" use:toggleEnhance id="toggleForm"></form>
41+
<form class="hidden" action="?/delete" method="post" use:deleteEnhance id="deleteForm"></form>

src/lib/zod.ts

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { z } from 'zod';
2+
3+
export const createSchema = z.object({
4+
title: z.string(),
5+
content: z.string()
6+
});
7+
8+
export const toggleSchema = z.object({
9+
id: z.number(),
10+
state: z.boolean()
11+
});
12+
13+
export const deleteSchema = z.object({
14+
id: z.number()
15+
});

src/routes/+page.server.ts

+36-11
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,30 @@
11
import { db } from '$lib/db/db';
22
import { todosTable } from '$lib/db/schema';
3-
import { desc } from 'drizzle-orm';
3+
import { desc, eq } from 'drizzle-orm';
44
import type { Actions, PageServerLoad } from './$types';
5-
import { z } from 'zod';
65
import { fail, superValidate } from 'sveltekit-superforms';
76
import { zod } from 'sveltekit-superforms/adapters';
8-
9-
const schema = z.object({
10-
title: z.string(),
11-
content: z.string()
12-
});
7+
import { createSchema, deleteSchema, toggleSchema } from '$lib/zod';
138

149
export const load: PageServerLoad = async ({ depends }) => {
1510
depends('query:todos');
1611
const todos = await db.select().from(todosTable).orderBy(desc(todosTable.id));
1712

18-
const form = await superValidate(zod(schema));
13+
const createForm = await superValidate(zod(createSchema));
14+
const toggleForm = await superValidate(zod(toggleSchema));
15+
const deleteForm = await superValidate(zod(deleteSchema));
1916

2017
return {
2118
todos,
22-
form
19+
createForm,
20+
toggleForm,
21+
deleteForm
2322
};
2423
};
2524

2625
export const actions = {
27-
default: async ({ request }) => {
28-
const form = await superValidate(request, zod(schema));
26+
create: async ({ request }) => {
27+
const form = await superValidate(request, zod(createSchema));
2928

3029
if (!form.valid) {
3130
return fail(400, { form });
@@ -35,5 +34,31 @@ export const actions = {
3534
title: form.data.title,
3635
content: form.data.content
3736
});
37+
},
38+
toggle: async ({ request }) => {
39+
const form = await superValidate(request, zod(toggleSchema));
40+
41+
if (!form.valid) {
42+
return fail(400, { form });
43+
}
44+
45+
await db
46+
.update(todosTable)
47+
.set({
48+
completed: !form.data.state
49+
})
50+
.where(eq(todosTable.id, form.data.id));
51+
52+
return { form };
53+
},
54+
delete: async ({ request }) => {
55+
const form = await superValidate(request, zod(toggleSchema));
56+
57+
if (!form.valid) {
58+
return fail(400, { form });
59+
}
60+
61+
await db.delete(todosTable).where(eq(todosTable.id, form.data.id));
62+
return { form };
3863
}
3964
} satisfies Actions;

src/routes/+page.svelte

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
export let data;
77
$: todos = data.todos;
88
9-
const { form, enhance } = superForm(data.form, {
9+
const { form, enhance } = superForm(data.createForm, {
1010
onUpdated() {
1111
invalidate('query:todos');
1212
}
@@ -17,7 +17,7 @@
1717
<h1 class="h1 mb-4">
1818
Todos{#if todos.length}<span>{' '}({todos.length})</span>{/if}:
1919
</h1>
20-
<form method="POST" class="mb-4 flex gap-2 text-black" use:enhance>
20+
<form method="POST" class="mb-4 flex gap-2 text-black" action="?/create" use:enhance>
2121
<input type="text" name="title" placeholder="Title" bind:value={$form.title} />
2222
<input
2323
type="text"
@@ -29,7 +29,7 @@
2929
</form>
3030
<ul class="flex flex-col gap-4">
3131
{#each todos as todo (todo.id)}
32-
<Todo {todo} />
32+
<Todo {todo} toggleFormData={data.toggleForm} deleteFormData={data.deleteForm} />
3333
{/each}
3434
</ul>
3535
</div>

src/routes/api/delete/+server.ts

-10
This file was deleted.

src/routes/api/toggle/+server.ts

-20
This file was deleted.

0 commit comments

Comments
 (0)