Skip to content

Commit 6f77c90

Browse files
authored
Merge pull request #141 from w3bdesign/development
Development
2 parents 518bb30 + c1f6c58 commit 6f77c90

File tree

11 files changed

+130
-138
lines changed

11 files changed

+130
-138
lines changed

components/Cart/AddToCartButton.component.jsx

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { request } from 'graphql-request';
33
import useSWR from 'swr';
44
import { v4 as uuidv4 } from 'uuid';
55

6-
import { useQuery, useMutation } from '@apollo/client';
6+
import { useQuery, useMutation } from '@apollo/react-hooks';
77

88
import { AppContext } from 'utils/context/AppContext';
99

@@ -37,26 +37,21 @@ const AddToCartButton = (props) => {
3737
productId: product.productId,
3838
};
3939

40-
41-
// Get Cart Data.
42-
const { loading, error, data, refetch } = useQuery( GET_CART, {
43-
notifyOnNetworkStatusChange: true,
44-
onCompleted: () => {
45-
46-
console.log("Data from add to cart button: ");
40+
// Get Cart Data.
41+
const { loading, error, data, refetch } = useQuery(GET_CART, {
42+
notifyOnNetworkStatusChange: true,
43+
onCompleted: () => {
44+
console.log('Data from add to cart button: ');
4745
console.log(data);
48-
// Update cart in the localStorage.
49-
const updatedCart = getFormattedCart( data );
50-
46+
// Update cart in the localStorage.
47+
const updatedCart = getFormattedCart(data);
5148

52-
localStorage.setItem( 'woocommerce-cart', JSON.stringify( updatedCart ) );
49+
localStorage.setItem('woocommerce-cart', JSON.stringify(updatedCart));
5350

54-
// Update cart data in React Context.
55-
setCart( updatedCart );
56-
57-
}
58-
} );
59-
51+
// Update cart data in React Context.
52+
setCart(updatedCart);
53+
},
54+
});
6055

