Skip to content

Commit fb9ca0d

Browse files
committed
Adding test.
1 parent 269393b commit fb9ca0d

File tree

2 files changed

+136
-0
lines changed

2 files changed

+136
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { fail } from '@sveltejs/kit';
2+
import { superValidate } from '$lib/server';
3+
import { z } from 'zod';
4+
5+
const postSchema = z.object({
6+
questions: z
7+
.object({
8+
text: z.string(),
9+
generated: z.boolean()
10+
})
11+
.array()
12+
.min(1, {
13+
message: 'Must have at least one question'
14+
})
15+
});
16+
17+
export const load = async () => {
18+
const form = await superValidate(postSchema);
19+
return { form };
20+
};
21+
22+
export const actions = {
23+
default: async (event) => {
24+
const formData = await event.request.formData();
25+
const form = await superValidate(formData, postSchema);
26+
27+
console.log('🚀 ~ file: +page.server.ts:31 ~ default: ~ form:', form);
28+
29+
if (!form.valid) {
30+
return fail(400, {
31+
form
32+
});
33+
}
34+
return { form };
35+
}
36+
};

src/routes/tests/sten/+page.svelte

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
<script lang="ts">
2+
import { z } from 'zod';
3+
import { page } from '$app/stores';
4+
import { superForm } from '$lib/client';
5+
6+
const postSchema = z.object({
7+
questions: z
8+
.object({
9+
text: z.string(),
10+
generated: z.boolean()
11+
})
12+
.array()
13+
.min(1, {
14+
message: 'Must have at least one question'
15+
})
16+
});
17+
18+
const { form, errors, message, enhance, constraints } = superForm<
19+
typeof postSchema
20+
>($page.data.form, {
21+
taintedMessage: 'Are you sure you want to leave?',
22+
validators: postSchema,
23+
resetForm: true
24+
//dataType: 'json'
25+
});
26+
27+
let tooManyQuestions = false;
28+
let query = '';
29+
30+
function addQuestion() {
31+
if (!query) {
32+
console.log('return');
33+
return;
34+
}
35+
if ($form.questions.length >= 10) {
36+
tooManyQuestions = true;
37+
return;
38+
}
39+
$form.questions = [
40+
...$form.questions,
41+
{ text: query, generated: false }
42+
];
43+
query = '';
44+
console.log('addQuestion', $form);
45+
}
46+
47+
function removeQuestion(index: number) {
48+
$form.questions.splice(index, 1);
49+
$form.questions = $form.questions;
50+
if ($form.questions.length <= 10) {
51+
tooManyQuestions = false;
52+
}
53+
}
54+
</script>
55+
56+
<h1>Welcome to SvelteKit</h1>
57+
58+
<form method="POST" class="sm:p-4 md:p-8">
59+
<div class="mt-2 flex flex-row">
60+
<input
61+
bind:value={query}
62+
type="text"
63+
class="block w-full rounded-md border-0 bg-white/5 py-1.5 text-white shadow-sm ring-1 ring-inset ring-white/10 focus:ring-2 focus:ring-inset focus:ring-indigo-500 sm:text-sm sm:leading-6"
64+
/>
65+
<button
66+
type="button"
67+
on:click={addQuestion}
68+
class="m-2 h-7 w-7 cursor-pointer"
69+
/>
70+
{#each $form.questions as _, index}
71+
<div class="col-span-full">
72+
<div class="my-2 flex flex-col">
73+
<label for="questions" class="font-dmsans text-sm text-white"
74+
>Question {index}</label
75+
>
76+
<div class="mt-2 flex flex-row">
77+
<input
78+
type="text"
79+
id={`q_${index}`}
80+
bind:value={$form.questions[index].text}
81+
name="questions"
82+
class="block w-full rounded-md border-0 bg-white/5 py-1.5 text-white shadow-sm ring-1 ring-inset ring-white/10 focus:ring-2 focus:ring-inset focus:ring-indigo-500 sm:text-sm sm:leading-6"
83+
/>
84+
{#if $errors.questions?.[index].text}{/if}
85+
<button
86+
type="button"
87+
class="m-2 h-7 w-7 cursor-pointer text-red-600"
88+
on:click={() => removeQuestion(index)}
89+
/>
90+
</div>
91+
</div>
92+
</div>
93+
{/each}
94+
</div>
95+
<button
96+
type="submit"
97+
class="rounded-md bg-indigo-500 px-3 py-2 text-sm font-semibold text-white shadow-sm hover:bg-indigo-400 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-500"
98+
>Save</button
99+
>
100+
</form>

0 commit comments

Comments
 (0)