Skip to content

Commit

Permalink
add login form loading states
Browse files Browse the repository at this point in the history
  • Loading branch information
narthur committed Oct 27, 2024
1 parent 976d029 commit f3ffbb3
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 3 deletions.
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

0 comments on commit f3ffbb3

Please sign in to comment.