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

CRDCDH-621 Restore current URL on login #221

Merged
merged 5 commits into from
Nov 22, 2023
Merged
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
31 changes: 20 additions & 11 deletions src/components/Header/HeaderTabletAndMobile.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { useState } from 'react';
import { NavLink, Link, useNavigate } from 'react-router-dom';
import React, { useEffect, useState } from 'react';
import { NavLink, Link, useNavigate, useLocation } from 'react-router-dom';
import styled from 'styled-components';
import Logo from "./components/LogoMobile";
import menuClearIcon from '../../assets/header/Menu_Cancel_Icon.svg';
Expand Down Expand Up @@ -171,10 +171,12 @@ const Header = () => {
const navbarMobileList: NavbarMobileList = navMobileListHookResult[0];
const setNavbarMobileList = navMobileListHookResult[1];
const [showLogoutAlert, setShowLogoutAlert] = useState<boolean>(false);
const [restorePath, setRestorePath] = useState<string>(null);

const authData = useAuthContext();
const displayName = authData?.user?.firstName || "N/A";
const navigate = useNavigate();
const location = useLocation();

const handleLogout = async () => {
const logoutStatus = await authData.logout();
Expand Down Expand Up @@ -236,6 +238,15 @@ const Header = () => {
setNavbarMobileList(navbarSublists[clickTitle]);
};

useEffect(() => {
if (!location?.pathname || location?.pathname === "/") {
setRestorePath(null);
return;
}

setRestorePath(location?.pathname);
}, [location]);

return (
<>
<GenericAlert open={showLogoutAlert}>
Expand Down Expand Up @@ -401,15 +412,13 @@ const Header = () => {
>
{displayName}
</div>
)
: (
<Link id="navbar-link-login" to="/login">
<div role="button" tabIndex={0} className="navMobileItem" onKeyDown={(e) => { if (e.key === "Enter") { setNavMobileDisplay('none'); } }} onClick={() => setNavMobileDisplay('none')}>
Login
</div>
</Link>

)) : null}
) : (
<Link id="navbar-link-login" to="/login" state={{ redirectURLOnLoginSuccess: restorePath }}>
<div role="button" tabIndex={0} className="navMobileItem" onKeyDown={(e) => { if (e.key === "Enter") { setNavMobileDisplay('none'); } }} onClick={() => setNavMobileDisplay('none')}>
Login
</div>
</Link>
)) : null}
</div>
</div>
<div
Expand Down
20 changes: 16 additions & 4 deletions src/components/Header/components/NavbarDesktop.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { useEffect, useState, useRef } from 'react';
import { NavLink, Link, useNavigate } from 'react-router-dom';
import { NavLink, Link, useNavigate, useLocation } from 'react-router-dom';
import { Button } from '@mui/material';
import styled from 'styled-components';
import { useAuthContext } from '../../Contexts/AuthContext';
Expand Down Expand Up @@ -376,8 +376,11 @@ const NavBar = () => {
const clickableTitle = clickableObject.map((item) => item.name);
const navigate = useNavigate();
const authData = useAuthContext();
const location = useLocation();
const displayName = authData?.user?.firstName?.toUpperCase() || "N/A";
const [showLogoutAlert, setShowLogoutAlert] = useState<boolean>(false);
const [restorePath, setRestorePath] = useState<string>(null);

clickableTitle.push(displayName);

useOutsideAlerter(dropdownSelection, nameDropdownSelection);
Expand Down Expand Up @@ -432,6 +435,16 @@ const NavBar = () => {
useEffect(() => {
setClickedTitle("");
}, []);

useEffect(() => {
if (!location?.pathname || location?.pathname === "/") {
setRestorePath(null);
return;
}

setRestorePath(location?.pathname);
}, [location]);

return (
<Nav>
<GenericAlert open={showLogoutAlert}>
Expand Down Expand Up @@ -499,9 +512,8 @@ const NavBar = () => {
</div>
</div>
</LiSection>
)
: (
<StyledLoginLink id="header-navbar-login-button" to="/login">
) : (
<StyledLoginLink id="header-navbar-login-button" to="/login" state={{ redirectURLOnLoginSuccess: restorePath }}>
Login
</StyledLoginLink>
)}
Expand Down
4 changes: 3 additions & 1 deletion src/config/globalHeaderData.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,12 @@ export const navbarSublists = {
// className: 'navMobileSubTitle',
// },
// ],
//
// To make it a link, the className has to be navMobileSubItem
"Model Navigator": DataCommons.map((dc) => ({
name: `${dc.name} Model`,
link: `/model-navigator/${dc.name}`,
text: '',
className: 'navMobileSubTitle',
className: 'navMobileSubItem',
})),
};