Skip to content

Commit 3a727a9

Browse files
author
[bot]
committed
version 2.0.0-next.173
1 parent f630bdd commit 3a727a9

File tree

4 files changed

+44
-121
lines changed

4 files changed

+44
-121
lines changed

src/lib/form.js

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

src/routes/todos/+page.server.js

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -35,28 +35,32 @@ export const load = async ({ locals }) => {
3535
throw error(response.status);
3636
};
3737

38-
/** @type {import('./$types').Action} */
39-
export const POST = async ({ request, locals }) => {
40-
const form = await request.formData();
38+
/** @type {import('./$types').Actions} */
39+
export const actions = {
40+
add: async ({ request, locals }) => {
41+
const form = await request.formData();
4142

42-
await api('POST', `todos/${locals.userid}`, {
43-
text: form.get('text')
44-
});
45-
};
46-
47-
/** @type {import('./$types').Action} */
48-
export const PATCH = async ({ request, locals }) => {
49-
const form = await request.formData();
43+
await api('POST', `todos/${locals.userid}`, {
44+
text: form.get('text')
45+
});
46+
},
47+
edit: async ({ request, locals }) => {
48+
const form = await request.formData();
5049

51-
await api('PATCH', `todos/${locals.userid}/${form.get('uid')}`, {
52-
text: form.has('text') ? form.get('text') : undefined,
53-
done: form.has('done') ? !!form.get('done') : undefined
54-
});
55-
};
50+
await api('PATCH', `todos/${locals.userid}/${form.get('uid')}`, {
51+
text: form.get('text')
52+
});
53+
},
54+
toggle: async ({ request, locals }) => {
55+
const form = await request.formData();
5656

57-
/** @type {import('./$types').Action} */
58-
export const DELETE = async ({ request, locals }) => {
59-
const form = await request.formData();
57+
await api('PATCH', `todos/${locals.userid}/${form.get('uid')}`, {
58+
done: !!form.get('done')
59+
});
60+
},
61+
delete: async ({ request, locals }) => {
62+
const form = await request.formData();
6063

61-
await api('DELETE', `todos/${locals.userid}/${form.get('uid')}`);
64+
await api('DELETE', `todos/${locals.userid}/${form.get('uid')}`);
65+
}
6266
};

src/routes/todos/+page.svelte

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
<script>
2-
import { enhance } from '$lib/form';
2+
import { enhance } from '$app/forms';
3+
import { invalidateAll } from '$app/navigation';
34
import { scale } from 'svelte/transition';
45
import { flip } from 'svelte/animate';
56
67
/** @type {import('./$types').PageData} */
78
export let data;
9+
$: todos = data.todos;
810
</script>
911

1012
<svelte:head>
@@ -17,49 +19,50 @@
1719

1820
<form
1921
class="new"
20-
action="/todos"
22+
action="/todos?/add"
2123
method="post"
22-
use:enhance={{
23-
result: async ({ form }) => {
24-
form.reset();
25-
}
24+
use:enhance={({ form }) => {
25+
return (result) => {
26+
if (result.type === 'success') {
27+
form.reset();
28+
invalidateAll();
29+
}
30+
};
2631
}}
2732
>
2833
<input name="text" aria-label="Add todo" placeholder="+ tap to add a todo" />
2934
</form>
3035

31-
{#each data.todos as todo (todo.uid)}
36+
{#each todos as todo (todo.uid)}
3237
<div
3338
class="todo"
3439
class:done={todo.done}
3540
transition:scale|local={{ start: 0.7 }}
3641
animate:flip={{ duration: 200 }}
3742
>
3843
<form
39-
action="/todos?_method=PATCH"
44+
action="/todos?/toggle"
4045
method="post"
41-
use:enhance={{
42-
pending: ({ data }) => {
43-
todo.done = !!data.get('done');
44-
}
46+
use:enhance={({ data }) => {
47+
todo.done = !!data.get('done');
4548
}}
4649
>
4750
<input type="hidden" name="uid" value={todo.uid} />
4851
<input type="hidden" name="done" value={todo.done ? '' : 'true'} />
4952
<button class="toggle" aria-label="Mark todo as {todo.done ? 'not done' : 'done'}" />
5053
</form>
5154

52-
<form class="text" action="/todos?_method=PATCH" method="post" use:enhance>
55+
<form class="text" action="/todos?/edit" method="post" use:enhance>
5356
<input type="hidden" name="uid" value={todo.uid} />
5457
<input aria-label="Edit todo" type="text" name="text" value={todo.text} />
5558
<button class="save" aria-label="Save todo" />
5659
</form>
5760

5861
<form
59-
action="/todos?_method=DELETE"
62+
action="/todos?/delete"
6063
method="post"
61-
use:enhance={{
62-
pending: () => (todo.pending_delete = true)
64+
use:enhance={() => {
65+
todo.pending_delete = true;
6366
}}
6467
>
6568
<input type="hidden" name="uid" value={todo.uid} />

svelte.config.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,7 @@ import adapter from '@sveltejs/adapter-auto';
33
/** @type {import('@sveltejs/kit').Config} */
44
const config = {
55
kit: {
6-
adapter: adapter(),
7-
8-
// Override http methods in the Todo forms
9-
methodOverride: {
10-
allowed: ['PATCH', 'DELETE']
11-
}
6+
adapter: adapter()
127
}
138
};
149

0 commit comments

Comments
 (0)