@@ -4,52 +4,29 @@ export async function useCart() {
44 const toast = useToast ( )
55
66 const id = useLocalStorage < string | null > ( 'shopify-cart-id' , null )
7- const cartItems = useState < any [ ] > ( 'cart-items' , ( ) => [ ] )
8- const numItems = computed ( ( ) => {
9- return cartItems . value . reduce ( ( acc , item ) => {
10- return acc + item . node . quantity
11- } , 0 )
12- } )
7+
8+ const cartItems = useState < unknown [ ] > ( 'cart-items' , ( ) => [ ] )
139
1410 const first = ref ( 10 )
1511
1612 const init = async ( ) => {
17- try {
18- const data = await $fetch ( '/api/cart/create' , {
19- method : 'POST' ,
20- } )
21-
22- if ( ! data ?. cartCreate ?. cart ?. id ) {
23- throw new Error ( 'Failed to create cart' )
24- }
25-
26- id . value = data . cartCreate . cart . id
27- }
28- catch ( error ) {
29- id . value = null
30-
31- toast . add ( {
32- title : 'Failed to initialize cart' ,
33- description : 'Please try again later' ,
34- color : 'error' ,
35- } )
36- }
13+ // TODO: Init cart
3714 }
3815
3916 const fetchCart = async ( ) => {
4017 try {
41- const { data, error } = await useFetch ( '/api/cart' , {
18+ const { data : _data , error } = await useFetch ( '/api/cart' , {
4219 method : 'POST' ,
4320 body : { id : id . value , lines : { first : first . value } } ,
4421 } )
4522
4623 if ( error . value ) {
4724 throw new Error ( 'Failed to fetch cart' )
4825 }
49-
50- cartItems . value = data . value ?. cart ?. lines ?. edges || [ ]
5126 }
5227 catch ( error ) {
28+ console . error ( error )
29+
5330 toast . add ( {
5431 title : 'Failed to fetch cart' ,
5532 description : 'Please try again later' ,
@@ -60,7 +37,7 @@ export async function useCart() {
6037
6138 const addItems = async ( ...items : { merchandiseId : string , quantity : number } [ ] ) => {
6239 try {
63- const { data, error } = await useFetch ( '/api/cart/add-items' , {
40+ const { data : _data , error } = await useFetch ( '/api/cart/add-items' , {
6441 method : 'POST' ,
6542 body : {
6643 id : id . value ,
@@ -72,37 +49,33 @@ export async function useCart() {
7249 if ( error . value ) {
7350 throw new Error ( 'Failed to add items to cart' )
7451 }
75-
76- cartItems . value = data . value ?. cartLinesAdd ?. cart ?. lines ?. edges || [ ]
7752 }
7853 catch ( error ) {
79- toast . add ( {
80- title : 'Failed to add items to cart' ,
81- description : error . message ,
82- color : 'error' ,
83- } )
54+ if ( error instanceof Error ) {
55+ toast . add ( {
56+ title : 'Failed to add items to cart' ,
57+ description : error . message ,
58+ color : 'error' ,
59+ } )
60+ }
8461 }
8562 }
8663
8764 const removeItems = async ( lineIds : string [ ] ) => {
8865 try {
89- const { data , error } = await $fetch ( '/api/cart/remove-items' , {
66+ const _data = await $fetch ( '/api/cart/remove-items' , {
9067 method : 'POST' ,
9168 body : { id : id . value , lineIds } ,
9269 } )
93-
94- if ( error ) {
95- throw new Error ( 'Failed to remove items from cart' )
96- }
97-
98- cartItems . value = data . value ?. cartLinesRemove ?. cart ?. lines ?. edges || [ ]
9970 }
10071 catch ( error ) {
101- toast . add ( {
102- title : 'Failed to remove items from cart' ,
103- description : error . message ,
104- color : 'error' ,
105- } )
72+ if ( error instanceof Error ) {
73+ toast . add ( {
74+ title : 'Failed to remove items from cart' ,
75+ description : error . message ,
76+ color : 'error' ,
77+ } )
78+ }
10679 }
10780 }
10881
@@ -116,7 +89,6 @@ export async function useCart() {
11689 return {
11790 id,
11891 cartItems,
119- numItems,
12092 init,
12193 addItems,
12294 removeItems,
0 commit comments