diff --git a/src/components/UserPage/UserHeader.tsx b/src/components/UserPage/UserHeader.tsx new file mode 100644 index 0000000..be41117 --- /dev/null +++ b/src/components/UserPage/UserHeader.tsx @@ -0,0 +1,50 @@ +import useTimeDistance from '@/hooks/utilities/useTimeDistance'; +import useGetUsersFollowedByUser from '@/hooks/data-fetching/user-follows/useGetUsersFollowedByUser'; +import useGetUsersFollowingUser from '@/hooks/data-fetching/user-follows/useGetUsersFollowingUser'; +import { FC } from 'react'; +import { z } from 'zod'; +import { format } from 'date-fns'; +import GetUserSchema from '@/services/User/schema/GetUserSchema'; +import UserAvatar from '../Account/UserAvatar'; + +interface UserHeaderProps { + user: z.infer; + followerCount: ReturnType['followerCount']; + followingCount: ReturnType['followingCount']; +} +const UserHeader: FC = ({ user, followerCount, followingCount }) => { + const timeDistance = useTimeDistance(new Date(user.createdAt)); + + return ( +
+
+
+ +
+ +
+

{user.username}

+
+ +
+ {followingCount} Following + {followerCount} Followers +
+ + + joined{' '} + {timeDistance && ( + + {`${timeDistance} ago`} + + )} + +
+
+ ); +}; + +export default UserHeader; diff --git a/src/hooks/data-fetching/user-follows/useGetUsersFollowedByUser.ts b/src/hooks/data-fetching/user-follows/useGetUsersFollowedByUser.ts index a09e78a..d3a3c4a 100644 --- a/src/hooks/data-fetching/user-follows/useGetUsersFollowedByUser.ts +++ b/src/hooks/data-fetching/user-follows/useGetUsersFollowedByUser.ts @@ -3,7 +3,7 @@ import APIResponseValidationSchema from '@/validation/APIResponseValidationSchem import useSWRInfinite from 'swr/infinite'; import { z } from 'zod'; -const useGetUsersFollowingUser = ({ +const useGetUsersFollowedByUser = ({ pageSize, userId, }: { @@ -61,4 +61,4 @@ const useGetUsersFollowingUser = ({ }; }; -export default useGetUsersFollowingUser; +export default useGetUsersFollowedByUser; diff --git a/src/pages/users/[id].tsx b/src/pages/users/[id].tsx index 795acef..a25bff7 100644 --- a/src/pages/users/[id].tsx +++ b/src/pages/users/[id].tsx @@ -1,13 +1,12 @@ import useMediaQuery from '@/hooks/utilities/useMediaQuery'; -import useTimeDistance from '@/hooks/utilities/useTimeDistance'; import findUserById from '@/services/User/findUserById'; import GetUserSchema from '@/services/User/schema/GetUserSchema'; -import { format } from 'date-fns'; -import { GetServerSideProps } from 'next'; + import Head from 'next/head'; import { FC } from 'react'; import { z } from 'zod'; -import UserAvatar from '@/components/Account/UserAvatar'; +import withPageAuthRequired from '@/util/withPageAuthRequired'; +import UserHeader from '@/components/UserPage/UserHeader'; import useGetUsersFollowedByUser from '@/hooks/data-fetching/user-follows/useGetUsersFollowedByUser'; import useGetUsersFollowingUser from '@/hooks/data-fetching/user-follows/useGetUsersFollowingUser'; @@ -15,8 +14,10 @@ interface UserInfoPageProps { user: z.infer; } -const UserHeader: FC<{ user: z.infer }> = ({ user }) => { - const timeDistance = useTimeDistance(new Date(user.createdAt)); +const UserInfoPage: FC = ({ user }) => { + // eslint-disable-next-line @typescript-eslint/no-unused-vars + const isDesktop = useMediaQuery('(min-width: 1024px)'); + const title = `${user.username} | The Biergarten App`; const { followingCount } = useGetUsersFollowedByUser({ userId: user.id, @@ -28,43 +29,6 @@ const UserHeader: FC<{ user: z.infer }> = ({ user }) => { pageSize: 10, }); - return ( -
-
-
- -
- -
-

{user.username}

-
- -
- {followingCount} Following - {followerCount} Followers -
- - - joined{' '} - {timeDistance && ( - - {`${timeDistance} ago`} - - )} - -
-
- ); -}; - -const UserInfoPage: FC = ({ user }) => { - // eslint-disable-next-line @typescript-eslint/no-unused-vars - const isDesktop = useMediaQuery('(min-width: 1024px)'); - const title = `${user.username} | The Biergarten App`; - return ( <> @@ -74,7 +38,32 @@ const UserInfoPage: FC = ({ user }) => { <>
- + + + {isDesktop ? ( +
+
+
+
+

About Me

+

{user.bio}

+
+
+
+ +
+
+
+
+
+
+ ) : ( + <> + )}
@@ -84,15 +73,12 @@ const UserInfoPage: FC = ({ user }) => { export default UserInfoPage; -export const getServerSideProps: GetServerSideProps = async ( - context, -) => { - const { id } = context.params!; - const user = await findUserById(id as string); - - if (!user) { - return { notFound: true }; - } - - return { props: { user: JSON.parse(JSON.stringify(user)) } }; -}; +export const getServerSideProps = withPageAuthRequired( + async (context) => { + const { id } = context.params!; + const user = await findUserById(id as string); + return user + ? { props: { user: JSON.parse(JSON.stringify(user)) } } + : { notFound: true }; + }, +);