UNPKG

1.19 kBJavaScriptView Raw
1import { useContext } from 'react';
2import qs from 'qs';
3import cartCheckoutQuery from './CartCheckoutQuery.gql';
4import { CartIdContext } from '../components/Cart/CartIdContext';
5import { useApolloClient } from 'react-apollo';
6function 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}
36export { useLoginRedirect };
37//# sourceMappingURL=useLoginRedirect.js.map
\No newline at end of file