Skip to content

Impr: add loading spinner to signin and signup buttons #582

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

Open
wants to merge 2 commits into
base: main
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
18 changes: 13 additions & 5 deletions src/components/auth/signin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
import { useToast } from '../ui/use-toast';
import { DemarcationLine, GoogleOauthButton } from './social-auth';
import { PasswordInput } from '../password-input';
import { LoadingSpinner } from '../loading-spinner';

export const Signin = () => {
const { toast } = useToast();
Expand All @@ -40,10 +41,11 @@ export const Signin = () => {
const response = await signIn('signin', { ...data, redirect: false });
if (!response?.ok) {
const errorMessage =
response?.error?.includes('User') && response?.error?.includes('does not exist')
response?.error?.includes('User') &&
response?.error?.includes('does not exist')
? 'User does not exist'
: response?.error || 'Internal server error';

return toast({
title: errorMessage,
variant: 'destructive',
Expand All @@ -53,7 +55,7 @@ export const Signin = () => {
title: 'Login successful! Welcome back!',
variant: 'success',
});

const searchParams = new URLSearchParams(window.location.search);
const redirect = searchParams.get('next') || APP_PATHS.HOME;
router.push(redirect);
Expand All @@ -65,7 +67,7 @@ export const Signin = () => {
});
}
}

return (
<div className="">
<Form {...form}>
Expand Down Expand Up @@ -114,7 +116,13 @@ export const Signin = () => {
className="w-full h-10"
aria-label="submit"
>
{form.formState.isSubmitting ? 'Please wait...' : 'Sign In'}
{form.formState.isSubmitting ? (
<>
<LoadingSpinner className="mr-2 size-5" /> Please wait...
</>
) : (
'Sign In'
)}
</Button>
<DemarcationLine />
<GoogleOauthButton label="Sign in with Google" />
Expand Down
9 changes: 8 additions & 1 deletion src/components/auth/signup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { useToast } from '../ui/use-toast';
import { signUp } from '@/actions/auth.actions';
import { DemarcationLine, GoogleOauthButton } from './social-auth';
import { PasswordInput } from '../password-input';
import { LoadingSpinner } from '../loading-spinner';

export const Signup = () => {
const { toast } = useToast();
Expand Down Expand Up @@ -121,7 +122,13 @@ export const Signup = () => {
className="w-full h-10"
aria-label="submit"
>
{form.formState.isSubmitting ? 'Please wait...' : 'Create Account'}
{form.formState.isSubmitting ? (
<>
<LoadingSpinner className="mr-2 size-5" /> Please wait...
</>
) : (
'Create Account'
)}
</Button>
<DemarcationLine />
<GoogleOauthButton label="Sign up with Google" />
Expand Down
10 changes: 8 additions & 2 deletions src/components/loading-spinner.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
'use client';
import { cn } from '@/lib/utils';

export const LoadingSpinner = () => {
export const LoadingSpinner = (props: { className?: string }) => {
return (
<div className="flex justify-center items-center">
<div className="size-6 border-4 border-t-4 border-gray-200 rounded-full animate-spin border-t-blue-500"></div>
<div
className={cn(
'size-6 border-4 border-t-4 border-gray-200 rounded-full animate-spin border-t-blue-500',
props.className
)}
></div>
</div>
);
};