Skip to content

Commit

Permalink
- Factors out user hook to only HeaderNavBar
Browse files Browse the repository at this point in the history
- Adds refetch to hook to get username after login
- Changes auth slice to store the user to state in a way that gets picked up by userData
  • Loading branch information
sophia-massie committed Nov 26, 2024
1 parent 09dbad7 commit 81e4a4f
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 42 deletions.
45 changes: 42 additions & 3 deletions react/src/components/HeaderNavBar/HeaderNavBar.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,42 @@
import React from 'react';
import React, { useEffect } from 'react';
import { Layout } from 'antd';
import useAuthenticatedUser from '@hazmapper/hooks/user/useAuthenticatedUser';
import { useNavigate } from 'react-router-dom';
import { Button, InlineMessage, LoadingSpinner } from '@tacc/core-components';
import styles from './HeaderNavBar.module.css';

export const HeaderNavBar: React.FC<{ user: string }> = ({ user }) => {
export const HeaderNavBar: React.FC = () => {
const { Header } = Layout;
const navigate = useNavigate();

const {
data: userData,
isLoading: isUserLoading,
error: isUserError,
refetch,
} = useAuthenticatedUser();

const handleLogin = () => {
const url = `/login?to=${encodeURIComponent(location.pathname)}`;
navigate(url);
};

useEffect(() => {
if (userData) refetch();
}, [userData, refetch]);

if (isUserLoading) {
return <LoadingSpinner />;
}

if (!isUserError) {
return (
<InlineMessage type="error">
{' '}
There was an error loading your username.
</InlineMessage>
);
}

return (
<Header className={styles.root}>
Expand All @@ -12,7 +45,13 @@ export const HeaderNavBar: React.FC<{ user: string }> = ({ user }) => {
src="./src/assets/hazmapper-header-logo.png"
alt="Hazmapper Logo"
/>
<div className={styles.userName}>{user}</div>
{userData && userData.username ? (
<div className={styles.userName}>{userData.username}</div>
) : (
<Button type="link" className={styles.userName} onClick={handleLogin}>
Login
</Button>
)}
</Header>
);
};
Expand Down
8 changes: 7 additions & 1 deletion react/src/hooks/user/useAuthenticatedUser.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
import { useSelector } from 'react-redux';
import { RootState } from '../../redux/store';
import { AuthenticatedUser } from '@hazmapper/types';
import { useState } from 'react';

type SuccessResult<T> = {
data: T;
isLoading: false;
error: null;
refetch: () => void;
};

// TODO remove this placeholder hook
const useAuthenticatedUser = (): SuccessResult<AuthenticatedUser> => {
const [, forceRerender] = useState({});
let username = useSelector((state: RootState) => state.auth.user?.username);

if (!username) {
username = '';
}
const refetch = () => {
forceRerender({});
};

return { data: { username }, isLoading: false, error: null };
return { data: { username }, isLoading: false, error: null, refetch };
};

export default useAuthenticatedUser;
27 changes: 1 addition & 26 deletions react/src/pages/MainMenu/MainMenu.tsx
Original file line number Diff line number Diff line change
@@ -1,37 +1,12 @@
import React from 'react';
import { LoadingSpinner, InlineMessage } from '@tacc/core-components';
import useAuthenticatedUser from '@hazmapper/hooks/user/useAuthenticatedUser';
import ProjectListing from '@hazmapper/components/Projects/ProjectListing';
import styles from './layout.module.css';
import HeaderNavBar from '@hazmapper/components/HeaderNavBar';

const MainMenu = () => {
const {
data: userData,
isLoading: isUserLoading,
error: userError,
} = useAuthenticatedUser();

if (isUserLoading) {
return (
<>
<HeaderNavBar user={''} />
<LoadingSpinner />
</>
);
}
if (userError) {
return (
<>
<HeaderNavBar user={''} />
<InlineMessage type="error">Unable to retrieve projects.</InlineMessage>
</>
);
}

return (
<div className={styles.root}>
<HeaderNavBar user={userData.username} />
<HeaderNavBar />
<div className={styles.listingContainer}>
<ProjectListing />
</div>
Expand Down
10 changes: 2 additions & 8 deletions react/src/pages/MapProject/MapProject.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,7 @@ import Map from '@hazmapper/components/Map';
import AssetsPanel from '@hazmapper/components/AssetsPanel';
import ManageMapProjectModal from '@hazmapper/components/ManageMapProjectModal';
import { queryPanelKey, Panel } from '@hazmapper/utils/panels';
import {
useAuthenticatedUser,
useFeatures,
useProject,
useTileServers,
} from '@hazmapper/hooks';
import { useFeatures, useProject, useTileServers } from '@hazmapper/hooks';
import { useParams } from 'react-router-dom';
import styles from './MapProject.module.css';
import MapProjectNavBar from '@hazmapper/components/MapProjectNavBar';
Expand Down Expand Up @@ -94,7 +89,6 @@ const LoadedMapProject: React.FC<LoadedMapProject> = ({
activeProject,
isPublicView,
}) => {
const { data: userData } = useAuthenticatedUser();
const [selectedAssetTypes, setSelectedAssetTypes] = useState<string[]>(
Object.keys(assetTypeOptions)
);
Expand Down Expand Up @@ -158,7 +152,7 @@ const LoadedMapProject: React.FC<LoadedMapProject> = ({

return (
<div className={styles.root}>
<HeaderNavBar user={userData ? userData.username : ''} />
<HeaderNavBar />
<div className={styles.mapControlBar}>
MapTopControlBar
{loading && <div> loading</div>}
Expand Down
6 changes: 2 additions & 4 deletions react/src/redux/authSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@ const authSlice = createSlice({
state,
action: PayloadAction<{ user: AuthenticatedUser; authToken: AuthToken }>
) {
state = {
user: action.payload.user,
authToken: action.payload.authToken,
};
state.user = action.payload.user;
state.authToken = action.payload.authToken;

// save to local storage
setAuthenticatedUserFromLocalStorage(state);
Expand Down

0 comments on commit 81e4a4f

Please sign in to comment.