From 1f817c8a79c5cb669d2f410c45dea4297d841058 Mon Sep 17 00:00:00 2001 From: MTG2000 Date: Mon, 18 Apr 2022 16:37:59 +0300 Subject: [PATCH] feat: added infinite scroll to feed page --- .../Posts/Components/PostsList/PostsList.tsx | 11 ++---- .../Posts/pages/FeedPage/FeedPage.tsx | 18 +++++++-- .../Posts/pages/FeedPage/feed.graphql | 4 +- src/graphql/index.tsx | 11 ++++-- src/mocks/data/posts.ts | 8 ++-- src/mocks/handlers.ts | 7 ++-- src/mocks/resolvers.ts | 8 ++-- src/utils/apollo.ts | 39 ++++++++++++++++++- src/utils/hooks/index.ts | 2 + src/utils/hooks/useInfiniteQuery.ts | 34 ++++++++++++++++ src/utils/hooks/useReachedBottom.ts | 17 +++++--- src/utils/interfaces/misc.interfaces.ts | 2 +- 12 files changed, 125 insertions(+), 36 deletions(-) create mode 100644 src/utils/hooks/useInfiniteQuery.ts diff --git a/src/features/Posts/Components/PostsList/PostsList.tsx b/src/features/Posts/Components/PostsList/PostsList.tsx index 383f97a..d983037 100644 --- a/src/features/Posts/Components/PostsList/PostsList.tsx +++ b/src/features/Posts/Components/PostsList/PostsList.tsx @@ -1,19 +1,14 @@ import { useCallback } from "react" import { Post } from "src/features/Posts/types" import { useReachedBottom } from "src/utils/hooks/useReachedBottom" -import { ListProps } from "src/utils/interfaces" +import { ListComponentProps } from "src/utils/interfaces" import PostCard, { PostCardSkeleton } from "../PostCard" -type Props = ListProps +type Props = ListComponentProps export default function PostsList(props: Props) { - - const reachedBottom = useCallback(() => { - console.log("NEW FETCH") - }, []) - - const { ref } = useReachedBottom(reachedBottom) + const { ref } = useReachedBottom(props.onReachedBottom) if (props.isLoading) return
diff --git a/src/features/Posts/pages/FeedPage/FeedPage.tsx b/src/features/Posts/pages/FeedPage/FeedPage.tsx index 81c2c89..c72d99d 100644 --- a/src/features/Posts/pages/FeedPage/FeedPage.tsx +++ b/src/features/Posts/pages/FeedPage/FeedPage.tsx @@ -1,15 +1,22 @@ import { useFeedQuery } from 'src/graphql' -import { MOCK_DATA } from 'src/mocks/data' +import { useInfiniteQuery } from 'src/utils/hooks' import PostsList from '../../Components/PostsList/PostsList' import TrendingCard from '../../Components/TrendingCard/TrendingCard' import PopularCategories from './PopularCategories/PopularCategories' import SortBy from './SortBy/SortBy' import styles from './styles.module.css' + export default function FeedPage() { - const feedQuery = useFeedQuery() + const feedQuery = useFeedQuery({ + variables: { + take: 10, + skip: 0 + } + }) + const { fetchMore, isFetchingMore } = useInfiniteQuery(feedQuery, 'getFeed') return (
- +