Skip to content

Commit

Permalink
Merge pull request #156 from Dev-FE-1/refactor/toast-152
Browse files Browse the repository at this point in the history
[Refactor] 포스트 등록 후 toast 메세지 딜레이
  • Loading branch information
nakyeonko3 authored Sep 11, 2024
2 parents 9b72861 + d8ce866 commit 695953e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
21 changes: 18 additions & 3 deletions src/pages/Home.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import { useEffect, memo } from 'react';
import { useEffect, memo, useState } from 'react';

import { css } from '@emotion/react';
import { HiArrowUturnUp } from 'react-icons/hi2';
import { LuPartyPopper } from 'react-icons/lu';
import { useInView } from 'react-intersection-observer';
import { useLocation } from 'react-router-dom';

import FitButton from '@/components/common/buttons/FitButton';
import Spinner from '@/components/common/loading/Spinner';
import LogoHeader from '@/components/layout/header/LogoHeader';
import { PostsTimeLine } from '@/components/post/PostsTimeline';
import { useAuth } from '@/hooks/useAuth';
import { useFilteredPostsTimelinesQuery } from '@/hooks/useFilteredPostsTimelines';
import { useToastStore } from '@/stores/toastStore';
import theme from '@/styles/theme';

const MemoizedPostsTimeLine = memo(PostsTimeLine);
Expand Down Expand Up @@ -52,11 +54,24 @@ const LoadingMessage = ({

const HomePage = () => {
const user = useAuth();
const { data, fetchNextPage, hasNextPage, isFetchingNextPage, isLoading } =
const location = useLocation();
const [isNewPostLoading, setIsNewPostLoading] = useState(false);
const addToast = useToastStore((state) => state.addToast);
const { data, fetchNextPage, hasNextPage, isFetchingNextPage, isLoading, refetch } =
useFilteredPostsTimelinesQuery({
userId: user?.uid || '',
});

useEffect(() => {
if (location.state?.isNewPostCreated) {
setIsNewPostLoading(true);
refetch().then(() => {
setIsNewPostLoading(false);
addToast('새 포스트가 등록되었습니다.');
});
}
}, [location.state, refetch, addToast]);

const posts = data?.pages.flatMap((page) => page.posts) || [];
const { ref, inView } = useInView();

Expand All @@ -66,7 +81,7 @@ const HomePage = () => {
}
}, [inView, fetchNextPage, hasNextPage]);

if (isLoading) {
if (isLoading || isNewPostLoading) {
return <Spinner customStyle={spinnerStyle} />;
}

Expand Down
4 changes: 2 additions & 2 deletions src/pages/NewPost.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ const NewPost = () => {
{ playlistId, videoId, description },
{
onSuccess: () => {
navigate(`/`);
addToast('새 포스트가 등록되었습니다.');
addToast('포스트가 등록중입니다.');
navigate(`/`, { state: { isNewPostCreated: true } });
},
onError: (error) => {
console.error('포스트 및 플레이리스트 업데이트 실패: ', error);
Expand Down

0 comments on commit 695953e

Please sign in to comment.