Skip to content

Commit

Permalink
Use session data for sidebar. No need for another query
Browse files Browse the repository at this point in the history
  • Loading branch information
JiashuHarryHuang committed Nov 15, 2023
1 parent 93870e6 commit 057d57c
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 21 deletions.
23 changes: 3 additions & 20 deletions components/DesktopSidebar/UserIcon.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,10 @@ import {
UserIconContainer,
} from '@/styles/components/Sidebar/userIcon.styles';
import { Media } from '@/lib/media';
import { QueriedUserData } from 'bookem-shared/src/types/database';
import { useSession } from 'next-auth/react';

export const UserIcon = () => {
const [userData, setUserData] = useState<QueriedUserData | null>(null);
const [error, setError] = useState<Error>();

useEffect(() => {
try {
fetchData('/api/users/')
.then(data => {
if (data != null) {
console.log('Fetched data:', data);
setUserData(data);
}
})
.catch(err => setError(err));
} catch (error) {
setError(new Error('Error fetching user data'));
console.error('Error fetching user data:', error);
}
}, []);
const { data: session } = useSession();

return (
<UserIconContainer>
Expand All @@ -42,7 +25,7 @@ export const UserIcon = () => {
<Image src="/bookem-logo.png" width="73" height="73" alt="" />
</Media>
</ImageContainer>
<Name>{userData !== null && userData.name}</Name>
<Name>{session?.user && session.user.name}</Name>
</UserIconContainer>
);
};
1 change: 0 additions & 1 deletion pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ const HomePage = () => {
try {
fetchData('/api/users/')
.then(data => {
console.log('Fetched data:', data);
setUserData(data);
})
.catch(err => setError(err));
Expand Down

0 comments on commit 057d57c

Please sign in to comment.