Skip to content

Commit 059c46f

Browse files
committed
Updating Components to set Context and Adding Redirection Logics
1 parent b2f48a0 commit 059c46f

File tree

4 files changed

+68
-3
lines changed

4 files changed

+68
-3
lines changed

packages/peregrine/lib/talons/CheckoutPage/OrderConfirmationPage/useCreateAccount.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { useGoogleReCaptcha } from '../../../hooks/useGoogleReCaptcha';
99

1010
import DEFAULT_OPERATIONS from './createAccount.gql';
1111
import { useEventingContext } from '../../../context/eventing';
12+
import { useHistory } from 'react-router-dom';
1213

1314
/**
1415
* Returns props necessary to render CreateAccount component. In particular this
@@ -95,6 +96,7 @@ export const useCreateAccount = props => {
9596
formAction: 'createAccount'
9697
});
9798

99+
const history = useHistory();
98100
const handleSubmit = useCallback(
99101
async formValues => {
100102
setIsSubmitting(true);
@@ -158,6 +160,8 @@ export const useCreateAccount = props => {
158160
if (onSubmit) {
159161
onSubmit();
160162
}
163+
164+
history.push('/account-information');
161165
} catch (error) {
162166
if (process.env.NODE_ENV !== 'production') {
163167
console.error(error);

packages/peregrine/lib/talons/CheckoutPage/OrderConfirmationPage/useOrderConfirmationPage.js

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import { useEffect } from 'react';
22
import { useUserContext } from '../../../context/user';
3+
import { setUserOnOrderSuccess } from '../../../store/actions/user/asyncActions';
34
import { useLazyQuery } from '@apollo/client';
45

56
import mergeOperations from '../../../util/shallowMerge';
67
import DEFAULT_OPERATIONS from './orderConfirmationPage.gql';
8+
import { useDispatch } from 'react-redux';
79

810
export const flattenGuestCartData = data => {
911
if (!data) {
@@ -34,8 +36,26 @@ export const flattenCustomerOrderData = data => {
3436
if (!data) {
3537
return;
3638
}
39+
3740
const { customer } = data;
38-
const order = customer.orders.items[0];
41+
const order = customer?.orders?.items?.[0];
42+
if (!order || !order.shipping_address) {
43+
// Return an empty response if no valid order or shipping address exists
44+
return;
45+
46+
//Below code can be uncommented in case of wanting to show the order success page with blank shipping address.
47+
// return {
48+
// city: '',
49+
// country: '',
50+
// email: customer.email,
51+
// firstname: '',
52+
// lastname: '',
53+
// postcode: '',
54+
// region: '',
55+
// street: ['',''],
56+
// shippingMethod: ''
57+
// };
58+
}
3959
const { shipping_address: address } = order;
4060

4161
return {
@@ -65,6 +85,8 @@ export const useOrderConfirmationPage = props => {
6585
const flatData =
6686
flattenGuestCartData(props.data) || flattenCustomerOrderData(queryData);
6787

88+
const dispatch = useDispatch();
89+
6890
useEffect(() => {
6991
if (props.orderNumber && !props.data) {
7092
const orderNumber = props.orderNumber;
@@ -74,6 +96,13 @@ export const useOrderConfirmationPage = props => {
7496
}
7597
});
7698
}
99+
100+
dispatch(setUserOnOrderSuccess(true));
101+
102+
return () => {
103+
// Reset the flag when leaving the page
104+
dispatch(setUserOnOrderSuccess(false));
105+
};
77106
}, [props.orderNumber, props.data, fetchOrderConfirmationDetails]);
78107

79108
return {

packages/peregrine/lib/talons/CreateAccount/useCreateAccount.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ import { useGoogleReCaptcha } from '../../hooks/useGoogleReCaptcha';
1010

1111
import DEFAULT_OPERATIONS from './createAccount.gql';
1212
import { useEventingContext } from '../../context/eventing';
13+
import { useHistory, useLocation } from 'react-router-dom';
14+
15+
/**
16+
* Routes to redirect from if used to create an account.
17+
*/
18+
const REDIRECT_FOR_ROUTES = ['/checkout', '/order-confirmation'];
1319

1420
/**
1521
* Returns props necessary to render CreateAccount component. In particular this
@@ -47,7 +53,7 @@ export const useCreateAccount = props => {
4753
{ createCart, removeCart, getCartDetails }
4854
] = useCartContext();
4955
const [
50-
{ isGettingDetails },
56+
{ isGettingDetails, userOnOrderSuccess },
5157
{ getUserDetails, setToken }
5258
] = useUserContext();
5359

@@ -112,6 +118,9 @@ export const useCreateAccount = props => {
112118
};
113119
}, [handleCancel]);
114120

121+
const history = useHistory();
122+
const location = useLocation();
123+
115124
const handleSubmit = useCallback(
116125
async formValues => {
117126
setIsSubmitting(true);
@@ -188,6 +197,13 @@ export const useCreateAccount = props => {
188197
if (onSubmit) {
189198
onSubmit();
190199
}
200+
201+
if (
202+
userOnOrderSuccess &&
203+
REDIRECT_FOR_ROUTES.includes(location.pathname)
204+
) {
205+
history.push('/account-information');
206+
}
191207
} catch (error) {
192208
if (process.env.NODE_ENV !== 'production') {
193209
console.error(error);

packages/peregrine/lib/talons/SignIn/useSignIn.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ import { retrieveCartId } from '../../store/actions/cart';
1010

1111
import DEFAULT_OPERATIONS from './signIn.gql';
1212
import { useEventingContext } from '../../context/eventing';
13+
import { useHistory, useLocation } from 'react-router-dom';
14+
15+
/**
16+
* Routes to redirect from if used to create an account.
17+
*/
18+
const REDIRECT_FOR_ROUTES = ['/checkout', '/order-confirmation'];
1319

1420
export const useSignIn = props => {
1521
const {
@@ -40,7 +46,7 @@ export const useSignIn = props => {
4046

4147
const userContext = useUserContext();
4248
const [
43-
{ isGettingDetails, getDetailsError },
49+
{ isGettingDetails, getDetailsError, userOnOrderSuccess },
4450
{ getUserDetails, setToken }
4551
] = userContext;
4652

@@ -83,6 +89,9 @@ export const useSignIn = props => {
8389
const formApiRef = useRef(null);
8490
const setFormApi = useCallback(api => (formApiRef.current = api), []);
8591

92+
const history = useHistory();
93+
const location = useLocation();
94+
8695
const handleSubmit = useCallback(
8796
async ({ email, password }) => {
8897
setIsSigningIn(true);
@@ -144,6 +153,13 @@ export const useSignIn = props => {
144153
});
145154

146155
getCartDetails({ fetchCartId, fetchCartDetails });
156+
157+
if (
158+
userOnOrderSuccess &&
159+
REDIRECT_FOR_ROUTES.includes(location.pathname)
160+
) {
161+
history.push('/order-history');
162+
}
147163
} catch (error) {
148164
if (process.env.NODE_ENV !== 'production') {
149165
console.error(error);

0 commit comments

Comments
 (0)