|
11 | 11 | } from '$lib/inputvalidation/usernameValidator'; |
12 | 12 | import type { AnyComponent } from '$lib/types/AnyComponent'; |
13 | 13 | import type { ValidationResult } from '$lib/types/ValidationResult'; |
14 | | - import type { TimeoutHandle } from '$lib/types/WAPI'; |
| 14 | + import { debounce } from '$lib/utils/debounce'; |
15 | 15 | import type { Snippet } from 'svelte'; |
16 | 16 | import type { FullAutoFill } from 'svelte/elements'; |
17 | 17 |
|
|
39 | 39 |
|
40 | 40 | let checkResponses = $state<Map<string, ValidationResult>>(new Map()); |
41 | 41 | let validationResult = $state<ValidationResult | null>(null); |
42 | | - let usernameDebounce: TimeoutHandle | undefined; |
43 | | - function checkUsernameAvailability(username: string) { |
44 | | - // Stop the previous debounce timer if it exists |
45 | | - clearTimeout(usernameDebounce); |
46 | 42 |
|
| 43 | + const requestAvailability = debounce(async (username: string) => { |
| 44 | + try { |
| 45 | + const response = await accountV2Api.accountCheckUsername({ username }); |
| 46 | + validationResult = mapUsernameCheckResponse(response); |
| 47 | + checkResponses.set(username, validationResult); |
| 48 | + } catch (error) { |
| 49 | + await handleApiError(error, (err) => { |
| 50 | + if (!isValidationError(err)) { |
| 51 | + validationResult = UsernameInternalServerErrorValRes; |
| 52 | + return false; |
| 53 | + } |
| 54 | +
|
| 55 | + const apiValRes = mapToValRes(err, 'Username'); |
| 56 | + if (apiValRes !== null) { |
| 57 | + validationResult = apiValRes; |
| 58 | + return true; |
| 59 | + } |
| 60 | +
|
| 61 | + return false; |
| 62 | + }); |
| 63 | + } |
| 64 | + }, 250); |
| 65 | +
|
| 66 | + function checkUsernameAvailability(username: string) { |
47 | 67 | const entry = checkResponses.get(username); |
48 | 68 | if (entry !== undefined) { |
| 69 | + requestAvailability.cancel(); |
49 | 70 | validationResult = entry; |
50 | | - usernameDebounce = undefined; |
51 | 71 | return; |
52 | 72 | } |
53 | 73 |
|
54 | | - // Set the validation result to the checking availability state |
55 | 74 | validationResult = UsernameCheckingAvailabilityValRes; |
56 | | -
|
57 | | - // Start a new username request in 250ms |
58 | | - usernameDebounce = setTimeout(async () => { |
59 | | - // 250ms has passed, check if the username is available |
60 | | - try { |
61 | | - // Make the API request |
62 | | - const response = await accountV2Api.accountCheckUsername({ username }); |
63 | | -
|
64 | | - // Map the response to a validation result |
65 | | - validationResult = mapUsernameCheckResponse(response); |
66 | | -
|
67 | | - checkResponses.set(username, validationResult); |
68 | | - } catch (error) { |
69 | | - // Show an error toast |
70 | | - await handleApiError(error, (err) => { |
71 | | - if (!isValidationError(err)) { |
72 | | - // Set the validation result to the internal server error state |
73 | | - validationResult = UsernameInternalServerErrorValRes; |
74 | | - return false; |
75 | | - } |
76 | | -
|
77 | | - const apiValRes = mapToValRes(err, 'Username'); |
78 | | - if (apiValRes !== null) { |
79 | | - validationResult = apiValRes; |
80 | | - return true; |
81 | | - } |
82 | | -
|
83 | | - return false; |
84 | | - }); |
85 | | - } |
86 | | - }, 250); |
| 75 | + requestAvailability(username); |
87 | 76 | } |
88 | 77 |
|
89 | 78 | $effect(() => { |
|
0 commit comments