From 8981bcb4b84d06590bada56351ed62004591e177 Mon Sep 17 00:00:00 2001 From: Aaron William Po Date: Sun, 9 Apr 2023 11:25:10 -0400 Subject: [PATCH] Replace useSWR with useSWRInfinite to facilitate infinite scrolling --- components/BeerById/BeerCommentForm.tsx | 10 +- .../BeerPostCommentsPaginationBar.tsx | 54 -------- .../BeerById/BeerPostCommentsSection.tsx | 38 +++--- components/BeerById/BeerPostLikeButton.tsx | 11 +- components/BeerById/CommentCardBody.tsx | 121 +++++++++--------- components/ui/Navbar.tsx | 2 +- contexts/userContext.ts | 4 +- hooks/useBeerPostComments.ts | 21 ++- pages/beers/[id]/index.tsx | 42 +++--- pages/user/current.tsx | 2 +- 10 files changed, 128 insertions(+), 177 deletions(-) delete mode 100644 components/BeerById/BeerPostCommentsPaginationBar.tsx diff --git a/components/BeerById/BeerCommentForm.tsx b/components/BeerById/BeerCommentForm.tsx index 9164279..780ceb1 100644 --- a/components/BeerById/BeerCommentForm.tsx +++ b/components/BeerById/BeerCommentForm.tsx @@ -8,9 +8,9 @@ import { Rating } from 'react-daisyui'; import { useForm, SubmitHandler } from 'react-hook-form'; import { z } from 'zod'; -import { KeyedMutator } from 'swr'; -import BeerCommentQueryResult from '@/services/BeerComment/schema/BeerCommentQueryResult'; + import { useRouter } from 'next/router'; +import useBeerPostComments from '@/hooks/useBeerPostComments'; import Button from '../ui/forms/Button'; import FormError from '../ui/forms/FormError'; import FormInfo from '../ui/forms/FormInfo'; @@ -18,12 +18,10 @@ import FormLabel from '../ui/forms/FormLabel'; import FormSegment from '../ui/forms/FormSegment'; import FormTextArea from '../ui/forms/FormTextArea'; + interface BeerCommentFormProps { beerPost: z.infer; - mutate: KeyedMutator<{ - comments: z.infer[]; - pageCount: number; - }>; + mutate: ReturnType['mutate'] } const BeerCommentForm: FunctionComponent = ({ diff --git a/components/BeerById/BeerPostCommentsPaginationBar.tsx b/components/BeerById/BeerPostCommentsPaginationBar.tsx deleted file mode 100644 index 7bfa8bb..0000000 --- a/components/BeerById/BeerPostCommentsPaginationBar.tsx +++ /dev/null @@ -1,54 +0,0 @@ -import { FC } from 'react'; -import Link from 'next/link'; -import beerPostQueryResult from '@/services/BeerPost/schema/BeerPostQueryResult'; -import { z } from 'zod'; - -import { FaArrowLeft, FaArrowRight } from 'react-icons/fa'; - -interface BeerCommentsPaginationBarProps { - commentsPageNum: number; - commentsPageCount: number; - beerPost: z.infer; -} - -const BeerCommentsPaginationBar: FC = ({ - commentsPageNum, - commentsPageCount, - beerPost, -}) => ( -
-
- - - - - - - -
-
-); - -export default BeerCommentsPaginationBar; diff --git a/components/BeerById/BeerPostCommentsSection.tsx b/components/BeerById/BeerPostCommentsSection.tsx index 1f9d167..a60559e 100644 --- a/components/BeerById/BeerPostCommentsSection.tsx +++ b/components/BeerById/BeerPostCommentsSection.tsx @@ -8,7 +8,7 @@ import { z } from 'zod'; import useBeerPostComments from '@/hooks/useBeerPostComments'; import { useRouter } from 'next/router'; import BeerCommentForm from './BeerCommentForm'; -import BeerCommentsPaginationBar from './BeerPostCommentsPaginationBar'; + import CommentCardBody from './CommentCardBody'; import NoCommentsCard from './NoCommentsCard'; import CommentLoadingCardBody from './CommentLoadingCardBody'; @@ -22,13 +22,14 @@ const BeerPostCommentsSection: FC = ({ beerPost }) const router = useRouter(); const { id } = beerPost; const pageNum = parseInt(router.query.comments_page as string, 10) || 1; - const pageSize = 5; + const PAGE_SIZE = 6; - const { comments, commentsPageCount, isLoading, mutate } = useBeerPostComments({ - id, - pageNum, - pageSize, - }); + const { comments, isLoading, mutate, setSize, size, isLoadingMore } = + useBeerPostComments({ + id, + pageNum, + pageSize: PAGE_SIZE, + }); return (
@@ -44,17 +45,20 @@ const BeerPostCommentsSection: FC = ({ beerPost })
- {comments && !!comments.length && !!commentsPageCount && !isLoading && ( + {comments && !!comments.length && !isLoading && (
{comments.map((comment) => ( ))} - + {isLoadingMore && + Array.from({ length: PAGE_SIZE }).map((_, i) => ( + + ))} + +
)} @@ -62,15 +66,9 @@ const BeerPostCommentsSection: FC = ({ beerPost }) {isLoading && (
- {Array.from({ length: pageSize }).map((_, i) => ( + {Array.from({ length: PAGE_SIZE }).map((_, i) => ( ))} - -
)} diff --git a/components/BeerById/BeerPostLikeButton.tsx b/components/BeerById/BeerPostLikeButton.tsx index 07311ad..68e4285 100644 --- a/components/BeerById/BeerPostLikeButton.tsx +++ b/components/BeerById/BeerPostLikeButton.tsx @@ -2,11 +2,13 @@ import useCheckIfUserLikesBeerPost from '@/hooks/useCheckIfUserLikesBeerPost'; import sendLikeRequest from '@/requests/sendLikeRequest'; import { FC, useEffect, useState } from 'react'; import { FaThumbsUp, FaRegThumbsUp } from 'react-icons/fa'; -import { KeyedMutator } from 'swr'; + + +import useGetLikeCount from '@/hooks/useGetLikeCount'; const BeerPostLikeButton: FC<{ beerPostId: string; - mutateCount: KeyedMutator; + mutateCount: ReturnType['mutate']; }> = ({ beerPostId, mutateCount }) => { const { isLiked, mutate: mutateLikeStatus } = useCheckIfUserLikesBeerPost(beerPostId); const [loading, setLoading] = useState(true); @@ -30,9 +32,8 @@ const BeerPostLikeButton: FC<{ return ( - - ) : ( -
  • + + ) : ( + -
  • - )} + + )} + ); }; -const CommentCardBody: React.FC<{ - comment: z.infer; +const CommentCardBody: FC + = ({ comment, mutate }) => { + const { user } = useContext(UserContext); - mutate: KeyedMutator<{ - comments: z.infer[]; - pageCount: number; - }>; -}> = ({ comment, mutate }) => { - const { user } = useContext(UserContext); + const timeDistance = useTimeDistance(new Date(comment.createdAt)); - const timeDistance = useTimeDistance(new Date(comment.createdAt)); + return ( +
    +
    +
    +

    + + {comment.postedBy.username} + +

    +

    + posted{' '} + {' '} + ago +

    +
    - return ( -
    -
    -
    -

    - - {comment.postedBy.username} - -

    -

    - posted{' '} - {' '} - ago -

    + {user && }
    - {user && } +
    + + {Array.from({ length: 5 }).map((val, index) => ( + + ))} + +

    {comment.content}

    +
    - -
    - - {Array.from({ length: 5 }).map((val, index) => ( - - ))} - -

    {comment.content}

    -
    -
    - ); -}; + ); + }; export default CommentCardBody; diff --git a/components/ui/Navbar.tsx b/components/ui/Navbar.tsx index 47ebece..8a10573 100644 --- a/components/ui/Navbar.tsx +++ b/components/ui/Navbar.tsx @@ -68,7 +68,7 @@ const Navbar = () => {
    -
    +