Skip to content

Commit 8af2c41

Browse files
committed
Move initial checkout form state to a separate file to make component cleaner
1 parent 4a4cd5b commit 8af2c41

File tree

3 files changed

+45
-83
lines changed

3 files changed

+45
-83
lines changed

components/Checkout/CheckoutForm.component.jsx

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { useState, useContext, useEffect } from 'react';
22
import { useQuery, useMutation } from '@apollo/client';
33

44
import { GET_CART } from 'utils/const/GQL_QUERIES';
5+
import { INITIAL_STATE } from 'utils/const/INITIAL_STATE';
56
import { AppContext } from 'utils/context/AppContext';
67

78
import {
@@ -11,23 +12,10 @@ import {
1112
} from 'utils/functions/functions';
1213

1314
const CheckoutForm = () => {
14-
const initialState = {
15-
firstName: 'Test',
16-
lastName: 'Test',
17-
address1: 'Test addresse',
18-
address2: 'Test addresse',
19-
city: 'Oslo',
20-
state: 'Oslo',
21-
country: 'NO',
22-
postcode: '1525',
23-
phone: '90561212',
24-
25-
company: 'Tech',
26-
createAccount: false,
27-
orderNotes: '',
28-
paymentMethod: 'cod',
29-
errors: null,
30-
};
15+
const [cart, setCart] = useContext(AppContext);
16+
const [input, setInput] = useState(INITIAL_STATE);
17+
const [orderData, setOrderData] = useState(null);
18+
const [requestError, setRequestError] = useState(null);
3119

3220
// Get Cart Data.
3321
const { loading, error, data, refetch } = useQuery(GET_CART, {
@@ -41,17 +29,32 @@ const CheckoutForm = () => {
4129
},
4230
});
4331

32+
/*
33+
* Handle form submit.
34+
*
35+
* @param {Object} event Event Object.
36+
*
37+
* @return {void}
38+
*/
39+
const handleFormSubmit = (event) => {
40+
event.preventDefault();
41+
const result = validateAndSanitizeCheckoutForm(input);
42+
if (!result.isValid) {
43+
setInput({ ...input, errors: result.errors });
44+
return;
45+
}
46+
const checkOutData = createCheckoutData(input);
47+
setOrderData(checkOutData);
48+
setRequestError(null);
49+
};
50+
4451
useEffect(() => {
4552
if (null !== orderData) {
4653
// Call the checkout mutation when the value for orderData changes/updates.
4754
// checkout();
4855
}
4956
}, [orderData]);
5057

51-
const [cart, setCart] = useContext(AppContext);
52-
const [input, setInput] = useState(initialState);
53-
const [orderData, setOrderData] = useState(null);
54-
const [requestError, setRequestError] = useState(null);
5558
return (
5659
<>
5760
<section className="py-8 bg-white">

utils/const/INITIAL_PRODUCTS.js

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

utils/const/INITIAL_STATE.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* Provides static object with initial state upon loading the checkout form
3+
* Good for testing.
4+
*/
5+
export const INITIAL_STATE = {
6+
firstName: 'Test',
7+
lastName: 'Test',
8+
address1: 'Test addresse',
9+
address2: 'Test addresse',
10+
city: 'Oslo',
11+
state: 'Oslo',
12+
country: 'NO',
13+
postcode: '1525',
14+
phone: '90561212',
15+
16+
company: 'Tech',
17+
createAccount: false,
18+
orderNotes: '',
19+
paymentMethod: 'cod',
20+
errors: null,
21+
};

0 commit comments

Comments
 (0)