Skip to content

Commit 9119fde

Browse files
committed
fix: Refactor cart actions to synchronous functions
Removed unnecessary async/await from cart action handlers and simplified error handling in changeQty. This streamlines cart operations and improves code clarity.
1 parent 6e3ab02 commit 9119fde

File tree

1 file changed

+6
-10
lines changed

1 file changed

+6
-10
lines changed

app/composables/useCart.ts

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// app/composables/useCart.ts
12
import { push } from 'notivue';
23

34
export const useCart = () => {
@@ -38,16 +39,11 @@ export const useCart = () => {
3839
};
3940

4041
const changeQty = (key: string, quantity: number) => {
41-
try {
42-
$fetch('/api/cart/update', { method: 'POST', body: { items: [{ key, quantity }] } });
43-
if (quantity <= 0) updateCart(cart.value.filter(i => i.key !== key));
44-
else updateCart(cart.value.map(i => (i.key === key ? { ...i, quantity } : i)));
45-
} catch {
46-
push.error('Unable to update quantity');
47-
}
42+
$fetch('/api/cart/update', { method: 'POST', body: { items: [{ key, quantity }] } });
43+
updateCart(quantity <= 0 ? cart.value.filter(i => i.key !== key) : cart.value.map(i => (i.key === key ? { ...i, quantity } : i)));
4844
};
4945

50-
const handleRemoveFromCart = async (key: string) => {
46+
const handleRemoveFromCart = (key: string) => {
5147
try {
5248
removeFromCartButtonStatus.value = 'loading';
5349
changeQty(key, 0);
@@ -56,15 +52,15 @@ export const useCart = () => {
5652
}
5753
};
5854

59-
const increment = async (variationId: number) => {
55+
const increment = (variationId: number) => {
6056
const item = findItem(variationId);
6157
if (!item) return handleAddToCart(variationId);
6258
const max = item.variation?.node?.stockQuantity ?? Infinity;
6359
if (item.quantity >= max) return push.error('Insufficient stock');
6460
changeQty(item.key, item.quantity + 1);
6561
};
6662

67-
const decrement = async (variationId: number) => {
63+
const decrement = (variationId: number) => {
6864
const item = findItem(variationId);
6965
if (item) changeQty(item.key, item.quantity - 1);
7066
};

0 commit comments

Comments
 (0)