Skip to content

Commit

Permalink
[#165] Feat: pull to refresh 적용 (#166)
Browse files Browse the repository at this point in the history
  • Loading branch information
ienrum authored Jul 14, 2024
1 parent 8e8fdee commit 4f9d120
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 63 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"react": "^18",
"react-dom": "^18",
"react-hook-form": "^7.51.4",
"react-simple-pull-to-refresh": "^1.3.3",
"react-spinners": "^0.14.1",
"react-toastify": "^10.0.5",
"swiper": "^11.1.1",
Expand Down
13 changes: 13 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 21 additions & 6 deletions src/app/(post)/result/[postId]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,33 @@
'use client';

import { useQueryClient } from '@tanstack/react-query';
import { useParams } from 'next/navigation';
import PullToRefresh from 'react-simple-pull-to-refresh';

import CommentList from './_components/Comments/CommentList';
import PostingMain from './_components/Posting';
import PostingButton from './_components/Posting/PostingButton';

const ResultPage = () => {
const { postId } = useParams() as { postId: string };
const queryClient = useQueryClient();

const handleRefresh = async () => {
await queryClient.invalidateQueries({ queryKey: ['posts', postId] });
await queryClient.invalidateQueries({ queryKey: ['votes', postId] });
await queryClient.invalidateQueries({ queryKey: ['comments', postId] });
};

return (
<div className='flex h-full grow flex-col gap-4 overflow-y-auto'>
<div className='flex flex-col gap-[36px]'>
<PostingMain />
<PostingButton />
<PullToRefresh onRefresh={handleRefresh} pullingContent=''>
<div className='flex h-full grow flex-col gap-4 overflow-y-auto'>
<div className='flex flex-col gap-[36px]'>
<PostingMain />
<PostingButton />
</div>
<CommentList />
</div>
<CommentList />
</div>
</PullToRefresh>
);
};

Expand Down
44 changes: 27 additions & 17 deletions src/app/(post)/search/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import React, { useState } from 'react';

import Link from 'next/link';
import { useSearchParams } from 'next/navigation';
import PullToRefresh from 'react-simple-pull-to-refresh';

import { useGetPostListAPI } from '@/src/apis/postList';
import { CATEGORY_MAP } from '@/src/app/(main)/constants';
Expand All @@ -22,10 +23,11 @@ const SearchPostPage = () => {
const channel = (searchParams.get('channel') as ChannelType | null) ?? 'all';

const [keyword, setKeyword] = useState('');
const { data: posts, fetchNextPage } = useGetPostListAPI(
keyword,
CATEGORY_MAP[channel],
);
const {
data: posts,
fetchNextPage,
refetch,
} = useGetPostListAPI(keyword, CATEGORY_MAP[channel]);

const scrollRef = useInfiniteScroll(fetchNextPage);

Expand All @@ -37,6 +39,10 @@ const SearchPostPage = () => {
setKeyword(input);
};

const handleRefresh = async () => {
await refetch();
};

return (
<>
<TopBar.Container>
Expand All @@ -48,19 +54,23 @@ const SearchPostPage = () => {
<>
<ChipContainer currentChannel={channel} defaultPath='/search' />
<main className='flex h-full grow flex-col overflow-y-scroll'>
{posts.length ? (
posts.map((post, index) => (
<Link
href={`/result/${post.postId}`}
key={`${index}-${post.voteTitle}`}
>
<PostItem post={post} />
</Link>
))
) : (
<EmptyPage href='/createPost' text='작성 글이 없습니다.' />
)}
<div ref={scrollRef} />
<PullToRefresh onRefresh={handleRefresh} pullingContent=''>
<ul>
{posts.length ? (
posts.map((post, index) => (
<Link
href={`/result/${post.postId}`}
key={`${index}-${post.voteTitle}`}
>
<PostItem post={post} />
</Link>
))
) : (
<EmptyPage href='/createPost' text='작성 글이 없습니다.' />
)}
<div ref={scrollRef} />
</ul>
</PullToRefresh>
</main>
</>
)}
Expand Down
42 changes: 26 additions & 16 deletions src/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import Image from 'next/image';
import Link from 'next/link';
import { useSearchParams } from 'next/navigation';
import PullToRefresh from 'react-simple-pull-to-refresh';

import { useGetPostListAPI } from '@/src/apis/postList';
import { CATEGORY_MAP } from '@/src/app/(main)/constants';
Expand All @@ -22,12 +23,17 @@ const Main = () => {
const channel = (searchParams.get('channel') as ChannelType | null) ?? 'all';
const keyword = searchParams.get('keyword') as string;

const { data: posts, fetchNextPage } = useGetPostListAPI(
keyword,
CATEGORY_MAP[channel],
);
const {
data: posts,
fetchNextPage,
refetch,
} = useGetPostListAPI(keyword, CATEGORY_MAP[channel]);
const scrollRef = useInfiniteScroll(fetchNextPage);

const handleRefresh = async () => {
await refetch();
};

return (
<>
<TopBar.Container>
Expand All @@ -38,18 +44,22 @@ const Main = () => {
</TopBar.Container>
<ChipContainer currentChannel={channel} />
<main className='flex h-full grow flex-col overflow-y-scroll'>
{posts.length ? (
posts.map((post, index) => (
<Link
href={`/result/${post.postId}`}
key={`${index}-${post.voteTitle}`}
>
<PostItem post={post} />
</Link>
))
) : (
<EmptyPage href='/createPost' text='작성 글이 없습니다.' />
)}
<PullToRefresh onRefresh={handleRefresh} pullingContent=''>
<ul>
{posts.length ? (
posts.map((post, index) => (
<Link
href={`/result/${post.postId}`}
key={`${index}-${post.voteTitle}`}
>
<PostItem post={post} />
</Link>
))
) : (
<EmptyPage href='/createPost' text='작성 글이 없습니다.' />
)}
</ul>
</PullToRefresh>

<div ref={scrollRef} />
</main>
Expand Down
36 changes: 24 additions & 12 deletions src/app/users/[userId]/_components/PostingList.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { useQueryClient } from '@tanstack/react-query';
import Link from 'next/link';
import { useParams } from 'next/navigation';
import PullToRefresh from 'react-simple-pull-to-refresh';

import { useGetProfileAPI } from '@/src/apis/profile';
import PostItem from '@/src/app/_components/PostItem';
Expand All @@ -10,22 +12,32 @@ const Posting = () => {
const { userId } = useParams() as { userId: string };
const { posts } = useGetProfileAPI(userId);

const queryClient = useQueryClient();

const handleRefresh = async () => {
await queryClient.invalidateQueries({ queryKey: ['profile', userId] });
};

return (
<div className='flex h-full flex-col'>
<span className='font-h4-sm px-4 text-gray-accent3'>작성 글</span>
<div className='flex grow flex-col'>
{posts.length ? (
posts.map((post, index) => (
<Link
href={`/result/${post.postId}`}
key={`${index}-${post.voteTitle}`}
>
<PostItem post={post} isNickname={false} />
</Link>
))
) : (
<EmptyPage text={NO_POSTING_TEXT} />
)}
<PullToRefresh onRefresh={handleRefresh} pullingContent=''>
<ul>
{posts.length ? (
posts.map((post, index) => (
<Link
href={`/result/${post.postId}`}
key={`${index}-${post.voteTitle}`}
>
<PostItem post={post} isNickname={false} />
</Link>
))
) : (
<EmptyPage text={NO_POSTING_TEXT} />
)}
</ul>
</PullToRefresh>
</div>
</div>
);
Expand Down
35 changes: 23 additions & 12 deletions src/app/users/profile/(posting)/_components/PostingList/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { useQueryClient } from '@tanstack/react-query';
import Link from 'next/link';
import PullToRefresh from 'react-simple-pull-to-refresh';

import PostItem from '@/src/app/_components/PostItem';
import EmptyPage from '@/src/components/EmptyPage';
Expand All @@ -13,21 +15,30 @@ const PostingList = ({
isNickname?: boolean;
}) => {
const posts = useGetPostHistory();
const queryClient = useQueryClient();

const handleRefresh = async () => {
await queryClient.invalidateQueries({ queryKey: ['postList'] });
};

return (
<div className='flex h-full flex-col'>
{posts.length ? (
posts.map((post, index) => (
<Link
href={`/result/${post.postId}`}
key={`${index}-${post.voteTitle}`}
>
<PostItem post={post} isNickname={isNickname} />
</Link>
))
) : (
<EmptyPage text={text} />
)}
<PullToRefresh onRefresh={handleRefresh} pullingContent=''>
<ul>
{posts.length ? (
posts.map((post, index) => (
<Link
href={`/result/${post.postId}`}
key={`${index}-${post.voteTitle}`}
>
<PostItem post={post} isNickname={isNickname} />
</Link>
))
) : (
<EmptyPage text={text} />
)}
</ul>
</PullToRefresh>
</div>
);
};
Expand Down

0 comments on commit 4f9d120

Please sign in to comment.