Skip to content

Commit af7bc51

Browse files
committed
Factorized debounce username
1 parent a595097 commit af7bc51

File tree

2 files changed

+22
-19
lines changed

2 files changed

+22
-19
lines changed
Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { superValidate, message, setError } from '$lib/index.js';
1+
import { superValidate, message, setError, type SuperValidated, type Infer } from '$lib/index.js';
22
import { zod } from '$lib/adapters/zod.js';
33
import { fail } from '@sveltejs/kit';
44
import { schema } from './schema.js';
@@ -12,32 +12,34 @@ export const load = async () => {
1212
return { form };
1313
};
1414

15+
function checkUsername(form: SuperValidated<Infer<typeof schema>>) {
16+
if (takenUsernames.includes(form.data.username)) {
17+
setError(form, 'username', 'Username is already taken.');
18+
return false;
19+
}
20+
return true;
21+
}
22+
1523
export const actions: Actions = {
1624
post: async ({ request }) => {
1725
const form = await superValidate(request, zod(schema));
26+
27+
if (!form.valid || !checkUsername(form)) return fail(400, { form });
28+
1829
console.log(form);
1930

20-
if (!form.valid) return fail(400, { form });
31+
// TODO: Create user
2132

2233
return message(form, 'Form posted successfully!');
2334
},
2435

2536
check: async ({ request }) => {
26-
// Introduce a little delay
37+
// Introduce a little delay (large DB check)
2738
await new Promise((res) => setTimeout(res, Math.random() * 500));
2839

29-
const formData = await request.formData();
30-
console.log(formData);
31-
32-
const form = await superValidate(formData, zod(usernameSchema));
33-
console.log(form);
34-
35-
if (!form.valid) return { form };
36-
37-
if (takenUsernames.includes(form.data.username)) {
38-
return setError(form, 'username', 'Username is already taken.');
39-
}
40+
const form = await superValidate(request, zod(usernameSchema));
4041

41-
return { form };
42+
if (!form.valid || !checkUsername(form)) return fail(400, { form });
43+
else return { form };
4244
}
4345
};

src/routes/(v2)/v2/debounce-username/+page.svelte

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
1414
const {
1515
submitting,
16-
submit: checkSubmit
16+
submit: submitCheckUsername,
17+
posted
1718
//enhance: checkEnhance
1819
} = superForm(
1920
{ username: '' },
@@ -30,13 +31,13 @@
3031
}
3132
);
3233
33-
const checkUsername = debounce(250, checkSubmit);
34+
const checkUsername = debounce(250, submitCheckUsername);
3435
3536
// eslint-disable-next-line svelte/valid-compile
3637
$page;
3738
</script>
3839

39-
<SuperDebug data={$form} />
40+
<SuperDebug data={{ $form, $posted }} />
4041

4142
<h3>Superforms testing ground - Zod</h3>
4243

@@ -56,7 +57,7 @@
5657
on:input={checkUsername}
5758
/>
5859
<!-- eslint-disable-next-line svelte/no-at-html-tags -->
59-
{#if $submitting}{@html spinner}{:else if $errors.username}❌{:else if $form.username}✅{/if}
60+
{#if $submitting}{@html spinner}{:else if $errors.username}❌{:else if $form.username && $posted}✅{/if}
6061
{#if $errors.username}<div class="invalid">{$errors.username}</div>{/if}
6162
</label>
6263

0 commit comments

Comments
 (0)