1+ import { push } from 'notivue' ;
2+
13export const useCart = ( ) => {
24 const cart = useState ( 'cart' , ( ) => [ ] ) ;
35 const addToCartButtonStatus = ref ( 'add' ) ;
46 const removeFromCartButtonStatus = ref ( 'remove' ) ;
5- const { push } = useNotivue ( ) ;
6- const findItemByVariationId = id => cart . value . find ( i => i ?. variation ?. node ?. databaseId === id ) ;
7+ const findItem = id => cart . value . find ( i => i ?. variation ?. node ?. databaseId === id ) ;
78
89 const handleAddToCart = productId => {
910 addToCartButtonStatus . value = 'loading' ;
10-
11- $fetch ( '/api/cart/add' , {
12- method : 'POST' ,
13- body : { productId } ,
14- } )
11+ $fetch ( '/api/cart/add' , { method : 'POST' , body : { productId } } )
1512 . then ( res => {
16- updateCart ( [ ...cart . value , res . addToCart . cartItem ] ) ;
13+ const incoming = res . addToCart . cartItem ;
14+ const existing = cart . value . find ( i => i . key === incoming . key ) ;
15+ if ( existing ) {
16+ existing . quantity = incoming . quantity ;
17+ if ( typeof incoming ?. variation ?. node ?. stockQuantity === 'number' ) {
18+ existing . variation . node . stockQuantity = incoming . variation . node . stockQuantity ;
19+ }
20+ updateCart ( [ ...cart . value ] ) ;
21+ } else {
22+ updateCart ( [ ...cart . value , incoming ] ) ;
23+ }
1724 addToCartButtonStatus . value = 'added' ;
1825 setTimeout ( ( ) => ( addToCartButtonStatus . value = 'add' ) , 2000 ) ;
1926 } )
2027 . catch ( err => {
2128 addToCartButtonStatus . value = 'add' ;
22- const errorMessage = err . response . errors [ 0 ] . message
23- . replace ( / < a [ ^ > ] * > ( .* ?) < \/ a > / g, '' )
24- . replace ( / & m d a s h ; / g, '—' )
25- . trim ( ) ;
26- push . error ( errorMessage ) ;
29+ push . error ( 'Insufficient stock' ) ;
2730 } ) ;
2831 } ;
2932
@@ -45,22 +48,17 @@ export const useCart = () => {
4548
4649 const changeQty = ( key , quantity ) => {
4750 $fetch ( '/api/cart/update' , { method : 'POST' , body : { items : [ { key, quantity } ] } } ) ;
48- if ( quantity > 0 ) {
49- const idx = cart . value . findIndex ( i => i . key === key ) ;
50- cart . value [ idx ] . quantity = quantity ;
51- updateCart ( [ ...cart . value ] ) ;
52- } else {
53- updateCart ( cart . value . filter ( i => i . key !== key ) ) ;
54- }
51+ updateCart ( quantity > 0 ? cart . value . map ( i => ( i . key === key ? { ...i , quantity } : i ) ) : cart . value . filter ( i => i . key !== key ) ) ;
5552 } ;
5653
5754 const increment = id => {
58- const item = findItemByVariationId ( id ) ;
59- item ? changeQty ( item . key , item . quantity + 1 ) : handleAddToCart ( id ) ;
55+ const item = findItem ( id ) ;
56+ if ( item . quantity >= ( item ?. variation ?. node ?. stockQuantity ?? Infinity ) ) return push . error ( 'Insufficient stock' ) ;
57+ changeQty ( item . key , item . quantity + 1 ) ;
6058 } ;
6159
6260 const decrement = id => {
63- const item = findItemByVariationId ( id ) ;
61+ const item = findItem ( id ) ;
6462 if ( item ) changeQty ( item . key , item . quantity - 1 ) ;
6563 } ;
6664
0 commit comments