Skip to content

Commit 55ec7c1

Browse files
committed
Cart
1 parent 0910043 commit 55ec7c1

File tree

3 files changed

+24
-7
lines changed

3 files changed

+24
-7
lines changed

src/components/Cart/CartContents.component.tsx

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,28 @@ import { UPDATE_CART } from '@/utils/gql/GQL_MUTATIONS';
1818

1919
const CartContents = () => {
2020
const router = useRouter();
21-
const { cart, setCart } = useCartStore();
21+
const { cart, setCart, syncError, setSyncError } = useCartStore();
2222
const isCheckoutPage = router.pathname === '/kasse';
2323

2424
useQuery(GET_CART, {
2525
notifyOnNetworkStatusChange: true,
2626
onCompleted: (data) => {
2727
// Only update if there's a significant difference to avoid unnecessary re-renders
2828
const updatedCart = getFormattedCart(data) as RootObject | undefined;
29+
if (!updatedCart && cart?.products?.length) {
30+
console.error('Server cart is empty but local cart has items');
31+
setSyncError(true);
32+
return;
33+
}
2934
if (!cart || cart.totalProductsCount !== updatedCart?.totalProductsCount) {
3035
setCart(updatedCart || null);
3136
}
3237
},
3338
onError: (error) => {
3439
console.error('Error fetching cart:', error);
35-
// On error, we'll show the cached cart data instead of setting to null
40+
if (cart?.products?.length) {
41+
setSyncError(true);
42+
}
3643
},
3744
fetchPolicy: 'cache-and-network',
3845
});
@@ -59,6 +66,12 @@ const CartContents = () => {
5966

6067
return (
6168
<div className="container mx-auto px-4 py-8">
69+
{syncError && cart?.products?.length && (
70+
<div className="bg-red-100 border border-red-400 text-red-700 px-4 py-3 rounded relative mb-4">
71+
<strong className="font-bold">Synkroniseringsfeil!</strong>
72+
<span className="block sm:inline"> Kunne ikke synkronisere med server. Vennligst prøv igjen.</span>
73+
</div>
74+
)}
6275
{cart?.products?.length ? (
6376
<>
6477
<div className="bg-white rounded-lg p-6 mb-8 md:w-full">

src/components/Checkout/CheckoutForm.component.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export interface ICheckoutData {
5151
}
5252

5353
const CheckoutForm = () => {
54-
const { cart, setCart } = useCartStore();
54+
const { cart, setCart, syncError } = useCartStore();
5555
const [orderData, setOrderData] = useState<ICheckoutData | null>(null);
5656
const [requestError, setRequestError] = useState<ApolloError | null>(null);
5757
const [orderCompleted, setorderCompleted] = useState<boolean>(false);
@@ -107,7 +107,7 @@ const CheckoutForm = () => {
107107

108108
return (
109109
<>
110-
{(cart || orderCompleted) ? (
110+
{(cart || orderCompleted) && !syncError ? (
111111
<div className="container mx-auto">
112112
{/* Order*/}
113113
<CartContents />
@@ -129,9 +129,9 @@ const CheckoutForm = () => {
129129
</div>
130130
) : (
131131
<>
132-
{!cart && (
132+
{(!cart || syncError) && (
133133
<h1 className="text-2xl m-12 mt-24 font-bold text-center">
134-
Ingen produkter i handlekurven
134+
{syncError ? 'Kan ikke fortsette - handlekurven er ikke synkronisert med serveren' : 'Ingen produkter i handlekurven'}
135135
</h1>
136136
)}
137137
{orderCompleted && (

src/stores/cart.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,12 @@ export interface RootObject {
2626
interface CartState {
2727
cart: RootObject | null;
2828
isLoading: boolean;
29+
syncError: boolean;
2930
setCart: (cart: RootObject | null) => void;
3031
updateCart: (newCart: RootObject) => void;
3132
updateProductQuantity: (cartKey: string, newQty: number) => void;
3233
removeProduct: (cartKey: string) => void;
34+
setSyncError: (error: boolean) => void;
3335
}
3436

3537
type CartPersist = {
@@ -41,7 +43,9 @@ const useCartStore = create<CartState>()(
4143
(set) => ({
4244
cart: null,
4345
isLoading: false, // Start with false since we'll show persisted data immediately
44-
setCart: (cart: RootObject | null) => set({ cart, isLoading: false }),
46+
syncError: false,
47+
setCart: (cart: RootObject | null) => set({ cart, isLoading: false, syncError: false }),
48+
setSyncError: (error: boolean) => set({ syncError: error }),
4549
updateCart: (newCart: RootObject) => {
4650
set({ cart: newCart });
4751
},

0 commit comments

Comments
 (0)