Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | 482x 738x 738x 482x | import { QKEY_LIST_CART } from "@constants/queryKeys";
import { customInstance } from "@services/api";
import { useQuery } from "react-query";
// POST /cart/items => add an item to the cart
// DELETE /cart/items/:item_id => remove an item from the cart
// DELETE /cart/items => remove all items from the cart (clear the cart)
// PATCH /cart/items/:item_id => update an item in the cart
// GET /cart => read cart content (list all items in the cart)
// Helper to safely convert and divide price values
const safePriceConversion = (value: any): number => {
const parsed = parseFloat(String(value ?? 0));
return isNaN(parsed) ? 0 : parsed / 100;
};
// list cart items
export function useCartItems(enabled: boolean, onSuccess?: any) {
const data = useQuery(
[QKEY_LIST_CART],
async () => {
const data = await customInstance({
url: `/cart`,
method: "GET",
});
// Safely handle items array - default to empty if missing/invalid
const items = Array.isArray(data?.items) ? data.items : [];
return {
...data,
items: items.map((item: any) => ({
...item,
productVariantPrice: safePriceConversion(item.productVariantPrice),
unitPrice: safePriceConversion(item.unitPrice),
})),
};
},
{
refetchOnWindowFocus: false,
staleTime: 1000 * 60 * 5,
enabled,
onSuccess,
},
);
return data;
}
export const deleteCartItems = () =>
customInstance({
url: `cart/items`,
method: "DELETE",
});
|