6156
const [
6257
addToCart,

components/Cart/CartPage/CartItemsContainer.component.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import Link from 'next/link';
22
import { v4 } from 'uuid';
33
import { useContext, useState, useEffect } from 'react';
44

5-
import { useQuery, useMutation } from '@apollo/client';
5+
import { useQuery, useMutation } from '@apollo/react-hooks';
66

77
import { AppContext } from 'utils/context/AppContext';
88
import {

components/Checkout/CheckoutForm.component.jsx

Lines changed: 74 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,80 @@
1+
import { useState, useContext, useEffect } from 'react';
2+
import { useQuery, useMutation } from '@apollo/react-hooks';
3+
4+
import { GET_CART } from 'utils/const/GQL_QUERIES';
5+
import { INITIAL_STATE } from 'utils/const/INITIAL_STATE';
6+
import { AppContext } from 'utils/context/AppContext';
7+
8+
import { getFormattedCart } from 'utils/functions/functions';
9+
110
const CheckoutForm = () => {
11+
const [cart, setCart] = useContext(AppContext);
12+
const [input, setInput] = useState(INITIAL_STATE);
13+
const [orderData, setOrderData] = useState(null);
14+
const [requestError, setRequestError] = useState(null);
15+
16+
// Get Cart Data.
17+
const { loading, error, data, refetch } = useQuery(GET_CART, {
18+
notifyOnNetworkStatusChange: true,
19+
onCompleted: () => {
20+
// Update cart in the localStorage.
21+
const updatedCart = getFormattedCart(data);
22+
localStorage.setItem('woocommerce-cart', JSON.stringify(updatedCart));
23+
// Update cart data in React Context.
24+
setCart(updatedCart);
25+
},
26+
});
27+
28+
/*
29+
* Handle form submit.
30+
*
31+
* @param {Object} event Event Object.
32+
*
33+
* @return {void}
34+
*/
35+
const handleFormSubmit = (event) => {
36+
event.preventDefault();
37+
const result = validateAndSanitizeCheckoutForm(input);
38+
if (!result.isValid) {
39+
setInput({ ...input, errors: result.errors });
40+
return;
41+
}
42+
const checkOutData = createCheckoutData(input);
43+
setOrderData(checkOutData);
44+
setRequestError(null);
45+
};
46+
47+
useEffect(() => {
48+
if (null !== orderData) {
49+
// Do checkout mutation when the value for orderData changes.
50+
// checkout();
51+
}
52+
}, [orderData]);
53+
254
return (
355
<>
4-
<section className="py-8 bg-white">
5-
<div className="container flex flex-wrap items-center pt-4 pb-12 mx-auto">
6-
<nav id="store" className="top-0 w-full px-6 py-1">
7-
<div className="container flex flex-wrap items-center justify-between w-full px-2 py-3 mx-auto mt-0">
8-
<a
9-
className="mt-6 text-xl font-bold tracking-wide text-gray-800 no-underline uppercase hover:no-underline"
10-
href="#"
11-
>
12-
Bestillingsskjema
13-
</a>
14-
</div>
15-
</nav>
16-
</div>
17-
</section>
56+
<section className="py-8 bg-white">
57+
<div className="container flex flex-wrap items-center pt-4 pb-12 mx-auto">
58+
<nav id="store" className="top-0 w-full px-6 py-1">
59+
<div className="container flex flex-wrap items-center justify-between w-full px-2 py-3 mx-auto mt-0">
60+
<a
61+
className="mt-6 text-xl font-bold tracking-wide text-gray-800 no-underline uppercase hover:no-underline"
62+
href="#"
63+
>
64+
Bestillingsskjema
65+
</a>
66+
</div>
67+
</nav>
68+
</div>
69+
</section>
70+
71+
{cart ? (
72+
<form onSubmit={handleFormSubmit} className="woo-next-checkout-form">
73+
<div className="row">Skjema kommer her</div>
74+
</form>
75+
) : (
76+
''
77+
)}
1878
</>
1979
);
2080
};

components/Product/IndexProducts.component.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { v4 as uuidv4 } from 'uuid';
44
/**
55
* Displays all of the products as long as length is defined.
66
* Does a map() over the props array and utilizes uuidv4 for unique key values.
7-
* @param {Object} props // Products
7+
* @param {Object} products
88
*/
99
const IndexProducts = ({ products }) => {
1010
return (

components/Product/SingleProduct.component.jsx

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,9 @@ import LoadingSpinner from 'components/LoadingSpinner/LoadingSpinner.component';
66
/**
77
* Shows a single product with an Add To Cart button.
88
* Uses GraphQL for product data
9-
* @param {Object} props // Product data
9+
* @param {Object} product // Product data
1010
*/
11-
const SingleProduct = (props) => {
12-
13-
const { product} = props;
14-
11+
const SingleProduct = ({ product }) => {
1512
const [isLoading, setIsLoading] = useState(true);
1613
useEffect(() => {
1714
// useEffect with empty array is the same as componentDidMount
@@ -30,7 +27,7 @@ const { product} = props;
3027
} = product;
3128

3229
// Strip out HTML from description
33-
const DESCRIPTION_WITHOUT_HTML = description.replace(/(<([^>]+)>)/gi, '');
30+
const DESCRIPTION_WITHOUT_HTML = description.replace(/(<([^>]+)>)/gi, '');
3431

3532
return (
3633
<section className="py-8 bg-white">

package-lock.json

Lines changed: 15 additions & 27 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"author": "",
1313
"license": "ISC",
1414
"dependencies": {
15-
"@apollo/client": "^3.0.0-rc.2",
15+
"@apollo/react-hooks": "^3.1.5",
1616
"algoliasearch": "^4.2.0",
1717
"apollo-boost": "^0.4.9",
1818
"apollo-cache-inmemory": "^1.6.6",

pages/_app.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ApolloProvider } from '@apollo/client';
1+
import { ApolloProvider } from '@apollo/react-hooks';
22

33
import { AppProvider } from 'utils/context/AppContext';
44

pages/index.js

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,6 @@ export async function getServerSideProps() {
4747
query: FETCH_ALL_PRODUCTS_QUERY,
4848
});
4949

50-
//console.log("getServerSideProps data: ")
51-
//console.log(data)
52-
console.log('getServerSideProps networkStatus: ');
53-
console.log(networkStatus);
54-
console.log('getServerSideProps loading: ');
55-
console.log(loading);
56-
5750
return {
5851
props: {
5952
// products: result.data.products.nodes,

utils/const/INITIAL_PRODUCTS.js

Lines changed: 0 additions & 62 deletions
This file was deleted.

0 commit comments

Comments
 (0)