Skip to content

Commit cbb9de8

Browse files
authored
Merge pull request #14 from pythonkr/fix/shop-login-not-invalidate-components
2 parents 5270b6e + 7fdce9c commit cbb9de8

File tree

7 files changed

+96
-113
lines changed

7 files changed

+96
-113
lines changed

packages/shop/src/apis/index.ts

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,18 +44,31 @@ namespace ShopAPIs {
4444

4545
/**
4646
* 로그아웃합니다.
47-
* @throws 401 - 로그아웃이 성공할 시에도 항상 401 에러가 발생합니다.
4847
*/
49-
export const signOut = (client: ShopAPIClient) => () =>
50-
client.delete<ShopSchemas.UserSignedInStatus>("authn/social/browser/v1/auth/session");
48+
export const signOut = (client: ShopAPIClient) => async () => {
49+
try {
50+
await client.delete<ShopSchemas.UserSignedInStatus>("authn/social/browser/v1/auth/session");
51+
} catch (error) {
52+
}
53+
return Promise.resolve({});
54+
}
5155

5256
/**
5357
* 로그인 정보를 조회합니다.
5458
* @returns 로그인 정보
55-
* @throws 401 - 로그인 정보가 없습니다.
5659
*/
57-
export const retrieveUserInfo = (client: ShopAPIClient) => () =>
58-
client.get<ShopSchemas.UserSignedInStatus>("authn/social/browser/v1/auth/session");
60+
export const retrieveUserInfo = (client: ShopAPIClient) => async () => {
61+
try {
62+
const response = await client.get<ShopSchemas.UserSignedInStatus>("authn/social/browser/v1/auth/session");
63+
if (response.meta.is_authenticated) {
64+
return response;
65+
} else {
66+
throw new Error("User is not authenticated");
67+
}
68+
} catch (error) {
69+
}
70+
return Promise.resolve(null);
71+
}
5972

6073
/**
6174
* 노출 중인 모든 상품의 목록을 가져옵니다.

packages/shop/src/components/features/cart.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,9 @@ export const CartStatus: React.FC<{ onPaymentCompleted?: () => void }> = ({
6969
onPaymentCompleted,
7070
}) => {
7171
const queryClient = useQueryClient();
72-
const cartOrderStartMutation = ShopHooks.usePrepareCartOrderMutation();
73-
const removeItemFromCartMutation = ShopHooks.useRemoveItemFromCartMutation();
72+
const shopAPIClient = ShopHooks.useShopClient();
73+
const cartOrderStartMutation = ShopHooks.usePrepareCartOrderMutation(shopAPIClient);
74+
const removeItemFromCartMutation = ShopHooks.useRemoveItemFromCartMutation(shopAPIClient);
7475

7576
const removeItemFromCart = (cartProductId: string) =>
7677
removeItemFromCartMutation.mutate({ cartProductId });
@@ -100,7 +101,7 @@ export const CartStatus: React.FC<{ onPaymentCompleted?: () => void }> = ({
100101

101102
const WrappedShopCartList: React.FC = () => {
102103
// eslint-disable-next-line react-hooks/rules-of-hooks
103-
const { data } = ShopHooks.useCart();
104+
const { data } = ShopHooks.useCart(shopAPIClient);
104105

105106
return !data.hasOwnProperty("products") || data.products.length === 0 ? (
106107
<Typography variant="body1" color="error">

packages/shop/src/components/features/order.tsx

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,24 +31,23 @@ const PaymentHistoryStatusTranslated: {
3131
refunded: "환불됨",
3232
};
3333

34-
const OrderItem: React.FC<{
35-
order: ShopSchemas.Order;
36-
disabled?: boolean;
37-
}> = ({ order, disabled }) => {
38-
const orderRefundMutation = ShopHooks.useOrderRefundMutation();
39-
const oneItemRefundMutation = ShopHooks.useOneItemRefundMutation();
40-
const optionsOfOneItemInOrderPatchMutation = ShopHooks.useOptionsOfOneItemInOrderPatchMutation();
34+
const OrderItem: React.FC<{ order: ShopSchemas.Order; disabled?: boolean }> = ({ order, disabled }) => {
35+
const { shopApiDomain } = ShopHooks.useShopContext();
36+
const shopAPIClient = ShopHooks.useShopClient();
37+
const orderRefundMutation = ShopHooks.useOrderRefundMutation(shopAPIClient);
38+
const oneItemRefundMutation = ShopHooks.useOneItemRefundMutation(shopAPIClient);
39+
const optionsOfOneItemInOrderPatchMutation = ShopHooks.useOptionsOfOneItemInOrderPatchMutation(shopAPIClient);
4140

4241
const refundOrder = () => orderRefundMutation.mutate({ order_id: order.id });
43-
const openReceipt = () => window.open(ShopUtils.getReceiptUrlFromOrder(order), "_blank");
42+
const openReceipt = () => window.open(`${shopApiDomain}/v1/orders/${order.id}/receipt/`, "_blank");
4443

4544
const isPending =
4645
disabled ||
4746
orderRefundMutation.isPending ||
4847
oneItemRefundMutation.isPending ||
4948
optionsOfOneItemInOrderPatchMutation.isPending;
50-
const btnDisabled =
51-
isPending || !R.isNullish(order.not_fully_refundable_reason);
49+
const refundBtnDisabled = isPending || !R.isNullish(order.not_fully_refundable_reason);
50+
const receipyBtnDisabled = isPending || order.current_status === "pending";
5251
const btnText = R.isNullish(order.not_fully_refundable_reason)
5352
? "주문 전체 환불"
5453
: order.current_status === "refunded"
@@ -189,15 +188,15 @@ const OrderItem: React.FC<{
189188
variant="contained"
190189
sx={{ width: "100%" }}
191190
onClick={openReceipt}
192-
disabled={btnDisabled}
191+
disabled={receipyBtnDisabled}
193192
>
194193
영수증
195194
</Button>
196195
<Button
197196
variant="contained"
198197
sx={{ width: "100%" }}
199198
onClick={refundOrder}
200-
disabled={btnDisabled}
199+
disabled={refundBtnDisabled}
201200
>
202201
{btnText}
203202
</Button>
@@ -209,7 +208,8 @@ const OrderItem: React.FC<{
209208
export const OrderList: React.FC = () => {
210209
const WrappedOrderList: React.FC = () => {
211210
// eslint-disable-next-line react-hooks/rules-of-hooks
212-
const { data } = ShopHooks.useOrders();
211+
const shopAPIClient = ShopHooks.useShopClient();
212+
const { data } = ShopHooks.useOrders(shopAPIClient);
213213

214214
return (
215215
<List>

packages/shop/src/components/features/product.tsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,9 @@ const ProductItem: React.FC<{
9595
const optionFormRef = React.useRef<HTMLFormElement>(null);
9696

9797
const queryClient = useQueryClient();
98-
const oneItemOrderStartMutation = ShopHooks.usePrepareOneItemOrderMutation();
99-
const addItemToCartMutation = ShopHooks.useAddItemToCartMutation();
98+
const shopAPIClient = ShopHooks.useShopClient();
99+
const oneItemOrderStartMutation = ShopHooks.usePrepareOneItemOrderMutation(shopAPIClient);
100+
const addItemToCartMutation = ShopHooks.useAddItemToCartMutation(shopAPIClient);
100101

101102
const addItemToCart = () =>
102103
addItemToCartMutation.mutate(
@@ -199,9 +200,10 @@ const ProductItem: React.FC<{
199200
);
200201
};
201202

202-
export const ProductList: React.FC = () => {
203+
export const ProductList: React.FC<ShopSchemas.ProductListQueryParams> = (qs) => {
203204
const WrappedProductList: React.FC = () => {
204-
const { data } = ShopHooks.useProducts();
205+
const shopAPIClient = ShopHooks.useShopClient();
206+
const { data } = ShopHooks.useProducts(shopAPIClient, qs);
205207
return (
206208
<List>
207209
{data.map((product) => (

packages/shop/src/components/features/user_status.tsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@ import ShopHooks from '../../hooks';
1414

1515
export const UserInfo: React.FC = () => {
1616
const formRef = React.useRef<HTMLFormElement>(null);
17-
const signInWithEmailMutation = ShopHooks.useSignInWithEmailMutation();
18-
const SignInWithSNSMutation = ShopHooks.useSignInWithSNSMutation();
19-
const signOutMutation = ShopHooks.useSignOutMutation();
17+
const shopAPIClient = ShopHooks.useShopClient();
18+
const signInWithEmailMutation = ShopHooks.useSignInWithEmailMutation(shopAPIClient);
19+
const SignInWithSNSMutation = ShopHooks.useSignInWithSNSMutation(shopAPIClient);
20+
const signOutMutation = ShopHooks.useSignOutMutation(shopAPIClient);
2021

2122
const signInWithGoogle = () =>
2223
SignInWithSNSMutation.mutate({
@@ -42,7 +43,8 @@ export const UserInfo: React.FC = () => {
4243

4344
const WrappedUserStatus: React.FC = () => {
4445
// eslint-disable-next-line react-hooks/rules-of-hooks
45-
const { data } = ShopHooks.useUserStatus();
46+
const shopAPIClient = ShopHooks.useShopClient();
47+
const { data } = ShopHooks.useUserStatus(shopAPIClient);
4648

4749
return data && data.meta.is_authenticated === true ? (
4850
<Stack>

packages/shop/src/hooks/index.ts

Lines changed: 48 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,23 @@ import ShopContext from '../contexts';
88
import ShopSchemas from "../schemas";
99

1010
const QUERY_KEYS = {
11-
USER: ["query", "user"],
12-
PRODUCT_LIST: ["query", "products"],
13-
CART_INFO: ["query", "cart"],
14-
ORDER_LIST: ["query", "orders"],
11+
BASE: ["query", "shop"],
12+
USER: ["query", "shop", "user"],
13+
PRODUCT_LIST: ["query", "shop", "products"],
14+
CART_INFO: ["query", "shop", "cart"],
15+
ORDER_LIST: ["query", "shop", "orders"],
1516
};
1617

1718
const MUTATION_KEYS = {
18-
USER_SIGN_IN_EMAIL: ["mutation", "user", "sign_in", "email"],
19-
USER_SIGN_IN_SNS: ["mutation", "user", "sign_in", "sns"],
20-
USER_SIGN_OUT: ["mutation", "user", "sign_out"],
21-
CART_ITEM_APPEND: ["mutation", "cart", "item", "append"],
22-
CART_ITEM_REMOVE: ["mutation", "cart", "item", "remove"],
23-
CART_ORDER_START: ["mutation", "cart_order", "start"],
24-
ONE_ITEM_ORDER_START: ["mutation", "one_item_order", "start"],
25-
ALL_ORDER_REFUND: ["mutation", "all_order_refund"],
26-
ONE_ITEM_REFUND: ["mutation", "one_item_refund"],
19+
USER_SIGN_IN_EMAIL: ["mutation", "shop", "user", "sign_in", "email"],
20+
USER_SIGN_IN_SNS: ["mutation", "shop", "user", "sign_in", "sns"],
21+
USER_SIGN_OUT: ["mutation", "shop", "user", "sign_out"],
22+
CART_ITEM_APPEND: ["mutation", "shop", "cart", "item", "append"],
23+
CART_ITEM_REMOVE: ["mutation", "shop", "cart", "item", "remove"],
24+
CART_ORDER_START: ["mutation", "shop", "cart_order", "start"],
25+
ONE_ITEM_ORDER_START: ["mutation", "shop", "one_item_order", "start"],
26+
ALL_ORDER_REFUND: ["mutation", "shop", "all_order_refund"],
27+
ONE_ITEM_REFUND: ["mutation", "shop", "one_item_refund"],
2728
};
2829

2930
namespace ShopHooks {
@@ -35,133 +36,103 @@ namespace ShopHooks {
3536
return context;
3637
}
3738

38-
const clientDecorator = <T = CallableFunction>(func:(client: ShopAPIClient) => T): T => {
39+
export const useShopClient = () => {
3940
const { shopApiDomain, shopApiCSRFCookieName, shopApiTimeout } = useShopContext();
40-
return func(new ShopAPIClient(shopApiDomain, shopApiCSRFCookieName, shopApiTimeout));
41+
return new ShopAPIClient(shopApiDomain, shopApiCSRFCookieName, shopApiTimeout);
4142
}
4243

43-
export const useUserStatus = () =>
44+
export const useUserStatus = (client: ShopAPIClient) =>
4445
useSuspenseQuery({
4546
queryKey: QUERY_KEYS.USER,
46-
queryFn: async () => {
47-
try {
48-
const userInfo = await clientDecorator(ShopAPIs.retrieveUserInfo)();
49-
return userInfo.meta.is_authenticated === true ? userInfo : null;
50-
} catch (e) {
51-
return null;
52-
}
53-
},
47+
queryFn: ShopAPIs.retrieveUserInfo(client),
48+
retry: 3,
5449
});
5550

56-
export const useSignInWithEmailMutation = () =>
51+
export const useSignInWithEmailMutation = (client: ShopAPIClient) =>
5752
useMutation({
5853
mutationKey: MUTATION_KEYS.USER_SIGN_IN_EMAIL,
59-
mutationFn: clientDecorator(ShopAPIs.signInWithEmail),
60-
meta: {
61-
invalidates: [
62-
QUERY_KEYS.USER,
63-
QUERY_KEYS.CART_INFO,
64-
QUERY_KEYS.ORDER_LIST,
65-
],
66-
},
54+
mutationFn: ShopAPIs.signInWithEmail(client),
55+
meta: { invalidates: [ QUERY_KEYS.BASE ] },
6756
});
6857

69-
export const useSignInWithSNSMutation = () =>
58+
export const useSignInWithSNSMutation = (client: ShopAPIClient) =>
7059
useMutation({
7160
mutationKey: MUTATION_KEYS.USER_SIGN_IN_SNS,
72-
mutationFn: clientDecorator(ShopAPIs.signInWithSNS),
73-
meta: {
74-
invalidates: [
75-
QUERY_KEYS.USER,
76-
QUERY_KEYS.CART_INFO,
77-
QUERY_KEYS.ORDER_LIST,
78-
],
79-
},
61+
mutationFn: ShopAPIs.signInWithSNS(client),
62+
meta: { invalidates: [ QUERY_KEYS.BASE ] },
8063
});
8164

82-
export const useSignOutMutation = () =>
65+
export const useSignOutMutation = (client: ShopAPIClient) =>
8366
useMutation({
8467
mutationKey: MUTATION_KEYS.USER_SIGN_OUT,
85-
mutationFn: async () => {
86-
try {
87-
return await clientDecorator(ShopAPIs.signOut)();
88-
} catch (e) {
89-
return null;
90-
}
91-
},
92-
meta: {
93-
invalidates: [
94-
QUERY_KEYS.USER,
95-
QUERY_KEYS.CART_INFO,
96-
QUERY_KEYS.ORDER_LIST,
97-
],
98-
},
68+
mutationFn: ShopAPIs.signOut(client),
69+
meta: { invalidates: [ QUERY_KEYS.BASE ] },
9970
});
10071

101-
export const useProducts = (qs?: ShopSchemas.ProductListQueryParams) =>
72+
export const useProducts = (client: ShopAPIClient, qs?: ShopSchemas.ProductListQueryParams) =>
10273
useSuspenseQuery({
10374
queryKey: QUERY_KEYS.PRODUCT_LIST,
104-
queryFn: () => clientDecorator(ShopAPIs.listProducts)(qs),
75+
queryFn: () => ShopAPIs.listProducts(client)(qs),
10576
});
10677

107-
export const useCart = () =>
78+
export const useCart = (client: ShopAPIClient) =>
10879
useSuspenseQuery({
10980
queryKey: QUERY_KEYS.CART_INFO,
110-
queryFn: clientDecorator(ShopAPIs.retrieveCart),
81+
queryFn: ShopAPIs.retrieveCart(client),
11182
});
11283

113-
export const useAddItemToCartMutation = () =>
84+
export const useAddItemToCartMutation = (client: ShopAPIClient) =>
11485
useMutation({
11586
mutationKey: MUTATION_KEYS.CART_ITEM_APPEND,
116-
mutationFn: clientDecorator(ShopAPIs.appendItemToCart),
87+
mutationFn: ShopAPIs.appendItemToCart(client),
11788
meta: { invalidates: [QUERY_KEYS.CART_INFO] },
11889
});
11990

120-
export const useRemoveItemFromCartMutation = () =>
91+
export const useRemoveItemFromCartMutation = (client: ShopAPIClient) =>
12192
useMutation({
12293
mutationKey: MUTATION_KEYS.CART_ITEM_REMOVE,
123-
mutationFn: clientDecorator(ShopAPIs.removeItemFromCart),
94+
mutationFn: ShopAPIs.removeItemFromCart(client),
12495
meta: { invalidates: [QUERY_KEYS.CART_INFO] },
12596
});
12697

127-
export const usePrepareOneItemOrderMutation = () =>
98+
export const usePrepareOneItemOrderMutation = (client: ShopAPIClient) =>
12899
useMutation({
129100
mutationKey: MUTATION_KEYS.ONE_ITEM_ORDER_START,
130-
mutationFn: clientDecorator(ShopAPIs.prepareOneItemOrder),
101+
mutationFn: ShopAPIs.prepareOneItemOrder(client),
131102
meta: { invalidates: [QUERY_KEYS.CART_INFO, QUERY_KEYS.ORDER_LIST] },
132103
});
133104

134-
export const usePrepareCartOrderMutation = () =>
105+
export const usePrepareCartOrderMutation = (client: ShopAPIClient) =>
135106
useMutation({
136107
mutationKey: MUTATION_KEYS.CART_ORDER_START,
137-
mutationFn: clientDecorator(ShopAPIs.prepareCartOrder),
108+
mutationFn: ShopAPIs.prepareCartOrder(client),
138109
meta: { invalidates: [QUERY_KEYS.CART_INFO, QUERY_KEYS.ORDER_LIST] },
139110
});
140111

141-
export const useOrders = () =>
112+
export const useOrders = (client: ShopAPIClient) =>
142113
useSuspenseQuery({
143114
queryKey: QUERY_KEYS.ORDER_LIST,
144-
queryFn: clientDecorator(ShopAPIs.listOrders),
115+
queryFn: ShopAPIs.listOrders(client),
145116
});
146117

147-
export const useOneItemRefundMutation = () =>
118+
export const useOneItemRefundMutation = (client: ShopAPIClient) =>
148119
useMutation({
149120
mutationKey: MUTATION_KEYS.ONE_ITEM_REFUND,
150-
mutationFn: clientDecorator(ShopAPIs.refundOneItemFromOrder),
121+
mutationFn: ShopAPIs.refundOneItemFromOrder(client),
151122
meta: { invalidates: [QUERY_KEYS.ORDER_LIST] },
152123
});
153124

154-
export const useOrderRefundMutation = () =>
125+
export const useOrderRefundMutation = (client: ShopAPIClient) =>
155126
useMutation({
156127
mutationKey: MUTATION_KEYS.ALL_ORDER_REFUND,
157-
mutationFn: clientDecorator(ShopAPIs.refundAllItemsInOrder),
128+
mutationFn: ShopAPIs.refundAllItemsInOrder(client),
158129
meta: { invalidates: [QUERY_KEYS.ORDER_LIST] },
159130
});
160131

161-
export const useOptionsOfOneItemInOrderPatchMutation = () =>
132+
export const useOptionsOfOneItemInOrderPatchMutation = (client: ShopAPIClient) =>
162133
useMutation({
163134
mutationKey: MUTATION_KEYS.CART_ITEM_APPEND,
164-
mutationFn: clientDecorator(ShopAPIs.patchOrderOptions),
135+
mutationFn: ShopAPIs.patchOrderOptions(client),
165136
meta: { invalidates: [QUERY_KEYS.ORDER_LIST] },
166137
});
167138
}

packages/shop/src/utils/index.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import * as R from "remeda";
22

3-
import ShopHooks from '../hooks';
43
import ShopSchemas from "../schemas";
54
import { startPortOnePurchase as _startPortOnePurchase } from "./portone";
65

@@ -30,11 +29,6 @@ namespace ShopAPIUtil {
3029
return false;
3130
};
3231

33-
export const getReceiptUrlFromOrder = (order: ShopSchemas.Order) => {
34-
const { shopApiDomain } = ShopHooks.useShopContext();
35-
return `${shopApiDomain}/v1/orders/${order.id}/receipt/`;
36-
}
37-
3832
export const startPortOnePurchase = _startPortOnePurchase;
3933
}
4034

0 commit comments

Comments
 (0)