@magento/peregrine 14.3.1 → 14.4.1-alpha.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,6 @@
1
1
  import { createHttpLink } from '@apollo/client';
2
2
 
3
+ import createAuthLink from '@magento/peregrine/lib/Apollo/links/authLink';
3
4
  import createErrorLink from '@magento/peregrine/lib/Apollo/links/errorLink';
4
5
  import createGqlCacheLink from '@magento/peregrine/lib/Apollo/links/gqlCacheLink';
5
6
  import createMutationQueueLink from '@magento/peregrine/lib/Apollo/links/mutationQueueLink';
@@ -31,6 +32,7 @@ export const customFetchToShrinkQuery = (uri, options) => {
31
32
  };
32
33
 
33
34
  const getLinks = apiBase => {
35
+ const authLink = createAuthLink();
34
36
  const storeLink = createStoreLink();
35
37
  const errorLink = createErrorLink();
36
38
  const retryLink = createRetryLink();
@@ -52,6 +54,7 @@ const getLinks = apiBase => {
52
54
  const links = new Map()
53
55
  .set('MUTATION_QUEUE', mutationQueueLink)
54
56
  .set('RETRY', retryLink)
57
+ .set('AUTH', authLink)
55
58
  .set('GQL_CACHE', gqlCacheLink)
56
59
  .set('STORE', storeLink)
57
60
  .set('ERROR', errorLink)
@@ -63,13 +63,17 @@ export const resetPassword = ({ email }) =>
63
63
  dispatch(actions.resetPassword.receive());
64
64
  };
65
65
 
66
- export const setToken = token =>
66
+ export const setToken = (token, customerAccessTokenLifetime = 1) =>
67
67
  async function thunk(...args) {
68
68
  const [dispatch] = args;
69
69
 
70
70
  // Store token in local storage.
71
71
  // TODO: Get correct token expire time from API
72
- storage.setItem('signin_token', token, 3600);
72
+ storage.setItem(
73
+ 'signin_token',
74
+ token,
75
+ customerAccessTokenLifetime * 3600
76
+ );
73
77
 
74
78
  // Persist in store
75
79
  dispatch(actions.setToken(token));
@@ -1,4 +1,5 @@
1
1
  import { gql } from '@apollo/client';
2
+ import { GET_STORE_CONFIG_DATA } from '../../CreateAccount/createAccount.gql';
2
3
 
3
4
  export const CREATE_ACCOUNT = gql`
4
5
  mutation CreateAccountAfterCheckout(
@@ -110,5 +111,6 @@ export default {
110
111
  createCartMutation: CREATE_CART,
111
112
  getCartDetailsQuery: GET_CART_DETAILS,
112
113
  getCustomerQuery: GET_CUSTOMER,
113
- signInMutation: SIGN_IN
114
+ signInMutation: SIGN_IN,
115
+ getStoreConfigQuery: GET_STORE_CONFIG_DATA
114
116
  };
@@ -1,5 +1,5 @@
1
1
  import { useCallback, useMemo, useState } from 'react';
2
- import { useMutation } from '@apollo/client';
2
+ import { useMutation, useQuery } from '@apollo/client';
3
3
 
4
4
  import mergeOperations from '../../../util/shallowMerge';
5
5
  import { useUserContext } from '../../../context/user';
@@ -39,7 +39,8 @@ export const useCreateAccount = props => {
39
39
  createCartMutation,
40
40
  getCartDetailsQuery,
41
41
  getCustomerQuery,
42
- signInMutation
42
+ signInMutation,
43
+ getStoreConfigQuery
43
44
  } = operations;
44
45
  const [isSubmitting, setIsSubmitting] = useState(false);
45
46
  const [, { createCart, getCartDetails, removeCart }] = useCartContext();
@@ -65,6 +66,23 @@ export const useCreateAccount = props => {
65
66
  fetchPolicy: 'no-cache'
66
67
  });
67
68
 
69
+ const { data: storeConfigData } = useQuery(getStoreConfigQuery, {
70
+ fetchPolicy: 'cache-and-network',
71
+ nextFetchPolicy: 'cache-first'
72
+ });
73
+
74
+ const {
75
+ minimumPasswordLength,
76
+ customerAccessTokenLifetime
77
+ } = useMemo(() => {
78
+ const storeConfig = storeConfigData?.storeConfig || {};
79
+
80
+ return {
81
+ minimumPasswordLength: storeConfig.minimum_password_length,
82
+ customerAccessTokenLifetime:
83
+ storeConfig.customer_access_token_lifetime
84
+ };
85
+ }, [storeConfigData]);
68
86
  const fetchUserDetails = useAwaitQuery(getCustomerQuery);
69
87
  const fetchCartDetails = useAwaitQuery(getCartDetailsQuery);
70
88
 
@@ -117,7 +135,9 @@ export const useCreateAccount = props => {
117
135
  ...recaptchaDataForSignIn
118
136
  });
119
137
  const token = signInResponse.data.generateCustomerToken.token;
120
- await setToken(token);
138
+ await (customerAccessTokenLifetime
139
+ ? setToken(token, customerAccessTokenLifetime)
140
+ : setToken(token));
121
141
 
122
142
  // Clear guest cart from redux.
123
143
  await removeCart();
@@ -146,6 +166,7 @@ export const useCreateAccount = props => {
146
166
  }
147
167
  },
148
168
  [
169
+ customerAccessTokenLifetime,
149
170
  createAccount,
150
171
  createCart,
151
172
  fetchCartDetails,
@@ -194,6 +215,7 @@ export const useCreateAccount = props => {
194
215
  handleEnterKeyPress,
195
216
  isDisabled: isSubmitting || isGettingDetails || recaptchaLoading,
196
217
  initialValues: sanitizedInitialValues,
197
- recaptchaWidgetProps
218
+ recaptchaWidgetProps,
219
+ minimumPasswordLength
198
220
  };
199
221
  };
@@ -125,6 +125,16 @@ export const MERGE_CARTS = gql`
125
125
  }
126
126
  ${CheckoutPageFragment}
127
127
  `;
128
+ export const GET_STORE_CONFIG_DATA = gql`
129
+ query GetStoreConfigData {
130
+ # eslint-disable-next-line @graphql-eslint/require-id-when-available
131
+ storeConfig {
132
+ store_code
133
+ minimum_password_length
134
+ customer_access_token_lifetime
135
+ }
136
+ }
137
+ `;
128
138
 
129
139
  export default {
130
140
  createAccountMutation: CREATE_ACCOUNT,
@@ -132,5 +142,6 @@ export default {
132
142
  getCartDetailsQuery: GET_CART_DETAILS,
133
143
  getCustomerQuery: GET_CUSTOMER,
134
144
  mergeCartsMutation: MERGE_CARTS,
135
- signInMutation: SIGN_IN
145
+ signInMutation: SIGN_IN,
146
+ getStoreConfigQuery: GET_STORE_CONFIG_DATA
136
147
  };
@@ -1,5 +1,5 @@
1
1
  import { useCallback, useMemo, useState } from 'react';
2
- import { useApolloClient, useMutation } from '@apollo/client';
2
+ import { useApolloClient, useMutation, useQuery } from '@apollo/client';
3
3
 
4
4
  import mergeOperations from '../../util/shallowMerge';
5
5
  import { useUserContext } from '../../context/user';
@@ -37,7 +37,8 @@ export const useCreateAccount = props => {
37
37
  getCartDetailsQuery,
38
38
  getCustomerQuery,
39
39
  mergeCartsMutation,
40
- signInMutation
40
+ signInMutation,
41
+ getStoreConfigQuery
41
42
  } = operations;
42
43
  const apolloClient = useApolloClient();
43
44
  const [isSubmitting, setIsSubmitting] = useState(false);
@@ -69,6 +70,24 @@ export const useCreateAccount = props => {
69
70
  fetchPolicy: 'no-cache'
70
71
  });
71
72
 
73
+ const { data: storeConfigData } = useQuery(getStoreConfigQuery, {
74
+ fetchPolicy: 'cache-and-network',
75
+ nextFetchPolicy: 'cache-first'
76
+ });
77
+
78
+ const {
79
+ minimumPasswordLength,
80
+ customerAccessTokenLifetime
81
+ } = useMemo(() => {
82
+ const storeConfig = storeConfigData?.storeConfig || {};
83
+
84
+ return {
85
+ minimumPasswordLength: storeConfig.minimum_password_length,
86
+ customerAccessTokenLifetime:
87
+ storeConfig.customer_access_token_lifetime
88
+ };
89
+ }, [storeConfigData]);
90
+
72
91
  const fetchUserDetails = useAwaitQuery(getCustomerQuery);
73
92
  const fetchCartDetails = useAwaitQuery(getCartDetailsQuery);
74
93
 
@@ -136,8 +155,9 @@ export const useCreateAccount = props => {
136
155
  ...recaptchaDataForSignIn
137
156
  });
138
157
  const token = signInResponse.data.generateCustomerToken.token;
139
- await setToken(token);
140
-
158
+ await (customerAccessTokenLifetime
159
+ ? setToken(token, customerAccessTokenLifetime)
160
+ : setToken(token));
141
161
  // Clear all cart/customer data from cache and redux.
142
162
  await apolloClient.clearCacheData(apolloClient, 'cart');
143
163
  await apolloClient.clearCacheData(apolloClient, 'customer');
@@ -177,6 +197,7 @@ export const useCreateAccount = props => {
177
197
  },
178
198
 
179
199
  [
200
+ customerAccessTokenLifetime,
180
201
  cartId,
181
202
  generateReCaptchaData,
182
203
  createAccount,
@@ -221,7 +242,8 @@ export const useCreateAccount = props => {
221
242
  handleCancelKeyPress,
222
243
  initialValues: sanitizedInitialValues,
223
244
  isDisabled: isSubmitting || isGettingDetails || recaptchaLoading,
224
- recaptchaWidgetProps
245
+ recaptchaWidgetProps,
246
+ minimumPasswordLength
225
247
  };
226
248
  };
227
249
 
@@ -235,6 +257,7 @@ export const useCreateAccount = props => {
235
257
  *
236
258
  * @property {GraphQLAST} customerQuery query to fetch customer details
237
259
  * @property {GraphQLAST} getCartDetailsQuery query to get cart details
260
+ * @property {GraphQLAST} getStoreConfigQuery query to get store config
238
261
  */
239
262
 
240
263
  /**
@@ -1,4 +1,4 @@
1
- import { useCallback, useEffect, useRef } from 'react';
1
+ import { useCallback, useEffect, useRef, useState } from 'react';
2
2
  import { useHistory, useLocation } from 'react-router-dom';
3
3
  import { useLazyQuery } from '@apollo/client';
4
4
  import { useRootComponents } from '../../context/rootComponents';
@@ -44,9 +44,11 @@ export const useMagentoRoute = (props = {}) => {
44
44
  const component = componentMap.get(pathname);
45
45
 
46
46
  const [runQuery, queryResult] = useLazyQuery(resolveUrlQuery);
47
+
47
48
  // destructure the query result
48
49
  const { data, error, loading } = queryResult;
49
- const { route } = data || {};
50
+ const [getRouteData, setRouteData] = useState(data);
51
+ const { route } = getRouteData || {};
50
52
 
51
53
  // redirect to external url
52
54
  useEffect(() => {
@@ -59,15 +61,34 @@ export const useMagentoRoute = (props = {}) => {
59
61
  }, [route]);
60
62
 
61
63
  useEffect(() => {
64
+ let isMounted = true; // Track whether the component is still mounted
65
+ const runInitialQuery = async () => {
66
+ try {
67
+ const { data } = await runQuery({
68
+ fetchPolicy: 'cache-and-network',
69
+ nextFetchPolicy: 'cache-first',
70
+ variables: { url: pathname }
71
+ });
72
+
73
+ if (isMounted) {
74
+ setRouteData(data);
75
+ fetchedPathname.current = pathname;
76
+ }
77
+ } catch (error) {
78
+ if (isMounted) {
79
+ console.error('Error running initial query:', error);
80
+ }
81
+ }
82
+ };
62
83
  if (initialized.current || !getInlinedPageData()) {
63
- runQuery({
64
- fetchPolicy: 'cache-and-network',
65
- nextFetchPolicy: 'cache-first',
66
- variables: { url: pathname }
67
- });
68
- fetchedPathname.current = pathname;
84
+ runInitialQuery();
69
85
  }
70
- }, [initialized, pathname]); // eslint-disable-line react-hooks/exhaustive-deps
86
+ // Cleanup function
87
+ return () => {
88
+ isMounted = false; // Mark as unmounted
89
+ };
90
+ }, [initialized, pathname, runQuery]);
91
+ // eslint-disable-line react-hooks/exhaustive-deps
71
92
 
72
93
  useEffect(() => {
73
94
  if (component) {
@@ -144,8 +144,8 @@ const getBreadcrumbCategoryId = categories => {
144
144
  const breadcrumbSet = new Set();
145
145
  categories.forEach(({ breadcrumbs }) => {
146
146
  // breadcrumbs can be `null`...
147
- (breadcrumbs || []).forEach(({ category_id }) =>
148
- breadcrumbSet.add(category_id)
147
+ (breadcrumbs || []).forEach(({ category_uid }) =>
148
+ breadcrumbSet.add(category_uid)
149
149
  );
150
150
  });
151
151
 
@@ -1,5 +1,6 @@
1
1
  import { gql } from '@apollo/client';
2
2
  import { CheckoutPageFragment } from '../CheckoutPage/checkoutPageFragments.gql';
3
+ import { GET_STORE_CONFIG_DATA } from '../CreateAccount/createAccount.gql';
3
4
 
4
5
  export const GET_CUSTOMER = gql`
5
6
  query GetCustomerAfterSignIn {
@@ -51,5 +52,6 @@ export default {
51
52
  createCartMutation: CREATE_CART,
52
53
  getCustomerQuery: GET_CUSTOMER,
53
54
  mergeCartsMutation: MERGE_CARTS,
54
- signInMutation: SIGN_IN
55
+ signInMutation: SIGN_IN,
56
+ getStoreConfigQuery: GET_STORE_CONFIG_DATA
55
57
  };
@@ -1,5 +1,5 @@
1
1
  import { useCallback, useRef, useState, useMemo } from 'react';
2
- import { useApolloClient, useMutation } from '@apollo/client';
2
+ import { useApolloClient, useMutation, useQuery } from '@apollo/client';
3
3
 
4
4
  import { useGoogleReCaptcha } from '../../hooks/useGoogleReCaptcha/useGoogleReCaptcha';
5
5
  import mergeOperations from '../../util/shallowMerge';
@@ -25,37 +25,56 @@ export const useSignIn = props => {
25
25
  createCartMutation,
26
26
  getCustomerQuery,
27
27
  mergeCartsMutation,
28
- signInMutation
28
+ signInMutation,
29
+ getStoreConfigQuery
29
30
  } = operations;
30
31
 
31
32
  const apolloClient = useApolloClient();
32
33
  const [isSigningIn, setIsSigningIn] = useState(false);
33
34
 
35
+ const cartContext = useCartContext();
34
36
  const [
35
37
  { cartId },
36
38
  { createCart, removeCart, getCartDetails }
37
- ] = useCartContext();
39
+ ] = cartContext;
38
40
 
41
+ const userContext = useUserContext();
39
42
  const [
40
43
  { isGettingDetails, getDetailsError },
41
44
  { getUserDetails, setToken }
42
- ] = useUserContext();
45
+ ] = userContext;
43
46
 
