Skip to content

Commit e313667

Browse files
committed
chore: refactor cart-related features to honor architecture and building blocks taxonomy
1 parent a14ef93 commit e313667

14 files changed

Lines changed: 73 additions & 58 deletions

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export const useAddToCart = () => {
2121
} = useAddToCartNotifications();
2222
const onOpen = useProductAddedDialogStore((store) => store.onOpen);
2323

24-
const execute = async (productId: number) => {
24+
const addToCart = async (productId: number) => {
2525
if (!isAuthenticated) {
2626
notifyNotAuthenticated();
2727
return;
@@ -46,5 +46,5 @@ export const useAddToCart = () => {
4646
}
4747
};
4848

49-
return { execute, isPending };
49+
return { addToCart, isPending };
5050
};

src/features/carts/components/ClearCartButton/use-clear-cart-notifications.ts renamed to src/features/carts/application/use-clear-cart-notifications.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,19 @@ export const useClearCartNotifications = () => {
55
const t = useTranslations("features.carts.clear-cart.notifications");
66
const toast = useToast();
77

8-
const success = () =>
8+
const notifySuccess = () =>
99
toast({
1010
status: "success",
1111
title: t("title"),
1212
description: t("success"),
1313
});
1414

15-
const failure = () =>
15+
const notifyFailure = () =>
1616
toast({
1717
status: "error",
1818
title: t("title"),
1919
description: t("error"),
2020
});
2121

22-
return [success, failure] as const;
22+
return { notifySuccess, notifyFailure };
2323
};
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { useAuthStore } from "@/features/auth/application/auth-store";
2+
import { useClearCartMutation } from "@/features/carts/providers/use-clear-cart-mutation";
3+
4+
import { useClearCartNotifications } from "./use-clear-cart-notifications";
5+
6+
export const useClearCart = () => {
7+
const cartId = useAuthStore((store) => store.user?.cartId);
8+
const [mutateAsync, isPending] = useClearCartMutation();
9+
const { notifySuccess, notifyFailure } = useClearCartNotifications();
10+
11+
const clearCart = async (): Promise<boolean> => {
12+
if (!cartId) return false;
13+
14+
try {
15+
await mutateAsync({ cartId });
16+
notifySuccess();
17+
return true;
18+
} catch {
19+
notifyFailure();
20+
return false;
21+
}
22+
};
23+
24+
return { clearCart, isPending };
25+
};

src/features/carts/components/use-checkout-notifications.ts renamed to src/features/carts/application/use-purchase-notifications.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,19 @@ export const usePurchaseNotifications = () => {
55
const t = useTranslations("features.carts.checkout.notifications");
66
const toast = useToast();
77

8-
const success = () =>
8+
const notifySuccess = () =>
99
toast({
1010
status: "success",
1111
title: t("title"),
1212
description: t("success"),
1313
});
1414

15-
const failure = () =>
15+
const notifyFailure = () =>
1616
toast({
1717
status: "error",
1818
title: t("title"),
1919
description: t("error"),
2020
});
2121

22-
return [success, failure] as const;
22+
return { notifySuccess, notifyFailure };
2323
};
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { usePurchaseMutation } from "@/features/carts/providers/use-purchase-mutation";
2+
3+
import { usePurchaseNotifications } from "./use-purchase-notifications";
4+
5+
export const usePurchase = () => {
6+
const [mutateAsync, isPending] = usePurchaseMutation();
7+
const { notifySuccess, notifyFailure } = usePurchaseNotifications();
8+
9+
const purchase = async (): Promise<boolean> => {
10+
try {
11+
await mutateAsync();
12+
notifySuccess();
13+
return true;
14+
} catch {
15+
notifyFailure();
16+
return false;
17+
}
18+
};
19+
20+
return { purchase, isPending };
21+
};

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ interface IProps {
1010

1111
const AddToCartButton = ({ productId, colorPalette = "gray" }: IProps) => {
1212
const t = useTranslations("features.carts.add-to-cart");
13-
const { execute, isPending } = useAddToCart();
13+
const { addToCart, isPending } = useAddToCart();
1414

1515
return (
1616
<Button
1717
w="100%"
1818
colorPalette={colorPalette}
1919
loading={isPending}
20-
onClick={() => execute(productId)}
20+
onClick={() => addToCart(productId)}
2121
>
2222
{t("button")}
2323
</Button>

src/features/carts/components/CheckoutForm.tsx

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
import { Button, VStack } from "@chakra-ui/react";
22
import { useState } from "react";
33

4+
import { usePurchase } from "@/features/carts/application/use-purchase";
45
import type { PaymentMethod } from "@/features/carts/models/payment-method";
5-
import { usePurchase } from "@/features/carts/providers/use-purchase";
66
import { Select } from "@/lib/components/Form/Select";
77
import { TextInput } from "@/lib/components/Form/TextInput";
88
import { useTranslations } from "@/lib/i18n/use-transations";
99

10-
import { usePurchaseNotifications } from "./use-checkout-notifications";
11-
1210
interface IProps {
1311
onSuccess?: () => void;
1412
}
@@ -19,8 +17,7 @@ const CheckoutForm = ({ onSuccess }: IProps) => {
1917
const [address, setAddress] = useState<string>();
2018
const [method, setMethod] = useState<PaymentMethod>("blik");
2119

22-
const [purchase, isLoading] = usePurchase();
23-
const [notifySuccess, notifyFailure] = usePurchaseNotifications();
20+
const { purchase, isPending } = usePurchase();
2421

2522
return (
2623
<VStack
@@ -29,13 +26,8 @@ const CheckoutForm = ({ onSuccess }: IProps) => {
2926
onSubmit={async (e) => {
3027
e.preventDefault();
3128

32-
try {
33-
await purchase();
34-
notifySuccess();
35-
onSuccess?.();
36-
} catch {
37-
notifyFailure();
38-
}
29+
const success = await purchase();
30+
if (success) onSuccess?.();
3931
}}
4032
>
4133
<TextInput
@@ -64,7 +56,7 @@ const CheckoutForm = ({ onSuccess }: IProps) => {
6456
>
6557
{t("payment-method")}
6658
</Select>
67-
<Button type="submit" colorPalette="blue" w="100%" loading={isLoading}>
59+
<Button type="submit" colorPalette="blue" w="100%" loading={isPending}>
6860
{t("submit")}
6961
</Button>
7062
</VStack>

src/features/carts/components/ClearCartButton/ConfirmClearCartDialog.tsx

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,19 @@ import {
77
Text,
88
} from "@chakra-ui/react";
99

10+
import { useClearCart } from "@/features/carts/application/use-clear-cart";
1011
import { useConfirmClearCartDialogStore } from "@/features/carts/components/ClearCartButton/use-confirm-clear-cart-dialog-store";
11-
import { useClearCart } from "@/features/carts/providers/use-clear-cart";
1212
import { useTranslations } from "@/lib/i18n/use-transations";
1313

14-
import { useClearCartNotifications } from "./use-clear-cart-notifications";
15-
1614
const ConfirmClearCartDialog = () => {
17-
const [clear, isLoading] = useClearCart();
15+
const { clearCart, isPending } = useClearCart();
1816
const t = useTranslations("features.carts.clear-cart.dialog");
1917

2018
const { isOpen, onClose } = useConfirmClearCartDialogStore((state) => ({
2119
isOpen: state.isOpen,
2220
onClose: state.onClose,
2321
}));
2422

25-
const [notifySuccess, notifyFailure] = useClearCartNotifications();
26-
2723
return (
2824
<Dialog.Root
2925
role="alertdialog"
@@ -52,15 +48,12 @@ const ConfirmClearCartDialog = () => {
5248
<Button
5349
colorPalette="red"
5450
onClick={() => {
55-
clear()
56-
.then(() => {
57-
notifySuccess();
58-
onClose();
59-
})
60-
.catch(() => notifyFailure());
51+
void clearCart().then((success) => {
52+
if (success) onClose();
53+
});
6154
}}
6255
ml={3}
63-
loading={isLoading}
56+
loading={isPending}
6457
>
6558
{t("confirm")}
6659
</Button>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { useClearCartMutation } from "@/lib/api/carts/{cart-id}/clear-cart-mutation";

src/features/carts/providers/use-clear-cart.ts

Lines changed: 0 additions & 16 deletions
This file was deleted.

0 commit comments

Comments
 (0)