Skip to content

Commit 3b5198f

Browse files
committed
chore: refactor add to cart feature to match building blocks taxonomy
1 parent 48c4701 commit 3b5198f

6 files changed

Lines changed: 59 additions & 76 deletions

File tree

src/features/carts/components/AddToCartButton/use-add-to-cart-notifications.ts renamed to src/features/carts/application/use-add-to-cart-notifications.ts

File renamed without changes.
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { useAuthStore } from "@/features/auth/application/auth-store";
2+
import { useProductAddedDialogStore } from "@/features/carts/components/AddToCartButton/use-product-added-dialog-store";
3+
import {
4+
useAddToCartMutation,
5+
UnknownProductError,
6+
ProductNotAvailableError,
7+
} from "@/features/carts/providers/use-add-to-cart-mutation";
8+
9+
import { useAddToCartNotifications } from "./use-add-to-cart-notifications";
10+
11+
export const useAddToCart = () => {
12+
const cartId = useAuthStore((store) => store.user?.cartId);
13+
const isAuthenticated = useAuthStore((store) => store.isAuthenticated);
14+
const [mutateAsync, isPending] = useAddToCartMutation();
15+
const {
16+
notifySuccess,
17+
notifyFailure,
18+
notifyNotAuthenticated,
19+
notifyUnknownProduct,
20+
notifyProductNotAvailable,
21+
} = useAddToCartNotifications();
22+
const onOpen = useProductAddedDialogStore((store) => store.onOpen);
23+
24+
const execute = async (productId: number) => {
25+
if (!isAuthenticated) {
26+
notifyNotAuthenticated();
27+
return;
28+
}
29+
30+
try {
31+
await mutateAsync(cartId, { productId, quantity: 1 });
32+
notifySuccess();
33+
onOpen(cartId);
34+
} catch (e) {
35+
if (e instanceof UnknownProductError) {
36+
notifyUnknownProduct();
37+
return;
38+
}
39+
40+
if (e instanceof ProductNotAvailableError) {
41+
notifyProductNotAvailable();
42+
return;
43+
}
44+
45+
notifyFailure();
46+
}
47+
};
48+
49+
return { execute, isPending };
50+
};

src/features/carts/components/AddToCartButton/AddToCartButton.tsx

Lines changed: 4 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,23 @@
11
import { Button, type ButtonProps } from "@chakra-ui/react";
22

3-
import { useAuthStore } from "@/features/auth/application/auth-store";
4-
import { useProductAddedDialogStore } from "@/features/carts/components/AddToCartButton/use-product-added-dialog-store";
5-
import {
6-
useAddToCart,
7-
UnknownProductError,
8-
ProductNotAvailableError,
9-
} from "@/features/carts/providers/use-add-to-cart";
3+
import { useAddToCart } from "@/features/carts/application/use-add-to-cart";
104
import { useTranslations } from "@/lib/i18n/use-transations";
115

12-
import { useAddToCartNotifications } from "./use-add-to-cart-notifications";
13-
146
interface IProps {
157
productId: number;
168
colorPalette?: ButtonProps["colorPalette"];
179
}
1810

1911
const AddToCartButton = ({ productId, colorPalette = "gray" }: IProps) => {
20-
const cartId = useAuthStore((store) => store.user?.cartId);
21-
const isAuthenticated = useAuthStore((store) => store.isAuthenticated);
2212
const t = useTranslations("features.carts.add-to-cart");
23-
24-
const [add, isLoading] = useAddToCart();
25-
const {
26-
notifyFailure,
27-
notifySuccess,
28-
notifyNotAuthenticated,
29-
notifyUnknownProduct,
30-
notifyProductNotAvailable,
31-
} = useAddToCartNotifications();
32-
const onOpen = useProductAddedDialogStore((store) => store.onOpen);
33-
34-
const onAdd = async () => {
35-
if (!isAuthenticated) {
36-
return notifyNotAuthenticated();
37-
}
38-
39-
try {
40-
await add({ productId, quantity: 1 });
41-
notifySuccess();
42-
onOpen(cartId);
43-
} catch (e) {
44-
if (e instanceof UnknownProductError) {
45-
notifyUnknownProduct();
46-
return;
47-
}
48-
49-
if (e instanceof ProductNotAvailableError) {
50-
notifyProductNotAvailable();
51-
return;
52-
}
53-
54-
notifyFailure();
55-
}
56-
};
13+
const { execute, isPending } = useAddToCart();
5714

5815
return (
5916
<Button
6017
w="100%"
6118
colorPalette={colorPalette}
62-
loading={isLoading}
63-
onClick={onAdd}
19+
loading={isPending}
20+
onClick={() => execute(productId)}
6421
>
6522
{t("button")}
6623
</Button>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export {
2+
useAddToCartMutation,
3+
UnknownProductError,
4+
ProductNotAvailableError,
5+
} from "@/lib/api/carts/{cart-id}/add-to-cart-mutation";

src/features/carts/providers/use-add-to-cart.ts

Lines changed: 0 additions & 29 deletions
This file was deleted.
File renamed without changes.

0 commit comments

Comments
 (0)