@@ -2,6 +2,7 @@ import { useState, useContext, useEffect } from 'react';
2
2
import { useQuery , useMutation } from '@apollo/client' ;
3
3
4
4
import { GET_CART } from 'utils/const/GQL_QUERIES' ;
5
+ import { INITIAL_STATE } from 'utils/const/INITIAL_STATE' ;
5
6
import { AppContext } from 'utils/context/AppContext' ;
6
7
7
8
import {
@@ -11,23 +12,10 @@ import {
11
12
} from 'utils/functions/functions' ;
12
13
13
14
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 ) ;
31
19
32
20
// Get Cart Data.
33
21
const { loading, error, data, refetch } = useQuery ( GET_CART , {
@@ -41,17 +29,32 @@ const CheckoutForm = () => {
41
29
} ,
42
30
} ) ;
43
31
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
+
44
51
useEffect ( ( ) => {
45
52
if ( null !== orderData ) {
46
53
// Call the checkout mutation when the value for orderData changes/updates.
47
54
// checkout();
48
55
}
49
56
} , [ orderData ] ) ;
50
57
51
- const [ cart , setCart ] = useContext ( AppContext ) ;
52
- const [ input , setInput ] = useState ( initialState ) ;
53
- const [ orderData , setOrderData ] = useState ( null ) ;
54
- const [ requestError , setRequestError ] = useState ( null ) ;
55
58
return (
56
59
< >
57
60
< section className = "py-8 bg-white" >
0 commit comments