44
- const [, { dispatch }] = useEventingContext();
47
+ const eventingContext = useEventingContext();
48
+ const [, { dispatch }] = eventingContext;
45
49
 
46
- const [signIn, { error: signInError }] = useMutation(signInMutation, {
50
+ const signInMutationResult = useMutation(signInMutation, {
47
51
  fetchPolicy: 'no-cache'
48
52
  });
53
+ const [signIn, { error: signInError }] = signInMutationResult;
49
54
 
55
+ const googleReCaptcha = useGoogleReCaptcha({
56
+ currentForm: 'CUSTOMER_LOGIN',
57
+ formAction: 'signIn'
58
+ });
50
59
  const {
51
60
  generateReCaptchaData,
52
61
  recaptchaLoading,
53
62
  recaptchaWidgetProps
54
- } = useGoogleReCaptcha({
55
- currentForm: 'CUSTOMER_LOGIN',
56
- formAction: 'signIn'
63
+ } = googleReCaptcha;
64
+
65
+ const { data: storeConfigData } = useQuery(getStoreConfigQuery, {
66
+ fetchPolicy: 'cache-and-network',
67
+ nextFetchPolicy: 'cache-first'
57
68
  });
58
69
 
70
+ const { customerAccessTokenLifetime } = useMemo(() => {
71
+ const storeConfig = storeConfigData?.storeConfig || {};
72
+
73
+ return {
74
+ customerAccessTokenLifetime:
75
+ storeConfig.customer_access_token_lifetime
76
+ };
77
+ }, [storeConfigData]);
59
78
  const [fetchCartId] = useMutation(createCartMutation);
60
79
  const [mergeCarts] = useMutation(mergeCartsMutation);
61
80
  const fetchUserDetails = useAwaitQuery(getCustomerQuery);
@@ -86,7 +105,9 @@ export const useSignIn = props => {
86
105
  });
87
106
 
88
107
  const token = signInResponse.data.generateCustomerToken.token;
89
- await setToken(token);
108
+ await (customerAccessTokenLifetime
109
+ ? setToken(token, customerAccessTokenLifetime)
110
+ : setToken(token));
90
111
 
91
112
  // Clear all cart/customer data from cache and redux.
92
113
  await apolloClient.clearCacheData(apolloClient, 'cart');
@@ -132,6 +153,7 @@ export const useSignIn = props => {
132
153
  }
133
154
  },
134
155
  [
156
+ customerAccessTokenLifetime,
135
157
  cartId,
136
158
  generateReCaptchaData,
137
159
  signIn,
@@ -216,6 +238,17 @@ export const useSignIn = props => {
216
238
  handleSubmit,
217
239
  isBusy: isGettingDetails || isSigningIn || recaptchaLoading,
218
240
  setFormApi,
219
- recaptchaWidgetProps
241
+ recaptchaWidgetProps,
242
+ userContext,
243
+ cartContext,
244
+ eventingContext,
245
+ signInMutationResult,
246
+ googleReCaptcha,
247
+ isSigningIn,
248
+ setIsSigningIn,
249
+ fetchCartId,
250
+ mergeCarts,
251
+ fetchUserDetails,
252
+ fetchCartDetails
220
253
  };
221
254
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@magento/peregrine",
3
- "version": "14.3.1",
3
+ "version": "14.4.1-alpha.3",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },