Skip to content

Commit

Permalink
[#19] Add header element and pass user instead of context for Outlet …
Browse files Browse the repository at this point in the history
…in router
  • Loading branch information
liamstevens111 committed Mar 21, 2023
1 parent 126b62e commit 98c2cf4
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 15 deletions.
16 changes: 16 additions & 0 deletions src/components/Header/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { User } from 'types/User';

type HeaderProps = {
user: User;
};

function Header({ user }: HeaderProps) {
return (
<header className="flex w-screen flex-row items-center justify-between p-0">
<div className="">Test logo for {user.email}</div>
<div className="profile-image">Test image for {user.name}</div>
</header>
);
}

export default Header;
20 changes: 11 additions & 9 deletions src/components/PrivateRoutes/index.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { useState, useEffect } from 'react';
import { Navigate, Outlet, useOutletContext } from 'react-router-dom';
import { Navigate, Outlet } from 'react-router-dom';

import Header from 'components/Header';
import { getItem } from 'helpers/localStorage';
import type { User } from 'types/User';

type ContextType = User;

import { LOGIN_URL } from '../../constants';

function PrivateRoutes() {
Expand All @@ -16,7 +15,7 @@ function PrivateRoutes() {
const fetchCurrentUser = async () => {
const userProfile = getItem('UserProfile');
if (userProfile?.user) {
setUser({ ...userProfile.user });
setUser({ name: userProfile.user.name, email: userProfile.user.email, avatarUrl: userProfile.user.avatar_url });
}

setLoading(false);
Expand All @@ -28,11 +27,14 @@ function PrivateRoutes() {
return <h3>Loading...</h3>;
}

return user ? <Outlet context={user} /> : <Navigate to={LOGIN_URL} />;
return user ? (
<>
<Header user={user} />
<Outlet />
</>
) : (
<Navigate to={LOGIN_URL} />
);
}

export default PrivateRoutes;

export function useUser() {
return useOutletContext<ContextType>();
}
6 changes: 0 additions & 6 deletions src/screens/Home/index.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
import { useUser } from 'components/PrivateRoutes';

const HomeScreen = (): JSX.Element => {
const user = useUser();

return (
<>
<div className="my-8 text-white opacity-50" data-test-id="app-main-heading">
Home Screen
</div>
{/* TODO: Remove when header implemented in #19 */}
<div className="my-8 text-white opacity-50">{`${user?.name} - ${user?.email} - ${user?.avatarUrl}`}</div>
</>
);
};
Expand Down

0 comments on commit 98c2cf4

Please sign in to comment.