Skip to content

Commit 4598acc

Browse files
committed
Improve logic in checkout form
1 parent 2e6d538 commit 4598acc

File tree

2 files changed

+42
-6
lines changed

2 files changed

+42
-6
lines changed

refactor/src/components/Checkout/CheckoutForm.component.tsx

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*eslint complexity: ["error", 20]*/
22
// Imports
33
import { useState, useContext, useEffect } from 'react';
4-
import { useQuery, useMutation } from '@apollo/client';
4+
import { useQuery, useMutation, ApolloError } from '@apollo/client';
55

66
// Components
77
import Billing from './Billing.component';
@@ -20,11 +20,41 @@ import {
2020
ICheckoutDataProps,
2121
} from '@/utils/functions/functions';
2222

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+
2353
const CheckoutForm = () => {
2454
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);
2858

2959
// Get cart data query
3060
const { data, refetch } = useQuery(GET_CART, {
@@ -84,6 +114,7 @@ const CheckoutForm = () => {
84114

85115
const onSubmit = (submitData: ICheckoutDataProps) => {
86116
const checkOutData = createCheckoutData(submitData);
117+
87118
setOrderData(checkOutData);
88119
setRequestError(null);
89120
};
@@ -112,6 +143,11 @@ const CheckoutForm = () => {
112143
</div>
113144
) : (
114145
<>
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+
)}
115151
{orderCompleted && (
116152
<div className="container h-24 m-12 mx-auto mt-32 text-xl text-center">
117153
Takk for din ordre!

refactor/src/utils/functions/functions.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,9 @@ export interface ICheckoutDataProps {
7070
city: string;
7171
country: string;
7272
state: string;
73-
postcode: number;
73+
postcode: string;
7474
email: string;
75-
phone: number;
75+
phone: string;
7676
company: string;
7777
paymentMethod: string;
7878
}

0 commit comments

Comments
 (0)