Skip to content

Commit

Permalink
Merge pull request #202 from BCSDLab/feature/#196
Browse files Browse the repository at this point in the history
feat: queryKey 파일 분리
  • Loading branch information
hyejun0228 authored Apr 7, 2024
2 parents 3e6f599 + bf44d8d commit 4cdcea9
Show file tree
Hide file tree
Showing 11 changed files with 47 additions and 29 deletions.
2 changes: 1 addition & 1 deletion src/page/MyShopPage/components/EditShopInfoModal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { OwnerShop } from 'model/shopInfo/ownerShop';
import { zodResolver } from '@hookform/resolvers/zod';
import { useMutation } from '@tanstack/react-query';
import { putShop } from 'api/shop';
import useShopCategory from 'query/shopCategory';
import useShopCategory from 'query/shop';
import useBooleanState from 'utils/hooks/useBooleanState';
import CustomModal from 'component/common/CustomModal';
import OperateTimePC from 'page/ShopRegistration/component/Modal/OperateTimePC';
Expand Down
4 changes: 2 additions & 2 deletions src/page/ShopRegistration/component/Modal/Category/index.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import useShopCategory from 'query/shopCategory';
import useMyShop from 'query/shop';
import cn from 'utils/ts/className';
import { Category as CategoryProps } from 'model/category/storeCategory';
import useShopRegistrationStore from 'store/shopRegistration';
import styles from './Category.module.scss';

export default function Category() {
const { categoryList } = useShopCategory();
const { categoryList } = useMyShop();
const { category, setCategory, setCategoryId } = useShopRegistrationStore();

const handleCategoryClick = (categoryInfo: CategoryProps) => {
Expand Down
4 changes: 2 additions & 2 deletions src/page/ShopRegistration/view/Mobile/ShopCategory/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import useStepStore from 'store/useStepStore';
import useShopCategory from 'query/shopCategory';
import useMyShop from 'query/shop';
import cn from 'utils/ts/className';
import { Category as CategoryProps } from 'model/category/storeCategory';
import useShopRegistrationStore from 'store/shopRegistration';
Expand All @@ -10,7 +10,7 @@ import styles from './ShopCategory.module.scss';

export default function ShopCategory() {
const [isError, setIsError] = useState(false);
const { categoryList } = useShopCategory();
const { categoryList } = useMyShop();
const { increaseStep } = useStepStore();
const {
category, setCategory, setCategoryId,
Expand Down
4 changes: 4 additions & 0 deletions src/query/KeyFactory/menuKeys.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const menuKeys = {
all: ['menu'] as const,
menuInfo: (menuId: number) => [...menuKeys.all, 'menuInfo', menuId] as const,
};
8 changes: 8 additions & 0 deletions src/query/KeyFactory/registerKeys.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export const registerKeys = {
all: ['register'] as const,
fileUrlList: ['fileUrlList'] as const,
registerUser: ['registerUser'] as const,
emailCheck: (email: string) => [...registerKeys.all, 'emailDuplicateCheck', email] as const,
authCode: (email: string) => [...registerKeys.all, 'generateEmailAuthCode', email] as const,
verificationCode: (code: string, email: string) => [...registerKeys.all, 'verificationCode', code, email] as const,
};
7 changes: 7 additions & 0 deletions src/query/KeyFactory/shopKeys.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export const shopKeys = {
all: ['shop'] as const,
shopCategoryInfo: ['shopCategory'] as const,
myShopList: (myShopQueryKey: string | undefined) => [...shopKeys.all, 'myShop', myShopQueryKey] as const,
myShopInfo: (shopId: number) => [...shopKeys.all, 'myShopInfo', shopId] as const,
myMenuInfo: (shopId: number) => [...shopKeys.all, 'myMenuInfo', shopId] as const,
};
4 changes: 3 additions & 1 deletion src/query/menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
import { getMenu, modifyMenu, deleteMenu } from 'api/shop';
import { NewMenu } from 'model/shopInfo/newMenu';
import useAddMenuStore from 'store/addMenu';
import { shopKeys } from './KeyFactory/shopKeys';

const useMenuInfo = (menuId:number) => {
const { resetAddMenuStore } = useAddMenuStore();
const queryClient = useQueryClient();
const { data: menuData } = useQuery(
{
queryKey: ['menuInfo', menuId],
queryKey: shopKeys.myMenuInfo(menuId),
queryFn: () => getMenu(menuId),
},
);
Expand All @@ -32,6 +33,7 @@ export const useDeleteMenu = () => {
queryClient.invalidateQueries();
},
});

return {
deleteMenuMutation,
};
Expand Down
11 changes: 6 additions & 5 deletions src/query/register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ import useRegisterInfo from 'store/registerStore';
import useShopRegistrationStore from 'store/shopRegistration';
import useUploadToken from 'store/uploadToken';
import showToast from 'utils/ts/showToast';
import { registerKeys } from './KeyFactory/registerKeys';

export const useCheckDuplicate = (email:string) => {
const { status, refetch, error } = useQuery({
queryKey: ['emailDuplicateCheck', email],
queryKey: registerKeys.emailCheck(email),
queryFn: () => getEmailDuplicate(email),
enabled: false,
});
Expand All @@ -22,7 +23,7 @@ export const useGenerateAuthCode = (email:string) => {
const {
status, refetch, isError, error,
} = useQuery({
queryKey: ['generateEmailAuthCode', email],
queryKey: registerKeys.authCode(email),
queryFn: () => getEmailAuthCode({ address: email }),
enabled: false,
});
Expand All @@ -36,7 +37,7 @@ export const useVerificationAuthCode = (code:string, email:string) => {
const {
status, refetch, isError, error, data,
} = useQuery({
queryKey: ['verificationCode', code],
queryKey: registerKeys.verificationCode(code, email),
queryFn: () => verificationAuthCode({ certification_code: code, address: email }),
enabled: false,
});
Expand All @@ -50,7 +51,7 @@ export const useRegisterUser = (goNext:()=>void) => {
const { userInfo, ownerInfo, resetRegisterInfo } = useRegisterInfo();
const { shopId, name } = useShopRegistrationStore();
const register = useMutation({
mutationKey: ['registerUser'],
mutationKey: registerKeys.registerUser,
mutationFn: async (fileUrls:string[]) => (
registerUser(await parseRegisterData(
userInfo,
Expand All @@ -77,7 +78,7 @@ export const useGetFileUrls = (goNext:()=>void) => {
formData.append('files', file);
});
const fileMutation = useMutation({
mutationKey: ['getFileUrls'],
mutationKey: registerKeys.fileUrlList,
mutationFn: async () => {
try {
const data = await getFileUrls(formData, uploadToken!);
Expand Down
17 changes: 12 additions & 5 deletions src/query/shop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,37 @@ import {
import useUserStore from 'store/user';
import useAddMenuStore from 'store/addMenu';
import { NewMenu } from 'model/shopInfo/newMenu';
import getShopCategory from 'api/category';
import { shopKeys } from './KeyFactory/shopKeys';

const useMyShop = () => {
const myShopQueryKey = useUserStore.getState().user?.company_number;
const queryClient = useQueryClient();
const { resetAddMenuStore } = useAddMenuStore();
const { data: myShop } = useSuspenseQuery({
queryKey: ['myShop', myShopQueryKey],
queryKey: shopKeys.myShopList(myShopQueryKey),
queryFn: () => getMyShopList(),
});

const shopId = myShop.shops[0]?.id;

const { data: shopData, refetch: refetchShopData, isLoading } = useQuery({
queryKey: ['myShopInfo', shopId],
queryKey: shopKeys.myShopInfo(shopId),
queryFn: () => getShopInfo({ id: shopId }),
enabled: !!shopId,
});

const { data: menusData } = useQuery({
queryKey: ['myMenuInfo', shopId],
queryKey: shopKeys.myMenuInfo(shopId),
queryFn: () => getMenuInfoList({ id: shopId }),
enabled: !!shopId,
});

const { data: categoryList } = useQuery({
queryKey: shopKeys.shopCategoryInfo,
queryFn: () => getShopCategory(),
});

const { mutate: addMenuMutation, isError: addMenuError } = useMutation({
mutationFn: (param: NewMenu) => {
if (typeof shopId === 'number') {
Expand All @@ -40,12 +47,12 @@ const useMyShop = () => {
},
onSuccess: () => {
resetAddMenuStore();
queryClient.invalidateQueries({ queryKey: ['myMenuInfo', shopId] });
queryClient.invalidateQueries({ queryKey: shopKeys.myMenuInfo(shopId) });
},
});

return {
shopData, menusData, addMenuMutation, addMenuError, refetchShopData, isLoading,
shopData, menusData, addMenuMutation, addMenuError, refetchShopData, isLoading, categoryList,
};
};

Expand Down
12 changes: 0 additions & 12 deletions src/query/shopCategory.ts

This file was deleted.

3 changes: 2 additions & 1 deletion src/query/shops.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { useQuery } from '@tanstack/react-query';
import { getShopList } from 'api/shop';
import { shopKeys } from './KeyFactory/shopKeys';

const useShopList = () => {
const { data: shopList, isError } = useQuery({
queryKey: ['allshops'],
queryKey: shopKeys.all,
queryFn: () => getShopList(),
});
return { shopList, isError };
Expand Down

0 comments on commit 4cdcea9

Please sign in to comment.