Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added useCart hook #92

Merged
merged 1 commit into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/api/api-hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { findItemIdInCart } from './cart-helpers';
import { useEcomAPI } from './ecom-api-context-provider';
import { AddToCartOptions } from './types';

export const useCart = () => {
export const useCartData = () => {
const ecomApi = useEcomAPI();
return useSwr('cart', async () => {
const response = await ecomApi.getCart();
Expand All @@ -19,7 +19,7 @@ export const useCart = () => {

export const useCartTotals = () => {
const ecomApi = useEcomAPI();
const { data } = useCart();
const { data } = useCartData();

const cartTotals = useSwr('cart-totals', async () => {
const response = await ecomApi.getCartTotals();
Expand All @@ -46,7 +46,7 @@ type AddToCartArgs = {

export const useAddToCart = () => {
const ecomApi = useEcomAPI();
const { data: cart } = useCart();
const { data: cart } = useCartData();
return useSWRMutation(
'cart',
async (_key: Key, { arg }: { arg: AddToCartArgs }) => {
Expand Down
25 changes: 7 additions & 18 deletions src/components/cart/cart.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
import { useState } from 'react';
import { useCart, useCartTotals, useRemoveItemFromCart, useUpdateCartItemQuantity } from '~/api/api-hooks';
import { useEcomAPI } from '~/api/ecom-api-context-provider';
import { Drawer } from '~/components/drawer/drawer';
import { isCartItemAvailable } from '~/utils';
import { useCart } from '~/hooks/use-cart';
import { useCartOpen } from './cart-open-context';
import { CartView } from './cart-view/cart-view';

export const Cart = () => {
const ecomAPI = useEcomAPI();
const { isOpen, setIsOpen } = useCartOpen();
const { data: cart } = useCart();
const { data: cartTotals } = useCartTotals();
const { trigger: updateQuantity } = useUpdateCartItemQuantity();
const { trigger: removeItem } = useRemoveItemFromCart();
const { cartData, cartTotals, checkout, removeItem, updateItemQuantity } = useCart();
const [checkoutAttempted, setCheckoutAttempted] = useState(false);

const someItemsOutOfStock = cart?.lineItems.some((item) => !isCartItemAvailable(item));
const someItemsOutOfStock = cartData?.lineItems.some((item) => !isCartItemAvailable(item));

const handleCheckout = async () => {
setCheckoutAttempted(true);
Expand All @@ -24,29 +19,23 @@ export const Cart = () => {
return;
}

const checkoutResponse = await ecomAPI.checkout();

if (checkoutResponse.status === 'success') {
window.location.href = checkoutResponse.body.checkoutUrl;
} else {
alert('checkout is not configured');
}
checkout();
};

const errorMessage = checkoutAttempted && someItemsOutOfStock ? 'Some items are out of stock' : undefined;

return (
<Drawer title="Cart" onClose={() => setIsOpen(false)} isOpen={isOpen}>
{cart === undefined ? (
{cartData === undefined ? (
<div>Loading...</div>
) : (
<CartView
cart={cart}
cart={cartData}
cartTotals={cartTotals}
errorMessage={errorMessage}
onCheckout={handleCheckout}
onItemRemove={removeItem}
onItemQuantityChange={updateQuantity}
onItemQuantityChange={updateItemQuantity}
/>
)}
</Drawer>
Expand Down
28 changes: 28 additions & 0 deletions src/hooks/use-cart.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { useCartTotals, useCartData, useUpdateCartItemQuantity, useRemoveItemFromCart } from '~/api/api-hooks';
import { useEcomAPI } from '~/api/ecom-api-context-provider';

export const useCart = () => {
Copy link
Collaborator

@VladyslavG VladyslavG Oct 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return type? or is it too verbose for such return data ?

const ecomAPI = useEcomAPI();
const { data: cartData } = useCartData();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe it would be better to rename values returned from hooks to not rename it during assignment?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can't do that, because it's a SWR object returned from the hook

const { data: cartTotals } = useCartTotals();
const { trigger: updateItemQuantity } = useUpdateCartItemQuantity();
const { trigger: removeItem } = useRemoveItemFromCart();

const checkout = async () => {
const checkoutResponse = await ecomAPI.checkout();

if (checkoutResponse.status === 'success') {
window.location.href = checkoutResponse.body.checkoutUrl;
} else {
alert('checkout is not configured');
}
};

return {
cartData,
cartTotals,
updateItemQuantity,
removeItem,
checkout,
};
};