-
Notifications
You must be signed in to change notification settings - Fork 127
/
Copy pathCartContents.component.tsx
187 lines (175 loc) · 5.77 KB
/
CartContents.component.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import { useEffect } from 'react';
import { useMutation, useQuery } from '@apollo/client';
import Link from 'next/link';
import Image from 'next/image';
import { useRouter } from 'next/router';
import { v4 as uuidv4 } from 'uuid';
import { useCartStore } from '@/stores/cartStore';
import Button from '@/components/UI/Button.component';
import LoadingSpinner from '../LoadingSpinner/LoadingSpinner.component';
import {
getFormattedCart,
getUpdatedItems,
handleQuantityChange,
IProductRootObject,
} from '@/utils/functions/functions';
import { GET_CART } from '@/utils/gql/GQL_QUERIES';
import { UPDATE_CART } from '@/utils/gql/GQL_MUTATIONS';
const CartContents = () => {
const router = useRouter();
const { clearWooCommerceSession, syncWithWooCommerce } = useCartStore();
const isCheckoutPage = router.pathname === '/kasse';
const { data, refetch } = useQuery(GET_CART, {
notifyOnNetworkStatusChange: true,
onCompleted: () => {
const updatedCart = getFormattedCart(data);
if (!updatedCart && !data?.cart?.contents?.nodes?.length) {
clearWooCommerceSession();
return;
}
if (updatedCart) {
syncWithWooCommerce(updatedCart);
}
},
});
const [updateCart, { loading: updateCartProcessing }] = useMutation(
UPDATE_CART,
{
onCompleted: () => {
refetch();
setTimeout(() => {
refetch();
}, 3000);
},
},
);
const handleRemoveProductClick = (
cartKey: string,
products: IProductRootObject[],
) => {
if (products?.length) {
const updatedItems = getUpdatedItems(products, 0, cartKey);
updateCart({
variables: {
input: {
clientMutationId: uuidv4(),
items: updatedItems,
},
},
});
}
refetch();
setTimeout(() => {
refetch();
}, 3000);
};
useEffect(() => {
refetch();
}, [refetch]);
const cartTotal = data?.cart?.total || '0';
const getUnitPrice = (subtotal: string, quantity: number) => {
const numericSubtotal = parseFloat(subtotal.replace(/[^0-9.-]+/g, ''));
return isNaN(numericSubtotal)
? 'N/A'
: (numericSubtotal / quantity).toFixed(2);
};
return (
<div className="container mx-auto px-4 py-8">
{data?.cart?.contents?.nodes?.length ? (
<>
<div className="bg-white rounded-lg p-6 mb-8 md:w-full">
{data.cart.contents.nodes.map((item: IProductRootObject) => (
<div
key={item.key}
className="flex items-center border-b border-gray-200 py-4"
>
<div className="flex-shrink-0 w-24 h-24 relative hidden md:block">
<Image
src={
item.product.node.image?.sourceUrl || '/placeholder.png'
}
alt={item.product.node.name}
layout="fill"
objectFit="cover"
className="rounded"
/>
</div>
<div className="flex-grow ml-4">
<h2 className="text-lg font-semibold">
{item.product.node.name}
</h2>
<p className="text-gray-600">
kr {getUnitPrice(item.subtotal, item.quantity)}
</p>
</div>
<div className="flex items-center">
<input
type="number"
min="1"
value={item.quantity}
onChange={(event) => {
handleQuantityChange(
event,
item.key,
data.cart.contents.nodes,
updateCart,
updateCartProcessing,
);
}}
className="w-16 px-2 py-1 text-center border border-gray-300 rounded mr-2"
/>
<Button
handleButtonClick={() =>
handleRemoveProductClick(
item.key,
data.cart.contents.nodes,
)
}
variant="secondary"
buttonDisabled={updateCartProcessing}
>
Fjern
</Button>
</div>
<div className="ml-4">
<p className="text-lg font-semibold">{item.subtotal}</p>
</div>
</div>
))}
</div>
<div className="bg-white rounded-lg p-6 md:w-full">
<div className="flex justify-end mb-4">
<span className="font-semibold pr-2">Subtotal:</span>
<span>{cartTotal}</span>
</div>
{!isCheckoutPage && (
<div className="flex justify-center mb-4">
<Link href="/kasse" passHref>
<Button variant="primary" fullWidth>GÅ TIL KASSE</Button>
</Link>
</div>
)}
</div>
</>
) : (
<div className="text-center">
<h2 className="text-2xl font-bold mb-4">
Ingen produkter i handlekurven
</h2>
<Link href="/produkter" passHref>
<Button variant="primary">Fortsett å handle</Button>
</Link>
</div>
)}
{updateCartProcessing && (
<div className="fixed inset-0 flex items-center justify-center bg-black bg-opacity-50">
<div className="bg-white p-4 rounded-lg">
<p className="text-lg mb-2">Oppdaterer handlekurv...</p>
<LoadingSpinner />
</div>
</div>
)}
</div>
);
};
export default CartContents;