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

fix: Single State for Button Disable and Loading #713

Merged
merged 7 commits into from
Oct 12, 2024

Conversation

mohdjami
Copy link
Contributor

@mohdjami mohdjami commented Oct 10, 2024

fixes #711

Description

Created a single state for both loading and disabling buttons.
This code not only adds a single state to the Login page but also reduces the use of separate loaders without affecting any error handling in the form. No need to maintain separate states for each button.

Used states before changes

  const [isLoginWithEmail, setIsLoginWithEmail] = useState<boolean>(false);
  const [isLoginWithGoogle, setIsLoginWithGoogle] = useState<boolean>(false);
  const [isLoginWithLinkedIn, setIsLoginWithLinkedIn] =
    useState<boolean>(false);

State after changes

 const [clickedMethod, setClickedMethod] = useState<AuthMethod | undefined>(
    undefined,
  );

Example button before changes

          <Button
              onClick={() => {
                setIsLoginWithLinkedIn(true);
                signIn("linkedin", {
                  ...(next && next.length > 0 ? { callbackUrl: next } : {}),
                }).then((res) => {
                  if (res?.status) {
                    setIsLoginWithLinkedIn(false);
                  }
                });
              }}
              disabled={isLoginWithLinkedIn}
              className="flex items-center justify-center space-x-2 border border-gray-200 bg-gray-100 font-normal text-gray-900 hover:bg-gray-200"
            >
              {isLoginWithLinkedIn ? (
                <Loader className="mr-2 h-5 w-5 animate-spin " />
              ) : (
                <LinkedIn />
              )}
              <span>Continue with LinkedIn</span>
            </Button>

Example button after changes

          <Button
              onClick={() => {
                setClickedMethod("google");
                signIn("google", {
                  ...(next && next.length > 0 ? { callbackUrl: next } : {}),
                }).then((res) => {
                  if (res?.status) {
                    setClickedMethod(undefined);
                  }
                });
              }}
              loading={clickedMethod === "google"}
              disabled={clickedMethod && clickedMethod !== "google"}
              className="flex items-center justify-center space-x-2 border border-gray-200 bg-gray-100 font-normal text-gray-900 hover:bg-gray-200"
            >
              <Google className="h-5 w-5" />
              <span>Continue with Google</span>
            </Button>

Preview

fixed.loading.state.mp4

This improves user experience by preventing multiple actions and maintaining a clear visual indication of the active button.

Final:

final.demo.mp4

@mohdjami mohdjami requested a review from mfts as a code owner October 10, 2024 15:33
Copy link

vercel bot commented Oct 10, 2024

@mohdjami is attempting to deploy a commit to the mftsio Team on Vercel.

A member of the Team first needs to authorize it.

Copy link

github-actions bot commented Oct 10, 2024

All contributors have signed the CLA ✍️ ✅
Posted by the CLA Assistant Lite bot.

@mohdjami
Copy link
Contributor Author

I have read the CLA Document and I hereby sign the CLA

@mohdjami mohdjami changed the title Single State for Button Disable and Loading fix: Single State for Button Disable and Loading Oct 10, 2024
@HarshTiwari-err
Copy link

@mohdjami
Please test one scenario:
Enter the email and click the submit button.
Check if your email is stuck on loading if there is any sort of server error.

@mohdjami
Copy link
Contributor Author

@mohdjami Please test one scenario: Enter the email and click the submit button. Check if your email is stuck on loading if there is any sort of server error.

Sure will do that, and try.

@mohdjami
Copy link
Contributor Author

mohdjami commented Oct 10, 2024

final.demo.mp4

I tested with email and google login. It works fine, Let me know if you find anything wrong. My goal in this PR was not to change the existing error handling but just to fix the loading and disabling.

Copy link
Owner

@mfts mfts left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great job!

Copy link

vercel bot commented Oct 11, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
papermark ✅ Ready (Inspect) Visit Preview 💬 Add feedback Oct 12, 2024 4:52am

@mohdjami
Copy link
Contributor Author

@mfts Let me know if I need to make any changes.

app/(auth)/login/page.tsx Outdated Show resolved Hide resolved
app/(auth)/login/page.tsx Outdated Show resolved Hide resolved
@mohdjami
Copy link
Contributor Author

@mfts Thanks for addressing all the issues. Let me know if I missed anything.

@mfts mfts merged commit 214201d into mfts:main Oct 12, 2024
3 checks passed
Copy link

oss-gg bot commented Oct 12, 2024

Awarding mohdjami: 150 points 🕹️ Well done! Check out your new contribution on oss.gg/mohdjami

@github-actions github-actions bot locked and limited conversation to collaborators Oct 12, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[🕹️] Loading on Login/Signup: Disable Buttons
3 participants