diff --git a/components/Navbars/TopBar.tsx b/components/Navbars/TopBar.tsx index e6ad569a7..0e33c98e0 100644 --- a/components/Navbars/TopBar.tsx +++ b/components/Navbars/TopBar.tsx @@ -24,6 +24,9 @@ import { toast } from 'react-toastify'; import Notifications from '../Modals/Notifications'; import axios from 'axios'; import { MARKETPLACE_API_URL } from '@modules/marketplace/http'; +import { returnFirstAndLastLetter } from '../../helpers'; +import { useQuery } from '@tanstack/react-query'; +import $http from '../../http/axios'; function TopBar(props: { activePage: string; showDashBorad: boolean }) { // change auth to True to see Auth User Header @@ -43,17 +46,31 @@ function TopBar(props: { activePage: string; showDashBorad: boolean }) { const [dropDown, setDropDown] = useState('Explore'); const { cartCount, setCartCountNav } = useCart(); const [shopId, setShopId] = useState(''); + + /** LINES 53 - 61 WAS COPIED FROM UpdatingProfilePic.tsx Component. + * If changing anything, make sure it stays the same in UpdatingProfilePic.tsx + */ + const userId = globalAuth?.user.id; + const baseUrl = `${API_BASE_URL}/portfolio/` as string; + + const { data: userData, isError: isUserDataError } = useQuery(['userData', userId], async () => { + const response = await $http.get(`${baseUrl}users/${userId}`); + if (response.status === 200) { + return response.data; + } + }); + useEffect(() => { async function cartFetch() { let carts; let token = localStorage.getItem('zpt') as string; - + if (token) { carts = await getUserCart(token as string); } else { carts = localStorage.getItem('products') ? JSON.parse(localStorage.getItem('products') as string) : []; } - + setCartCountNav(carts.length); } cartFetch(); @@ -499,7 +516,10 @@ function TopBar(props: { activePage: string; showDashBorad: boolean }) { )} {notificationMenu && ( -
+
)} @@ -508,6 +528,10 @@ function TopBar(props: { activePage: string; showDashBorad: boolean }) { ); function AuthUser(): React.ReactNode { + const getUserInitials = (firstName: string, lastName: string) => { + return `${firstName.charAt(0)}${lastName.charAt(0)}`; + } + return (
@@ -552,7 +576,25 @@ function TopBar(props: { activePage: string; showDashBorad: boolean }) { {globalAuth?.user?.firstName} {globalAuth?.user?.lastName}

-
+ {userData?.data?.user?.profilePic ? ( +
+ avatar +
+ ) : ( + globalAuth?.user && ( +
+ + {getUserInitials(globalAuth?.user?.firstName, globalAuth?.user?.lastName)} + +
+ ) + )}
{/* {notificationMenu && @@ -760,9 +802,9 @@ function Cart({ items, style }: { items: number; style?: {} }) {
{/* {items > 0 && ( */} - - {items} - + + {items} + {/* )} */} Cart Icon diff --git a/context/AuthContext.tsx b/context/AuthContext.tsx index e0c307e6d..71d53e5f8 100644 --- a/context/AuthContext.tsx +++ b/context/AuthContext.tsx @@ -52,4 +52,12 @@ export function AuthContextProvider({ children }: { children: React.ReactNode }) } // use this hook in your component to have access to the AuthContext -export const useAuth = () => useContext(AuthContext); +export const useAuth = () => { + const authContext = useContext(AuthContext) + + if(!authContext) { + throw new Error('useAuth must be used within an AuthProvider') + } + + return authContext; +}; diff --git a/modules/portfolio/component/portfolioSettingsComponents/UpdatingProfilePic.tsx b/modules/portfolio/component/portfolioSettingsComponents/UpdatingProfilePic.tsx index 2691a8cc5..04a9fab0b 100644 --- a/modules/portfolio/component/portfolioSettingsComponents/UpdatingProfilePic.tsx +++ b/modules/portfolio/component/portfolioSettingsComponents/UpdatingProfilePic.tsx @@ -14,12 +14,15 @@ import { API_BASE_URL } from '../../../../http/checkout'; const UpdatingProfilePic = ({userId}:{userId: string}) => { const queryClient = useQueryClient(); - + const [selectedPics, setSelectedPics] = React.useState(''); const [reload, setReload] = React.useState(false); - + + /** LINES 21 - 35 WAS COPIED to TopBar.tsx Component, from LINE 53. + * If anything changes in the below lines(21-35), make sure it is changed in TopBar.tsx + */ const baseUrl = `${API_BASE_URL}/portfolio/` as string; - + const { data: userData, isLoading: isUserDataLoading,