Skip to content

Commit

Permalink
fix: queryClient.resetQueries after mutation success
Browse files Browse the repository at this point in the history
  • Loading branch information
a-honey committed Feb 29, 2024
1 parent 1862efa commit cd71368
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 6 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"@emotion/react": "^11.11.3",
"@emotion/styled": "^11.11.0",
"@tanstack/react-query": "^5.18.1",
"@tanstack/react-query-devtools": "^5.24.1",
"@typescript-eslint/parser": "^6.19.0",
"axios": "^1.6.5",
"dayjs": "^1.11.10",
Expand Down
1 change: 1 addition & 0 deletions src/components/organisms/FridgeBoard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ const FridgeBoard: React.FC<{ currentFridgeInfo: CurrentFridgeInfoType }> = ({ c
>
<ModalBody padding={0}>
<IngredientModal
currentFridgeInfo={currentFridgeInfo}
isDetailModal
id={detailIngredientId}
ingredientsRefetch={ingredientsRefetch}
Expand Down
16 changes: 13 additions & 3 deletions src/components/organisms/IngredientModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
usePostIngredient,
} from '@/hooks/queries/fridge';

import type { CurrentFridgeInfoType } from '@/types/fridge';
import Image from 'next/image';
import ModalContainer from '../atoms/ModalContainer';
import type { PostIngredientBodyType } from '@/hooks/queries/fridge/usePostIngredient';
Expand All @@ -25,7 +26,16 @@ const IngredientModal: React.FC<{
categoryImage?: string;
category?: string;
toggleIsOpenIngredientModal: () => void;
}> = ({ id, categoryImage, toggleIsOpenIngredientModal, isDetailModal = false, category, ingredientsRefetch }) => {
currentFridgeInfo?: CurrentFridgeInfoType;
}> = ({
id,
categoryImage,
currentFridgeInfo,
toggleIsOpenIngredientModal,
isDetailModal = false,
category,
ingredientsRefetch,
}) => {
const router = useRouter();
const today = new Date();

Expand Down Expand Up @@ -60,11 +70,11 @@ const IngredientModal: React.FC<{
isDeleted: false,
});

const deleteIngredient = useDeleteIngredientById(id, Number(fridgeid), reqBody?.location, () => {
const deleteIngredient = useDeleteIngredientById(id, currentFridgeInfo?.fridgeId as number, reqBody?.location, () => {
ingredientsRefetch();
toggleIsOpenIngredientModal();
});
const putIngredient = usePutIngredientById(id, Number(fridgeid), reqBody?.location, () => {
const putIngredient = usePutIngredientById(id, currentFridgeInfo?.fridgeId as number, reqBody?.location, () => {
ingredientsRefetch();
toggleIsOpenIngredientModal();
});
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/queries/fridge/useDeleteIngredientById.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { useBaseMutation } from '../useBaseMutation';
const useDeleteIngredientById = (id: number, fridgeId: number, location: string, fn?: () => void) => {
const onSuccess = () => {
if (fn) fn();
void queryClient.invalidateQueries();
void queryClient.resetQueries({ queryKey: queryKeys.MY_FRIDGE_CONTENT(fridgeId, location), exact: true });
};
return useBaseMutation(queryKeys.MY_FRIDGE_CONTENT(fridgeId, location), `/ingrs/detail/${id}`, onSuccess, 'DELETE');
};
Expand Down
4 changes: 2 additions & 2 deletions src/hooks/queries/fridge/usePutIngredientById.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ export interface FridgeBodyType {
const usePutIngredientById = (id: number, fridgeId: number, location: string, fn?: () => void) => {
const onSuccess = () => {
if (fn) fn();
void queryClient.invalidateQueries();
void queryClient.resetQueries({ queryKey: queryKeys.MY_FRIDGE_CONTENT(fridgeId, location), exact: true });
};
return useBaseMutation<FridgeBodyType>(
[...queryKeys.MY_FRIDGE_CONTENT(fridgeId, location), ...queryKeys.MY_INGREDIENT_ID(id)],
queryKeys.MY_FRIDGE_CONTENT(fridgeId, location),
`/ingrs/detail/${id}`,
onSuccess,
'PUT',
Expand Down
3 changes: 3 additions & 0 deletions src/pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import React from 'react';
import { RecoilRoot } from 'recoil';
import dayjs from 'dayjs';

// import { ReactQueryDevtools } from '@tanstack/react-query-devtools';

dayjs.locale('ko');

const theme = extendTheme({
Expand Down Expand Up @@ -50,6 +52,7 @@ export default function App({ Component, pageProps }: AppProps): JSX.Element {
</ErrorBoundary>
)}
</QueryErrorResetBoundary>
{/* <ReactQueryDevtools initialIsOpen={false} /> */}
</QueryClientProvider>
);
}
12 changes: 12 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3971,6 +3971,18 @@
resolved "https://registry.yarnpkg.com/@tanstack/query-core/-/query-core-5.18.1.tgz#b653ee354b7f4712d53565ccc5c6d8fb83ec866c"
integrity sha512-fYhrG7bHgSNbnkIJF2R4VUXb4lF7EBiQjKkDc5wOlB7usdQOIN4LxxHpDxyE3qjqIst1WBGvDtL48T0sHJGKCw==

"@tanstack/[email protected]":
version "5.24.0"
resolved "https://registry.yarnpkg.com/@tanstack/query-devtools/-/query-devtools-5.24.0.tgz#b9b7828d42d5034415b1973ff4a154e880b17d59"
integrity sha512-pThim455t69zrZaQKa7IRkEIK8UBTS+gHVAdNfhO72Xh4rWpMc63ovRje5/n6iw63+d6QiJzVadsJVdPoodSeQ==

"@tanstack/react-query-devtools@^5.24.1":
version "5.24.1"
resolved "https://registry.yarnpkg.com/@tanstack/react-query-devtools/-/react-query-devtools-5.24.1.tgz#e3a8ea71115fb899119126e1507fc340ee9d9496"
integrity sha512-qa4SEugN+EF8JJXcpsM9Lu05HfUv5cvHvLuB0uw/81eJZyNHFdtHFBi5RLCgpBrOyVMDfH8UQ3VBMqXzFKV68A==
dependencies:
"@tanstack/query-devtools" "5.24.0"

"@tanstack/react-query@^5.18.1":
version "5.18.1"
resolved "https://registry.yarnpkg.com/@tanstack/react-query/-/react-query-5.18.1.tgz#fd4e7b87260e82c5277355ad64f0e431a9302e02"
Expand Down

0 comments on commit cd71368

Please sign in to comment.