papermark icon indicating copy to clipboard operation
papermark copied to clipboard

fix: Single State for Button Disable and Loading

Open mohdjami opened this issue 1 year ago • 9 comments

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

https://github.com/user-attachments/assets/64af5fac-8389-4dc0-b71a-1c94ed9271ab

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

Final:

https://github.com/user-attachments/assets/e0d20dba-fae8-4f07-a36e-28fa0dd31f9e

mohdjami avatar Oct 10 '24 15:10 mohdjami

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

A member of the Team first needs to authorize it.

vercel[bot] avatar Oct 10 '24 15:10 vercel[bot]

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

github-actions[bot] avatar Oct 10 '24 15:10 github-actions[bot]

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

mohdjami avatar Oct 10 '24 15:10 mohdjami

@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.

HarshTiwari-err avatar Oct 10 '24 18:10 HarshTiwari-err

@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 avatar Oct 10 '24 18:10 mohdjami

https://github.com/user-attachments/assets/34412aba-f2b5-434d-b7e7-fe40075881b6

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.

mohdjami avatar Oct 10 '24 19:10 mohdjami

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

vercel[bot] avatar Oct 11 '24 14:10 vercel[bot]

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

mohdjami avatar Oct 11 '24 14:10 mohdjami

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

mohdjami avatar Oct 11 '24 15:10 mohdjami

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

oss-gg[bot] avatar Oct 12 '24 04:10 oss-gg[bot]