@@ -21,33 +21,32 @@ interface ShoppingCartProps {
2121
2222export function ShoppingCart ( { isOpen, onClose, userId } : ShoppingCartProps ) {
2323 const [ cartItems , setCartItems ] = useState < CartItem [ ] > ( [ ] )
24- const [ isLoading , setIsLoading ] = useState ( false )
2524
2625 // Load cart items on mount
2726 useEffect ( ( ) => {
28- loadCartItems ( )
29- } , [ userId ] )
30-
31- const loadCartItems = async ( ) => {
32- if ( userId ) {
33- // Load from database for logged-in users
34- try {
35- const response = await fetch ( '/api/cart' )
36- if ( response . ok ) {
37- const data = await response . json ( )
38- setCartItems ( data )
27+ const loadCartItems = async ( ) => {
28+ if ( userId ) {
29+ // Load from database for logged-in users
30+ try {
31+ const response = await fetch ( '/api/cart' )
32+ if ( response . ok ) {
33+ const data = await response . json ( )
34+ setCartItems ( data )
35+ }
36+ } catch ( error ) {
37+ console . error ( 'Error loading cart:' , error )
38+ }
39+ } else {
40+ // Load from localStorage for non-logged-in users
41+ const savedCart = localStorage . getItem ( 'cartzy-cart' )
42+ if ( savedCart ) {
43+ setCartItems ( JSON . parse ( savedCart ) )
3944 }
40- } catch ( error ) {
41- console . error ( 'Error loading cart:' , error )
42- }
43- } else {
44- // Load from localStorage for non-logged-in users
45- const savedCart = localStorage . getItem ( 'cartzy-cart' )
46- if ( savedCart ) {
47- setCartItems ( JSON . parse ( savedCart ) )
4845 }
4946 }
50- }
47+
48+ loadCartItems ( )
49+ } , [ userId ] )
5150
5251 const saveCartItems = async ( items : CartItem [ ] ) => {
5352 if ( userId ) {
@@ -67,30 +66,7 @@ export function ShoppingCart({ isOpen, onClose, userId }: ShoppingCartProps) {
6766 }
6867 }
6968
70- const addToCart = async ( product : any ) => {
71- const existingItem = cartItems . find ( item => item . productId === product . id )
72-
73- let newItems : CartItem [ ]
74- if ( existingItem ) {
75- newItems = cartItems . map ( item =>
76- item . productId === product . id
77- ? { ...item , quantity : item . quantity + 1 }
78- : item
79- )
80- } else {
81- newItems = [ ...cartItems , {
82- id : Date . now ( ) . toString ( ) ,
83- productId : product . id ,
84- name : product . name ,
85- price : product . price ,
86- quantity : 1 ,
87- image : product . images [ 0 ]
88- } ]
89- }
90-
91- setCartItems ( newItems )
92- await saveCartItems ( newItems )
93- }
69+
9470
9571 const updateQuantity = async ( itemId : string , newQuantity : number ) => {
9672 if ( newQuantity <= 0 ) {
0 commit comments