-
-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
147 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { zod } from '$lib/adapters/zod.js'; | ||
import { message, superValidate } from '$lib/server/index.js'; | ||
import { type Actions, fail } from '@sveltejs/kit'; | ||
import { schema } from './schema.js'; | ||
|
||
const items = [ | ||
{ | ||
id: 1, | ||
label: 'One' | ||
}, | ||
{ | ||
id: 2, | ||
label: 'Two' | ||
}, | ||
{ | ||
id: 3, | ||
label: 'Three' | ||
} | ||
]; | ||
|
||
export const load = async () => { | ||
const item_forms = await Promise.all( | ||
items.map((item) => | ||
superValidate(item, zod(schema), { | ||
id: item.id.toString() | ||
}) | ||
) | ||
); | ||
|
||
return { item_forms }; | ||
}; | ||
|
||
export const actions: Actions = { | ||
async create() { | ||
items.push({ | ||
id: items.length + 1, | ||
label: (items.length + 1).toString() | ||
}); | ||
|
||
return { success: true }; | ||
}, | ||
|
||
async save({ request }) { | ||
const form = await superValidate(request, zod(schema)); | ||
|
||
if (!form.valid) { | ||
// Again, return { form } and things will just work. | ||
return fail(400, { form }); | ||
} | ||
|
||
const index = items.findIndex((item) => item.id === form.data.id); | ||
if(index !== -1) { | ||
items[index].label = form.data.label; | ||
} | ||
|
||
return message(form, `Item ${form.data.id} updated`); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<script lang="ts"> | ||
import type { PageData } from './$types.js'; | ||
import { superForm } from '$lib/client/index.js'; | ||
import Form from './Form.svelte'; | ||
import { enhance } from '$app/forms'; | ||
let { | ||
data | ||
}: { | ||
data: PageData; | ||
} = $props(); | ||
const superforms = $derived(data.item_forms.map(item_form => superForm(item_form, { | ||
id: item_form.id, | ||
dataType: 'json' | ||
}))); | ||
</script> | ||
|
||
{#each superforms as superform (superform.formId)} | ||
<Form {superform} /> | ||
<hr> | ||
{/each} | ||
|
||
<form action="?/create" method=post use:enhance> | ||
<button type=submit>Add</button> | ||
</form> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<script lang="ts"> | ||
import type { SuperForm } from '$lib/index.js'; | ||
import type { schema } from './schema.js'; | ||
import { z } from 'zod'; | ||
import SuperDebug from '$lib/client/SuperDebug.svelte'; | ||
let { superform }: { | ||
superform: SuperForm<z.infer<typeof schema>>; | ||
} = $props(); | ||
let { form, message, enhance } = superform; | ||
</script> | ||
|
||
<form action="?/save" method="post" use:enhance> | ||
<input type="hidden" name="id" value={$form.id} /> | ||
|
||
<SuperDebug data={$form} /> | ||
|
||
<label> | ||
<input type="text" name="label" bind:value={$form.label} /> | ||
</label> | ||
|
||
<button type="submit">Save</button> | ||
|
||
{#if $message} | ||
<p>{$message}</p> | ||
{/if} | ||
</form> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { z } from 'zod'; | ||
|
||
export const schema = z.object({ | ||
id: z.number(), | ||
label: z.string() | ||
}); |