1
1
/*eslint complexity: ["error", 20]*/
2
2
// Imports
3
3
import { useState , useContext , useEffect } from 'react' ;
4
- import { useQuery , useMutation } from '@apollo/client' ;
4
+ import { useQuery , useMutation , ApolloError } from '@apollo/client' ;
5
5
6
6
// Components
7
7
import Billing from './Billing.component' ;
@@ -20,11 +20,41 @@ import {
20
20
ICheckoutDataProps ,
21
21
} from '@/utils/functions/functions' ;
22
22
23
+ export interface IBilling {
24
+ firstName : string ;
25
+ lastName : string ;
26
+ address1 : string ;
27
+ city : string ;
28
+ postcode : string ;
29
+ email : string ;
30
+ phone : string ;
31
+ }
32
+
33
+ export interface IShipping {
34
+ firstName : string ;
35
+ lastName : string ;
36
+ address1 : string ;
37
+ city : string ;
38
+ postcode : string ;
39
+ email : string ;
40
+ phone : string ;
41
+ }
42
+
43
+ export interface ICheckoutData {
44
+ clientMutationId : string ;
45
+ billing : IBilling ;
46
+ shipping : IShipping ;
47
+ shipToDifferentAddress : boolean ;
48
+ paymentMethod : string ;
49
+ isPaid : boolean ;
50
+ transactionId : string ;
51
+ }
52
+
23
53
const CheckoutForm = ( ) => {
24
54
const { cart, setCart } = useContext ( CartContext ) ;
25
- const [ orderData , setOrderData ] = useState < any > ( null ) ;
26
- const [ requestError , setRequestError ] = useState < any > ( null ) ;
27
- const [ orderCompleted , setorderCompleted ] = useState ( false ) ;
55
+ const [ orderData , setOrderData ] = useState < ICheckoutData | null > ( null ) ;
56
+ const [ requestError , setRequestError ] = useState < ApolloError | null > ( null ) ;
57
+ const [ orderCompleted , setorderCompleted ] = useState < boolean > ( false ) ;
28
58
29
59
// Get cart data query
30
60
const { data, refetch } = useQuery ( GET_CART , {
@@ -84,6 +114,7 @@ const CheckoutForm = () => {
84
114
85
115
const onSubmit = ( submitData : ICheckoutDataProps ) => {
86
116
const checkOutData = createCheckoutData ( submitData ) ;
117
+
87
118
setOrderData ( checkOutData ) ;
88
119
setRequestError ( null ) ;
89
120
} ;
@@ -112,6 +143,11 @@ const CheckoutForm = () => {
112
143
</ div >
113
144
) : (
114
145
< >
146
+ { ! data ?. cart ?. contents ?. nodes . length && ! orderCompleted && (
147
+ < h1 className = "text-2xl m-12 mt-32 font-bold text-center" >
148
+ Ingen produkter i handlekurven
149
+ </ h1 >
150
+ ) }
115
151
{ orderCompleted && (
116
152
< div className = "container h-24 m-12 mx-auto mt-32 text-xl text-center" >
117
153
Takk for din ordre!
0 commit comments