Skip to content

Commit f3f8a3a

Browse files
authored
Merge pull request #167 from w3bdesign/development
Development
2 parents 9f32d65 + 3e05b2f commit f3f8a3a

File tree

8 files changed

+69
-19
lines changed

8 files changed

+69
-19
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ You can also import default Woocommerce products that come with wooCommerce Plug
1717

1818
2. Clone or fork the repo and modify ```nextConfig.js```
1919
3. Modify the values according to your setup
20-
21-
Start the server with ```npm run dev ```
20+
4. Start the server with ```npm run dev ```
21+
5. Enable COD (Cash On Demand) payment method in WooCommerce
2222

2323
## Features
2424

components/Cart/CartPage/CartItem.component.jsx

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,56 @@
11
import { useState } from 'react';
2+
import { v4 } from 'uuid';
23

34
import SVGX from 'components/SVG/SVGX.component';
45

5-
const CartItem = ({ item, products, handleRemoveProductClick }) => {
6+
import { getUpdatedItems } from '../../../utils/functions/functions';
7+
8+
const CartItem = ({
9+
item,
10+
products,
11+
handleRemoveProductClick,
12+
updateCart,
13+
}) => {
614
const [productCount, setProductCount] = useState(item.qty);
715

16+
console.log("Update cart: ");
17+
console.log(updateCart);
18+
19+
/*
20+
* When user changes the quantity, update the cart in localStorage
21+
* Also update the cart in the global Context
22+
*
23+
* @param {Object} event cartKey
24+
*
25+
* @return {void}
26+
*/
27+
const handleQuantityChange = (event, cartKey) => {
28+
if (process.browser) {
29+
event.stopPropagation();
30+
// If the previous update cart mutation request is still processing, then return.
31+
32+
/*if (updateCartProcessing) {
33+
return;
34+
}*/
35+
// If the user tries to delete the count of product, set that to 1 by default ( This will not allow him to reduce it less than zero )
36+
const newQty = event.target.value ? parseInt(event.target.value) : 1;
37+
// Set the new qty in state.
38+
setProductCount(newQty);
39+
if (products.length) {
40+
const updatedItems = getUpdatedItems(products, newQty, cartKey);
41+
42+
updateCart({
43+
variables: {
44+
input: {
45+
clientMutationId: v4(),
46+
items: updatedItems,
47+
},
48+
},
49+
});
50+
}
51+
}
52+
};
53+
854
return (
955
<tr className="bg-gray-100">
1056
<td className="px-4 py-2 border">
@@ -35,9 +81,7 @@ const CartItem = ({ item, products, handleRemoveProductClick }) => {
3581
type="number"
3682
min="1"
3783
defaultValue={productCount}
38-
onChange={() => {
39-
console.log('Changed quantity ...');
40-
}}
84+
onChange={(event) => handleQuantityChange(event, item.cartKey)}
4185
/>
4286
</td>
4387

components/Cart/CartPage/CartItemsContainer.component.jsx

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ const CartItemsContainer = () => {
6868
};
6969

7070
const { loading, error, data, refetch } = useQuery(GET_CART, {
71+
notifyOnNetworkStatusChange: true,
7172
onCompleted: () => {
7273
// Update cart in the localStorage.
7374
const updatedCart = getFormattedCart(data);
@@ -80,7 +81,6 @@ const CartItemsContainer = () => {
8081
setRequestError(error);
8182
},
8283
});
83-
// TODO Add more functionality
8484

8585
return (
8686
<>
@@ -91,11 +91,15 @@ const CartItemsContainer = () => {
9191
<PageTitle title="Handlekurv" />
9292
<RegularCart
9393
cart={cart}
94-
handleRemoveProductClick={handleRemoveProductClick}
94+
updateCartProcessing={updateCartProcessing}
95+
handleRemoveProductClick={handleRemoveProductClick}
96+
updateCart={updateCart}
9597
/>
9698
<MobileCart
9799
cart={cart}
98-
handleRemoveProductClick={handleRemoveProductClick}
100+
updateCartProcessing={updateCartProcessing}
101+
handleRemoveProductClick={handleRemoveProductClick}
102+
updateCart={updateCart}
99103
/>
100104
<div className="mt-4">
101105
<Link href="/kasse">
@@ -106,7 +110,7 @@ const CartItemsContainer = () => {
106110
</div>
107111
{updateCartProcessing && (
108112
<>
109-
<div className="mt-4 text-xl text-left">
113+
<div className="mt-4 text-xl text-center">
110114
Oppdaterer antall, vennligst vent ...
111115
<br />
112116
</div>

components/Cart/CartPage/MobileCart.component.jsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import MobileCartItem from './MobileCartItem.component';
22

3-
const MobileCart = ({ cart, handleRemoveProductClick }) => {
3+
const MobileCart = ({ cart, handleRemoveProductClick, updateCart }) => {
44
return (
55
<section
66
//className="container mx-auto bg-white md:hidden lg:hidden xl:hidden"
@@ -124,6 +124,7 @@ const MobileCart = ({ cart, handleRemoveProductClick }) => {
124124
item={item}
125125
products={cart.products}
126126
handleRemoveProductClick={handleRemoveProductClick}
127+
updateCart={updateCart}
127128
/>
128129
))}
129130
</div>

components/Cart/CartPage/RegularCart.component.jsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import CartItem from './CartItem.component';
22

3-
const RegularCart = ({ cart, handleRemoveProductClick }) => {
3+
const RegularCart = ({ cart, handleRemoveProductClick, updateCart }) => {
44
return (
55
<table className="hidden table-auto md:block lg:block xl:block">
66
<thead>
@@ -28,7 +28,8 @@ const RegularCart = ({ cart, handleRemoveProductClick }) => {
2828
key={item.productId}
2929
item={item}
3030
products={cart.products}
31-
handleRemoveProductClick={handleRemoveProductClick}
31+
handleRemoveProductClick={handleRemoveProductClick}
32+
updateCart={updateCart}
3233
/>
3334
))}
3435
</tbody>

components/Category/Categories.component.jsx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,12 @@ import { v4 as uuidv4 } from 'uuid';
88
const Categories = ({ categories }) => {
99
return (
1010
<>
11-
1211
<section className="container mx-auto bg-white">
13-
1412
<div className="flex ">
15-
1613
{categories.map(({ name }) => (
1714
<div
1815
key={uuidv4()}
19-
className="flex flex-col justify-around p-6 xs:w-1/2 md:w-1/3 xl:w-1/4"
16+
className="flex flex-col justify-around p-6 cursor-pointer xs:w-1/2 md:w-1/3 xl:w-1/4"
2017
>
2118
<div className="flex items-center justify-center w-full h-16 text-center border border-gray-300 rounded-lg shadow hover:shadow-outline">
2219
<p className="text-lg">{name}</p>

components/Checkout/Billing.component.jsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ const Billing = ({ input, handleOnChange }) => {
108108
name="paymentMethod"
109109
type="radio"
110110
checked
111+
readOnly
111112
/>
112113
</div>
113114
<div className="w-full p-2">

utils/const/GQL_QUERIES.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,12 @@ export const FETCH_ALL_PRODUCTS_QUERY = gql`
107107
* Fetch all categories from GraphQL
108108
*/
109109
export const FETCH_ALL_CATEGORIES_QUERY = gql`
110-
query MyQuery {
111-
productCategories {
110+
query Categories {
111+
productCategories(first: 10) {
112112
nodes {
113+
id
113114
name
115+
slug
114116
}
115117
}
116118
}

0 commit comments

Comments
 (0)