1 | import { useContext } from 'react';
|
2 | import qs from 'qs';
|
3 | import cartCheckoutQuery from './CartCheckoutQuery.gql';
|
4 | import { CartIdContext } from '../components/Cart/CartIdContext';
|
5 | import { useApolloClient } from 'react-apollo';
|
6 | function useLoginRedirect() {
|
7 | const { ReturnURL } = __IN_SERVER__
|
8 | ? { ReturnURL: null }
|
9 | : qs.parse(window.location.search, {
|
10 | ignoreQueryPrefix: true
|
11 | });
|
12 | const { cartId } = useContext(CartIdContext);
|
13 | const client = useApolloClient();
|
14 | function getRedirectUrl() {
|
15 | if (!ReturnURL)
|
16 | return;
|
17 | if (ReturnURL.startsWith('/checkout')) {
|
18 | return client
|
19 | .query({
|
20 | query: cartCheckoutQuery,
|
21 | variables: {
|
22 | cartId
|
23 | }
|
24 | })
|
25 | .then(result => {
|
26 | const externalCheckoutUrl = result.data.cart.externalCheckoutUrl;
|
27 | return externalCheckoutUrl;
|
28 | });
|
29 | }
|
30 | else {
|
31 | return Promise.resolve(ReturnURL);
|
32 | }
|
33 | }
|
34 | return { getRedirectUrl };
|
35 | }
|
36 | export { useLoginRedirect };
|
37 |
|
\ | No newline at end of file |