Skip to content

Commit

Permalink
πŸ› query invalidation μ„€μ • (#345)
Browse files Browse the repository at this point in the history
### πŸ“ μž‘μ—… λ‚΄μš©

- mutation 이후 query invalidation을 μˆ˜ν–‰ν•©λ‹ˆλ‹€.
- λ‘œκ·Έμ•„μ›ƒ μ‹œ νŽ˜μ΄μ§€ λ¦¬λ‘œλ“œλ₯Ό μˆ˜ν–‰ν•˜μ—¬ 메인 νŽ˜μ΄μ§€μ˜ 뢁마크 ν‘œμ‹œκ°€ λ‘œκ·Έμ•„μ›ƒ μ΄ν›„μ—λŠ” λ‚˜νƒ€λ‚˜μ§€ μ•Šλ„λ‘ μ„€μ •ν•©λ‹ˆλ‹€.
  • Loading branch information
Yeonu-Kim authored Feb 6, 2025
1 parent 0edf58e commit 5849b36
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 14 deletions.
10 changes: 9 additions & 1 deletion apps/client/src/components/nav/GlobarNavigationBar.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useMutation } from '@tanstack/react-query';
import { useMutation, useQueryClient } from '@tanstack/react-query';

import { Button } from '@/components/ui/button';
import { useGuardContext } from '@/shared/context/hooks';
Expand Down Expand Up @@ -58,6 +58,8 @@ export const GlobalNavigationBar = () => {
const useLogout = () => {
const { authService } = useGuardContext(ServiceContext);
const { token } = useGuardContext(TokenContext);
const { refreshPage } = useRouteNavigation();
const queryClient = useQueryClient();

const { mutate: logout, isPending } = useMutation({
mutationFn: () => {
Expand All @@ -66,6 +68,12 @@ const useLogout = () => {
}
return authService.logout({ token });
},
onSuccess: async (response) => {
if (response.type === 'success') {
await queryClient.invalidateQueries();
refreshPage();
}
},
});

return { logout, isPending };
Expand Down
7 changes: 5 additions & 2 deletions apps/client/src/feature/company/ui/CreateCompanyForm.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useMutation } from '@tanstack/react-query';
import { useMutation, useQueryClient } from '@tanstack/react-query';
import { useState } from 'react';

import { ImageField } from '@/components/field/ImageField';
Expand Down Expand Up @@ -298,6 +298,8 @@ const useCreateCompanyWithUploads = ({
}) => {
const { fileService, postService } = useGuardContext(ServiceContext);
const { token } = useGuardContext(TokenContext);
const queryClient = useQueryClient();

const [isPending, setIsPending] = useState(false);
const { toMain } = useRouteNavigation();

Expand Down Expand Up @@ -387,8 +389,9 @@ const useCreateCompanyWithUploads = ({
}
return postService.createCompany({ token, companyContents });
},
onSuccess: (response) => {
onSuccess: async (response) => {
if (response.type === 'success') {
await queryClient.invalidateQueries();
toMain();
}
},
Expand Down
2 changes: 1 addition & 1 deletion apps/client/src/feature/landing/ui/PostCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ export const useAddBookmark = () => {
},
onSuccess: async (response) => {
if (response.type === 'success') {
await queryClient.invalidateQueries({ queryKey: ['postService'] });
await queryClient.invalidateQueries();
return;
} else {
// TODO: 뢁마크 생성 μ‹€νŒ¨ μ‹œ ν•˜λ‹¨μ— ν† μŠ€νŠΈ λ„μš°κΈ°
Expand Down
6 changes: 4 additions & 2 deletions apps/client/src/feature/post/ui/CreatePostForm.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useMutation } from '@tanstack/react-query';
import { useMutation, useQueryClient } from '@tanstack/react-query';
import { useState } from 'react';

import { MarkdownEditorField } from '@/components/field/MarkdownEditorField';
Expand Down Expand Up @@ -212,6 +212,7 @@ const useCreatePost = ({
}) => {
const { postService } = useGuardContext(ServiceContext);
const { token } = useGuardContext(TokenContext);
const queryClient = useQueryClient();

const { mutate: createPost, isPending } = useMutation({
mutationFn: ({
Expand All @@ -226,8 +227,9 @@ const useCreatePost = ({
}
return postService.createPost({ token, companyId, postContents: post });
},
onSuccess: (response) => {
onSuccess: async (response) => {
if (response.type === 'success') {
await queryClient.invalidateQueries();
onSuccess();
} else {
setResponseMessage(createErrorMessage(response.code));
Expand Down
6 changes: 4 additions & 2 deletions apps/client/src/feature/resume/ui/CreateResumeForm.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useMutation } from '@tanstack/react-query';
import { useMutation, useQueryClient } from '@tanstack/react-query';
import { useState } from 'react';

import { FormContainer } from '@/components/form';
Expand Down Expand Up @@ -174,6 +174,7 @@ const useCreateResume = ({
const { resumeService } = useGuardContext(ServiceContext);
const { token } = useGuardContext(TokenContext);
const { toPost } = useRouteNavigation();
const queryClient = useQueryClient();

const { mutate: createResume, isPending } = useMutation({
mutationFn: ({ resume }: { resume: ResumeRequest }) => {
Expand All @@ -186,8 +187,9 @@ const useCreateResume = ({
postId,
});
},
onSuccess: (response) => {
onSuccess: async (response) => {
if (response.type === 'success') {
await queryClient.invalidateQueries();
toPost({ postId });
} else {
setResponseMessage(response.code);
Expand Down
7 changes: 4 additions & 3 deletions apps/client/src/shared/auth/AuthProtectedRoute.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useMutation } from '@tanstack/react-query';
import { useMutation, useQueryClient } from '@tanstack/react-query';
import { useRef } from 'react';
import { Outlet } from 'react-router';

Expand All @@ -23,14 +23,15 @@ export const AuthProtectedRoute = () => {

const useRefreshToken = () => {
const { authService } = useGuardContext(ServiceContext);
const queryClient = useQueryClient();

const { mutate: reissueToken } = useMutation({
mutationFn: async () => {
const response = await authService.reissueAccessToken();
return response;
},
onSuccess: () => {
return;
onSuccess: async () => {
await queryClient.invalidateQueries();
},
onError: () => {
return;
Expand Down
7 changes: 4 additions & 3 deletions apps/client/src/shared/auth/CompanyProtectedRoute.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useMutation } from '@tanstack/react-query';
import { useMutation, useQueryClient } from '@tanstack/react-query';
import { useRef } from 'react';
import { Outlet } from 'react-router';

Expand Down Expand Up @@ -33,14 +33,15 @@ export const CompanyProtectedRoute = () => {

const useRefreshToken = () => {
const { authService } = useGuardContext(ServiceContext);
const queryClient = useQueryClient();

const { mutate: reissueToken } = useMutation({
mutationFn: async () => {
const response = await authService.reissueAccessToken();
return response;
},
onSuccess: () => {
return;
onSuccess: async () => {
await queryClient.invalidateQueries();
},
onError: () => {
return;
Expand Down

0 comments on commit 5849b36

Please sign in to comment.