Skip to content

Commit

Permalink
Merge pull request #1525 from hackforla/evan1510
Browse files Browse the repository at this point in the history
feat: Delay the 'sorry not working' msg
  • Loading branch information
trillium authored Oct 7, 2023
2 parents 9361a0d + 9ed9214 commit 942b871
Showing 1 changed file with 32 additions and 20 deletions.
52 changes: 32 additions & 20 deletions client/src/components/auth/HandleAuth.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { useState, useEffect } from 'react';
import { Redirect } from 'react-router-dom';
import { isValidToken } from '../../services/user.service';
import {authLevelRedirect} from '../../utils/authUtils'
import { authLevelRedirect } from '../../utils/authUtils';

import '../../sass/MagicLink.scss';
import useAuth from '../../hooks/useAuth';
Expand All @@ -17,47 +17,59 @@ const HandleAuth = (props) => {
const params = new URLSearchParams(search);
const api_token = params.get('token');

if(!api_token) return;
if (!api_token) return;
isValidToken(api_token).then((isValid) => {
setMagicLink(isValid)
setMagicLink(isValid);
});
}, [props.location.search]);

// Step 2: Refresh user auth (requires valid Magic Link)
useEffect(() => {
if(!isMagicLinkValid) return;
if(!auth?.isError) return;
if (!isMagicLinkValid) return;
if (!auth?.isError) return;

refreshAuth();
},[isMagicLinkValid, refreshAuth, auth])
}, [isMagicLinkValid, refreshAuth, auth]);

// Step 3: Set IsLoaded value to render Component
useEffect(() => {
if(!isMagicLinkValid) {
if (!isMagicLinkValid) {
setIsLoaded(true);
return;
};
}

if (!auth || auth.isError) return;

if(!auth || auth.isError) return;

setIsLoaded(true);
},[isMagicLinkValid, setIsLoaded, auth])
}, [isMagicLinkValid, setIsLoaded, auth]);

if (!isLoaded) return <div>Loading...</div>;

const Delayed = ({ children, waitBeforeShow = 500 }) => {
const [isShown, setIsShown] = useState(false);
useEffect(() => {
const timer = setTimeout(() => {
setIsShown(true);
}, waitBeforeShow);

return () => clearTimeout(timer);
}, [waitBeforeShow]);

return isShown ? children : null;
};

if(!isLoaded)
return (
<div>Loading...</div>
);
let loginRedirect = '';

if (auth?.user) {
const loginRedirect = authLevelRedirect(auth.user);
return (
<Redirect to={loginRedirect} />
);
loginRedirect = authLevelRedirect(auth.user);
}

return (
<div className="flex-container">
<div>Sorry, the link is not valid anymore.</div>
<Delayed waitBeforeShow={1000}>
<div>Sorry, the link is not valid anymore.</div>
</Delayed>
{auth?.user && <Redirect to={loginRedirect} /> /* Redirect to /welcome */}
</div>
);
};
Expand Down

0 comments on commit 942b871

Please sign in to comment.