Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Login form loading states #159

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions src/components/ButtonLoading.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
---
interface Props extends astroHTML.JSX.ButtonHTMLAttributes {}

const { class: className } = Astro.props;
---

<button
class:list={['button-loading', className]}
data-loading="false"
{...Astro.props}
>
<span class="content">
<slot />
</span>
<span class="spinner" aria-label="loading">Loading...</span>
</button>

<style>
.button-loading {
position: relative;
display: inline-flex;
align-items: center;
justify-content: center;
min-width: 80px;
}

.button-loading[disabled] {
cursor: not-allowed;
opacity: 0.7;
}

.spinner {
position: absolute;
display: none;
}

.button-loading[data-loading='true'] .content {
visibility: hidden;
}

.button-loading[data-loading='true'] .spinner {
display: block;
}
</style>
22 changes: 19 additions & 3 deletions src/pages/login.astro
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
import Layout from '../layouts/Layout.astro';
import ButtonLoading from '../components/ButtonLoading.astro';
---

<Layout title="Login">
Expand All @@ -11,8 +12,8 @@ import Layout from '../layouts/Layout.astro';
<input type="email" id="email" name="email" required />
<label for="password">Password</label>
<input type="password" id="password" name="password" required />
<button type="submit">Login</button>
<button id="reset">Reset Password</button>
<ButtonLoading type="submit">Login</ButtonLoading>
<ButtonLoading id="reset">Reset Password</ButtonLoading>
</form>
</Layout>

Expand All @@ -21,7 +22,13 @@ import Layout from '../layouts/Layout.astro';

async function handleSubmit(event: Event) {
event.preventDefault();
const form = event.target as HTMLFormElement;
const form = document.querySelector('#login-form') as HTMLFormElement;
const submitButton = form.querySelector(
'button[type="submit"]'
) as HTMLButtonElement;
submitButton.dataset.loading = 'true';
submitButton.disabled = true;

const email = form.email.value;
const password = form.password.value;
const success = await login(email, password);
Expand All @@ -31,12 +38,17 @@ import Layout from '../layouts/Layout.astro';
window.location.href = prev || '/';
} else {
alert('Login failed');
submitButton.dataset.loading = 'false';
submitButton.disabled = false;
}
}

async function handleReset(event: Event) {
event.preventDefault();
const form = document.querySelector('#login-form') as HTMLFormElement;
const resetButton = form.querySelector('#reset') as HTMLButtonElement;
resetButton.dataset.loading = 'true';
resetButton.disabled = true;
const email = form.email.value;

if (!email) {
Expand All @@ -47,12 +59,16 @@ import Layout from '../layouts/Layout.astro';
try {
await requestResetEmail(email);
alert('Password reset email sent');
resetButton.dataset.loading = 'false';
resetButton.disabled = false;
} catch (error) {
if (error instanceof Error) {
alert(error.message);
} else {
alert('An error occurred');
}
resetButton.dataset.loading = 'false';
resetButton.disabled = false;
}
}

Expand Down
Loading