11/*eslint complexity: ["error", 20]*/
22// Imports
3- import { useState , useContext , useEffect } from 'react' ;
3+ import { useState , useEffect } from 'react' ;
44import { useQuery , useMutation , ApolloError } from '@apollo/client' ;
55
66// Components
@@ -11,7 +11,7 @@ import LoadingSpinner from '../LoadingSpinner/LoadingSpinner.component';
1111// GraphQL
1212import { GET_CART } from '@/utils/gql/GQL_QUERIES' ;
1313import { CHECKOUT_MUTATION } from '@/utils/gql/GQL_MUTATIONS' ;
14- import { CartContext } from '@/stores/CartProvider ' ;
14+ import { useCartStore } from '@/stores/cartStore ' ;
1515
1616// Utils
1717import {
@@ -51,7 +51,7 @@ export interface ICheckoutData {
5151}
5252
5353const CheckoutForm = ( ) => {
54- const { cart, setCart } = useContext ( CartContext ) ;
54+ const { cart, setCart } = useCartStore ( ) ;
5555 const [ orderData , setOrderData ] = useState < ICheckoutData | null > ( null ) ;
5656 const [ requestError , setRequestError ] = useState < ApolloError | null > ( null ) ;
5757 const [ orderCompleted , setorderCompleted ] = useState < boolean > ( false ) ;
@@ -63,17 +63,19 @@ const CheckoutForm = () => {
6363 // Update cart in the localStorage.
6464 const updatedCart = getFormattedCart ( data ) ;
6565
66- if ( ! updatedCart && ! data . cart . contents . nodes . length ) {
66+ if ( ! updatedCart && ! data ? .cart ? .contents ? .nodes ? .length ) {
6767 localStorage . removeItem ( 'woo-session' ) ;
68- localStorage . removeItem ( 'wooocommerce -cart' ) ;
68+ localStorage . removeItem ( 'woocommerce -cart' ) ;
6969 setCart ( null ) ;
7070 return ;
7171 }
7272
7373 localStorage . setItem ( 'woocommerce-cart' , JSON . stringify ( updatedCart ) ) ;
7474
75- // Update cart data in React Context.
76- setCart ( updatedCart ) ;
75+ // Update cart data in Zustand store
76+ if ( updatedCart ) {
77+ setCart ( updatedCart ) ;
78+ }
7779 } ,
7880 } ) ;
7981
0 commit comments