From 98c2cf43222c47882df184f73691db46c8d2e6b6 Mon Sep 17 00:00:00 2001 From: Liam Stevens <8955671+liamstevens111@users.noreply.github.com> Date: Wed, 8 Mar 2023 16:28:29 +0700 Subject: [PATCH] [#19] Add header element and pass user instead of context for Outlet in router --- src/components/Header/index.tsx | 16 ++++++++++++++++ src/components/PrivateRoutes/index.tsx | 20 +++++++++++--------- src/screens/Home/index.tsx | 6 ------ 3 files changed, 27 insertions(+), 15 deletions(-) create mode 100644 src/components/Header/index.tsx diff --git a/src/components/Header/index.tsx b/src/components/Header/index.tsx new file mode 100644 index 0000000..ef1f11f --- /dev/null +++ b/src/components/Header/index.tsx @@ -0,0 +1,16 @@ +import { User } from 'types/User'; + +type HeaderProps = { + user: User; +}; + +function Header({ user }: HeaderProps) { + return ( +
+
Test logo for {user.email}
+
Test image for {user.name}
+
+ ); +} + +export default Header; diff --git a/src/components/PrivateRoutes/index.tsx b/src/components/PrivateRoutes/index.tsx index 74d261e..ce3c46d 100644 --- a/src/components/PrivateRoutes/index.tsx +++ b/src/components/PrivateRoutes/index.tsx @@ -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() { @@ -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); @@ -28,11 +27,14 @@ function PrivateRoutes() { return

Loading...

; } - return user ? : ; + return user ? ( + <> +
+ + + ) : ( + + ); } export default PrivateRoutes; - -export function useUser() { - return useOutletContext(); -} diff --git a/src/screens/Home/index.tsx b/src/screens/Home/index.tsx index 6cc0710..c87cb3e 100644 --- a/src/screens/Home/index.tsx +++ b/src/screens/Home/index.tsx @@ -1,15 +1,9 @@ -import { useUser } from 'components/PrivateRoutes'; - const HomeScreen = (): JSX.Element => { - const user = useUser(); - return ( <>
Home Screen
- {/* TODO: Remove when header implemented in #19 */} -
{`${user?.name} - ${user?.email} - ${user?.avatarUrl}`}
); };