Skip to content

Commit

Permalink
feat: Toast mutation에서 처리
Browse files Browse the repository at this point in the history
  • Loading branch information
hyeonjinan096 committed Jul 16, 2024
1 parent 069f628 commit 4f666ca
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 10 deletions.
11 changes: 9 additions & 2 deletions src/apis/createPost.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { useMutation, useQueryClient } from '@tanstack/react-query';

import Toast from '@/src/components/Toast';

import { api } from './core';

export interface PostRequestBody {
Expand All @@ -26,10 +28,15 @@ export const useCreatePostAPI = () => {
const queryClient = useQueryClient();
const { mutateAsync } = useMutation({
mutationFn: (data: PostRequestBody) => createPost(data),
onSuccess: () =>
onSuccess: () => {
queryClient.invalidateQueries({
queryKey: ['postList'],
}),
});
Toast.default('등록되었습니다.');
},
onError: () => {
Toast.default('등록에 실패했습니다.');
},
});

return mutateAsync;
Expand Down
10 changes: 9 additions & 1 deletion src/apis/logout.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { useMutation, useQueryClient } from '@tanstack/react-query';

import Toast from '@/src/components/Toast';

import { api } from './core';

const logout = () => {
Expand All @@ -13,7 +15,13 @@ export const useLogoutAPI = () => {

const { mutateAsync } = useMutation({
mutationFn: () => logout(),
onSuccess: () => queryClient.removeQueries(),
onSuccess: () => {
queryClient.removeQueries();
Toast.default('로그아웃 되었습니다.');
},
onError: () => {
Toast.default('로그아웃에 실패했습니다.');
},
});

return mutateAsync;
Expand Down
6 changes: 6 additions & 0 deletions src/apis/postDetail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import {
useSuspenseQuery,
} from '@tanstack/react-query';

import Toast from '@/src/components/Toast';

import { api } from './core';

interface PostDetailBody {
Expand Down Expand Up @@ -48,6 +50,10 @@ export const useDeletePostDetailAPI = (postId: string) => {
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: ['postList'] });
queryClient.invalidateQueries({ queryKey: ['posts', postId] });
Toast.default('게시글이 삭제되었습니다.');
},
onError: () => {
Toast.default('게시글 삭제에 실패했습니다.');
},
});
};
2 changes: 0 additions & 2 deletions src/app/(post)/createPost/_components/PostForm/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import Selector from '@/src/components/Selector';
import TextInput from '@/src/components/TextInput';
import Textarea from '@/src/components/Textarea';
import TimeInput from '@/src/components/TimeInput';
import Toast from '@/src/components/Toast';

import { formOptions } from './formSchema';
import { useCreatePost } from './useCreatePost';
Expand All @@ -39,7 +38,6 @@ const PostForm = () => {

const onSubmitPost = (data: PostRequestBody) => {
createPost(data);
Toast.default('검색 되었습니다.');
router.push('/');
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
useGetPostDetailAPI,
} from '@/src/apis/postDetail';
import ConfirmationModal from '@/src/components/Modal/ConfirmationModal';
import Toast from '@/src/components/Toast';
import TopBar from '@/src/components/Topbar';
import useModal from '@/src/hooks/useModal';

Expand All @@ -24,7 +23,6 @@ const PostDetailNavMore = () => {
await deletePost(undefined, {
onSuccess: () => {
router.push('/');
Toast.default('게시글이 삭제되었습니다.');
},
});
};
Expand Down
3 changes: 0 additions & 3 deletions src/app/users/profile/_components/Setting.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
import { useRouter } from 'next/navigation';

import Icon from '@/src/components/Icon';
import Toast from '@/src/components/Toast';

import { useLogout } from '../hooks/useLogout';

const Setting = () => {
const logout = useLogout();
const router = useRouter();

const handleLogout = () => {
logout();
router.push('/');
Toast.default('로그아웃 되었습니다.');
};

return (
Expand Down

0 comments on commit 4f666ca

Please sign in to comment.