|
1 | 1 | <script lang="ts"> |
2 | 2 | import { tick } from 'svelte'; |
3 | | - import { writable } from 'svelte/store'; |
| 3 | + import { goto } from '$app/navigation'; |
4 | 4 | import { Card, Button, Label, Input, Hr } from 'flowbite-svelte'; |
5 | 5 |
|
6 | 6 | // 必要なコンポーネントだけを読み込んで、コンパイルを時間を短縮 |
|
26 | 26 | export let alternativePageLink: string; |
27 | 27 |
|
28 | 28 | const { form, message, errors, submitting, enhance } = formProperties; |
29 | | - let isSubmitting = writable(false); |
| 29 | +
|
| 30 | + $: isSubmitting = false; |
30 | 31 |
|
31 | 32 | async function handleLoginAsGuest(event: Event) { |
32 | 33 | if (!event || (event instanceof KeyboardEvent && event.key !== 'Enter')) { |
|
35 | 36 | } |
36 | 37 |
|
37 | 38 | event.preventDefault(); |
38 | | - $isSubmitting = true; |
39 | | -
|
40 | | - $form.username = GUEST_USER_NAME; |
41 | 39 |
|
42 | 40 | // HACK: ローカル環境のパスワードは一時的に書き換えて対応している |
43 | 41 | // |
44 | 42 | // See: |
45 | 43 | // src/lib/constants/forms.ts |
| 44 | + $form.username = GUEST_USER_NAME; |
46 | 45 | // $form.password = GUEST_USER_PASSWORD_FOR_LOCAL; |
47 | 46 | $form.password = GUEST_USER_PASSWORD; |
48 | 47 |
|
49 | | - // $formの更新後にフォームを送信 |
50 | | - await tick(); |
51 | | - const authForm = document.getElementById('auth-form') as HTMLFormElement; |
52 | | -
|
53 | | - if (authForm instanceof HTMLFormElement) { |
54 | | - authForm.submit(); |
55 | | - } else { |
56 | | - console.error('Failed to submit the form'); |
| 48 | + try { |
| 49 | + // $formの更新後にフォームを送信 |
| 50 | + await tick(); |
| 51 | + isSubmitting = true; |
| 52 | + const formElement = event.target instanceof Element ? event.target.closest('form') : null; |
| 53 | +
|
| 54 | + if (!formElement || !(formElement instanceof HTMLFormElement)) { |
| 55 | + throw new Error('Not found form element or HTMLFormElement'); |
| 56 | + } |
| 57 | +
|
| 58 | + const response = await fetch('/login', { |
| 59 | + method: 'POST', |
| 60 | + body: new FormData(formElement), |
| 61 | + }); |
| 62 | +
|
| 63 | + if (!response.ok) { |
| 64 | + console.error('Failed to login as a guest: ', response); |
| 65 | + return; |
| 66 | + } |
| 67 | +
|
| 68 | + await goto('/'); |
| 69 | + } catch (error) { |
| 70 | + console.error('Failed to login as a guest: ', error); |
| 71 | + } finally { |
| 72 | + isSubmitting = false; |
57 | 73 | } |
58 | | -
|
59 | | - $isSubmitting = false; |
60 | 74 | } |
61 | 75 |
|
62 | 76 | const UNFOCUSABLE = -1; |
|
72 | 86 | <Card class="w-full max-w-md"> |
73 | 87 | <form id="auth-form" method="post" use:enhance class="flex flex-col space-y-6"> |
74 | 88 | <h3 class="text-xl font-medium text-gray-900 dark:text-white">{title}</h3> |
75 | | - |
76 | 89 | <MessageHelperWrapper message={$message} /> |
77 | 90 |
|
78 | 91 | {#if title === LOGIN_LABEL} |
|
81 | 94 | class="w-full" |
82 | 95 | on:click={handleLoginAsGuest} |
83 | 96 | on:keydown={handleLoginAsGuest} |
84 | | - disabled={$submitting || $isSubmitting} |
| 97 | + disabled={isSubmitting || $submitting} |
85 | 98 | > |
86 | 99 | <div class="text-md">お試し用のアカウントでログイン</div> |
87 | 100 | </Button> |
|
104 | 117 | placeholder="chokudai" |
105 | 118 | aria-invalid={$errors.username ? 'true' : undefined} |
106 | 119 | bind:value={$form.username} |
107 | | - disabled={$submitting} |
| 120 | + disabled={$submitting || isSubmitting} |
108 | 121 | required |
109 | 122 | > |
110 | 123 | <UserOutlineSolid slot="left" class="w-5 h-5" tabindex={UNFOCUSABLE} /> |
|
129 | 142 | placeholder="•••••••" |
130 | 143 | aria-invalid={$errors.password ? 'true' : undefined} |
131 | 144 | bind:value={$form.password} |
132 | | - disabled={$submitting} |
| 145 | + disabled={$submitting || isSubmitting} |
133 | 146 | required |
134 | 147 | > |
135 | 148 | <!-- Show / hide password --> |
|
160 | 173 | </a> |
161 | 174 | </div> --> |
162 | 175 |
|
163 | | - <Button type="submit" class="w-full" disabled={$submitting}> |
| 176 | + <Button type="submit" class="w-full" disabled={$submitting || isSubmitting}> |
164 | 177 | {submitButtonLabel} |
165 | 178 | </Button> |
166 | 179 |
|
|
0 commit comments