Skip to content

Commit 1d030f2

Browse files
committed
Add Checkout mutation
1 parent 67abda1 commit 1d030f2

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

components/Checkout/CheckoutForm.component.jsx

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

44
import { GET_CART } from 'utils/const/GQL_QUERIES';
5+
import { CHECKOUT_MUTATION } from 'utils/const/GQL_MUTATIONS';
56
import { INITIAL_STATE } from 'utils/const/INITIAL_STATE';
7+
68
import { AppContext } from 'utils/context/AppContext';
79

810
import { getFormattedCart } from 'utils/functions/functions';
@@ -13,6 +15,24 @@ const CheckoutForm = () => {
1315
const [orderData, setOrderData] = useState(null);
1416
const [requestError, setRequestError] = useState(null);
1517

18+
// Checkout GraphQL mutation
19+
const [
20+
checkout,
21+
{ data: checkoutResponse, loading: checkoutLoading, error: checkoutError },
22+
] = useMutation(CHECKOUT_MUTATION, {
23+
variables: {
24+
input: orderData,
25+
},
26+
onCompleted: () => {
27+
refetch();
28+
},
29+
onError: (error) => {
30+
if (error) {
31+
setRequestError(error);
32+
}
33+
},
34+
});
35+
1636
// Get Cart Data.
1737
const { loading, error, data, refetch } = useQuery(GET_CART, {
1838
notifyOnNetworkStatusChange: true,
@@ -46,8 +66,8 @@ const CheckoutForm = () => {
4666

4767
useEffect(() => {
4868
if (null !== orderData) {
49-
// Do checkout mutation when the value for orderData changes.
50-
// checkout();
69+
// Perform checkout mutation when the value for orderData changes.
70+
checkout();
5171
}
5272
}, [orderData]);
5373

utils/const/GQL_MUTATIONS.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,23 @@ export const ADD_TO_CART = gql`
6060
}
6161
}
6262
`;
63+
64+
export const CHECKOUT_MUTATION = gql`
65+
mutation CHECKOUT_MUTATION( $input: CheckoutInput! ) {
66+
checkout(input: $input) {
67+
clientMutationId
68+
order {
69+
id
70+
orderId
71+
refunds {
72+
nodes {
73+
amount
74+
}
75+
}
76+
status
77+
}
78+
result
79+
redirect
80+
}
81+
}
82+
`;

0 commit comments

Comments
 (0)