@graphcommerce/magento-cart 8.1.0-canary.9 → 9.0.0-canary.101

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.
Files changed (32) hide show
  1. package/Api/CartItemCountChanged.graphql +1 -1
  2. package/CHANGELOG.md +247 -54
  3. package/Config.graphqls +17 -0
  4. package/components/ApolloCartError/ApolloCartErrorAlert.tsx +1 -46
  5. package/components/CartAddress/CartAddress.graphql +1 -0
  6. package/components/CartAgreementsForm/CartAgreementsForm.tsx +60 -33
  7. package/components/CartFab/CartFab.tsx +8 -2
  8. package/components/CartStartCheckout/CartStartCheckout.graphql +1 -1
  9. package/components/CartStartCheckout/CartStartCheckout.tsx +23 -7
  10. package/components/CartStartCheckout/CartStartCheckoutLinkOrButton.tsx +4 -2
  11. package/components/CartTotals/CartTotals.graphql +8 -4
  12. package/components/CartTotals/CartTotals.tsx +9 -6
  13. package/components/EmptyCart/EmptyCart.tsx +8 -4
  14. package/components/InlineAccount/InlineAccount.tsx +6 -6
  15. package/components/OrderSucces/OrderSuccesPage.graphql +1 -1
  16. package/hooks/index.ts +5 -3
  17. package/hooks/useCartPermissions.ts +21 -0
  18. package/hooks/useCartQuery.ts +24 -3
  19. package/hooks/useCheckoutPermissions.ts +21 -0
  20. package/hooks/useFormGqlMutationCart.ts +40 -3
  21. package/index.ts +4 -3
  22. package/link/cartLink.ts +127 -0
  23. package/link/isProtectedCartOperation.ts +5 -0
  24. package/package.json +16 -15
  25. package/plugins/MagentoCartGraphqlProvider.tsx +15 -3
  26. package/plugins/useSignInFormMergeCart.ts +8 -7
  27. package/typePolicies.ts +1 -0
  28. package/utils/cartPermissions.ts +23 -0
  29. package/utils/checkoutPermissions.ts +13 -0
  30. package/utils/index.ts +2 -0
  31. package/hooks/CurrentCartId.graphqls +0 -12
  32. package/link/createCartErrorLink.ts +0 -71
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@graphcommerce/magento-cart",
3
3
  "homepage": "https://www.graphcommerce.org/",
4
4
  "repository": "github:graphcommerce-org/graphcommerce",
5
- "version": "8.1.0-canary.9",
5
+ "version": "9.0.0-canary.101",
6
6
  "sideEffects": false,
7
7
  "prettier": "@graphcommerce/prettier-config-pwa",
8
8
  "eslintConfig": {
@@ -12,25 +12,26 @@
12
12
  }
13
13
  },
14
14
  "peerDependencies": {
15
- "@graphcommerce/ecommerce-ui": "^8.1.0-canary.9",
16
- "@graphcommerce/eslint-config-pwa": "^8.1.0-canary.9",
17
- "@graphcommerce/framer-next-pages": "^8.1.0-canary.9",
18
- "@graphcommerce/framer-scroller": "^8.1.0-canary.9",
19
- "@graphcommerce/framer-utils": "^8.1.0-canary.9",
20
- "@graphcommerce/graphql": "^8.1.0-canary.9",
21
- "@graphcommerce/image": "^8.1.0-canary.9",
22
- "@graphcommerce/magento-customer": "^8.1.0-canary.9",
23
- "@graphcommerce/magento-graphql": "^8.1.0-canary.9",
24
- "@graphcommerce/magento-store": "^8.1.0-canary.9",
25
- "@graphcommerce/next-ui": "^8.1.0-canary.9",
26
- "@graphcommerce/prettier-config-pwa": "^8.1.0-canary.9",
27
- "@graphcommerce/react-hook-form": "^8.1.0-canary.9",
28
- "@graphcommerce/typescript-config-pwa": "^8.1.0-canary.9",
15
+ "@graphcommerce/ecommerce-ui": "^9.0.0-canary.101",
16
+ "@graphcommerce/eslint-config-pwa": "^9.0.0-canary.101",
17
+ "@graphcommerce/framer-next-pages": "^9.0.0-canary.101",
18
+ "@graphcommerce/framer-scroller": "^9.0.0-canary.101",
19
+ "@graphcommerce/framer-utils": "^9.0.0-canary.101",
20
+ "@graphcommerce/graphql": "^9.0.0-canary.101",
21
+ "@graphcommerce/image": "^9.0.0-canary.101",
22
+ "@graphcommerce/magento-customer": "^9.0.0-canary.101",
23
+ "@graphcommerce/magento-graphql": "^9.0.0-canary.101",
24
+ "@graphcommerce/magento-store": "^9.0.0-canary.101",
25
+ "@graphcommerce/next-ui": "^9.0.0-canary.101",
26
+ "@graphcommerce/prettier-config-pwa": "^9.0.0-canary.101",
27
+ "@graphcommerce/react-hook-form": "^9.0.0-canary.101",
28
+ "@graphcommerce/typescript-config-pwa": "^9.0.0-canary.101",
29
29
  "@lingui/core": "^4.2.1",
30
30
  "@lingui/macro": "^4.2.1",
31
31
  "@lingui/react": "^4.2.1",
32
32
  "@mui/material": "^5.10.16",
33
33
  "framer-motion": "^10.0.0",
34
+ "graphql": "^16.0.0",
34
35
  "next": "*",
35
36
  "react": "^18.2.0",
36
37
  "react-dom": "^18.2.0"
@@ -1,6 +1,9 @@
1
1
  import { GraphQLProviderProps } from '@graphcommerce/graphql'
2
2
  import type { PluginConfig, PluginProps } from '@graphcommerce/next-config'
3
- import { cartErrorLink } from '../link/createCartErrorLink'
3
+ import { useEventCallback } from '@mui/material'
4
+ import { NextRouter } from 'next/router'
5
+ import { useMemo } from 'react'
6
+ import { cartLink } from '../link/cartLink'
4
7
  import { cartTypePolicies, migrateCart } from '../typePolicies'
5
8
 
6
9
  export const config: PluginConfig = {
@@ -9,11 +12,20 @@ export const config: PluginConfig = {
9
12
  }
10
13
 
11
14
  export function GraphQLProvider(props: PluginProps<GraphQLProviderProps>) {
12
- const { Prev, links = [], policies = [], migrations = [], ...rest } = props
15
+ const { Prev, router, links = [], policies = [], migrations = [], ...rest } = props
16
+
17
+ const push = useEventCallback<NextRouter['push']>((...args) => router.push(...args))
18
+
19
+ const cartLinkMemo = useMemo(
20
+ () => cartLink({ push, events: router.events, locale: router.locale }),
21
+ [push, router.events, router.locale],
22
+ )
23
+
13
24
  return (
14
25
  <Prev
15
26
  {...rest}
16
- links={[...links, cartErrorLink]}
27
+ router={router}
28
+ links={[...links, cartLinkMemo]}
17
29
  policies={[...policies, cartTypePolicies]}
18
30
  migrations={[...migrations, migrateCart]}
19
31
  />
@@ -1,14 +1,16 @@
1
1
  import { useApolloClient } from '@graphcommerce/graphql'
2
- import type { useSignInForm } from '@graphcommerce/magento-customer'
3
- import type { MethodPlugin } from '@graphcommerce/next-config'
2
+ import type { useSignInForm as useSignInFormType } from '@graphcommerce/magento-customer'
3
+ import type { FunctionPlugin, PluginConfig } from '@graphcommerce/next-config'
4
4
  import { cartLock, readCartId, useAssignCurrentCartId } from '../hooks'
5
5
  import { CustomerCartDocument } from '../hooks/CustomerCart.gql'
6
6
  import { MergeCartsDocument } from '../hooks/MergeCarts.gql'
7
7
 
8
- export const func = 'useSignInForm'
9
- export const exported = '@graphcommerce/magento-customer'
8
+ export const config: PluginConfig = {
9
+ type: 'function',
10
+ module: '@graphcommerce/magento-customer',
11
+ }
10
12
 
11
- const useSignInFormMergeCart: MethodPlugin<typeof useSignInForm> = (useSignInForm, options) => {
13
+ export const useSignInForm: FunctionPlugin<typeof useSignInFormType> = (useSignInForm, options) => {
12
14
  const client = useApolloClient()
13
15
  const assignCurrentCartId = useAssignCurrentCartId()
14
16
 
@@ -16,6 +18,7 @@ const useSignInFormMergeCart: MethodPlugin<typeof useSignInForm> = (useSignInFor
16
18
  ...options,
17
19
  onComplete: async (data, variables) => {
18
20
  await options.onComplete?.(data, variables)
21
+ if (data.errors) return
19
22
 
20
23
  cartLock(client.cache, true)
21
24
 
@@ -46,5 +49,3 @@ const useSignInFormMergeCart: MethodPlugin<typeof useSignInForm> = (useSignInFor
46
49
  },
47
50
  })
48
51
  }
49
-
50
- export const plugin = useSignInFormMergeCart
package/typePolicies.ts CHANGED
@@ -24,6 +24,7 @@ export const cartTypePolicies: StrictTypedTypePolicies = {
24
24
  return merged ? [merged] : []
25
25
  },
26
26
  },
27
+ items: { merge: (_, incoming) => incoming },
27
28
  prices: {
28
29
  merge: (existing: CartPrices[] | undefined, incoming: CartPrices[], options) =>
29
30
  options.mergeObjects(existing ?? {}, incoming),
@@ -0,0 +1,23 @@
1
+ import { storefrontConfig } from '@graphcommerce/next-ui'
2
+
3
+ function getCartPermissions(locale: string | undefined) {
4
+ return (
5
+ storefrontConfig(locale)?.permissions?.cart ??
6
+ import.meta.graphCommerce.permissions?.cart ??
7
+ 'ENABLED'
8
+ )
9
+ }
10
+
11
+ export function getCartDisabled(locale: string | undefined) {
12
+ return getCartPermissions(locale) === 'DISABLED'
13
+ }
14
+
15
+ export function getCartGuestEnabled(locale: string | undefined) {
16
+ return getCartPermissions(locale) === 'ENABLED'
17
+ }
18
+
19
+ export function getCartEnabledForUser(locale: string | undefined, loggedIn: () => boolean) {
20
+ if (getCartGuestEnabled(locale)) return true
21
+ if (getCartDisabled(locale)) return false
22
+ return !!loggedIn()
23
+ }
@@ -0,0 +1,13 @@
1
+ import { storefrontConfig } from '@graphcommerce/next-ui'
2
+
3
+ function getCheckoutPermission(locale: string | undefined) {
4
+ return (
5
+ storefrontConfig(locale)?.permissions?.checkout ??
6
+ import.meta.graphCommerce.permissions?.checkout ??
7
+ 'ENABLED'
8
+ )
9
+ }
10
+
11
+ export function getCheckoutIsDisabled(locale: string | undefined) {
12
+ return getCheckoutPermission(locale) === 'DISABLED'
13
+ }
package/utils/index.ts ADDED
@@ -0,0 +1,2 @@
1
+ export * from './cartPermissions'
2
+ export * from './checkoutPermissions'
@@ -1,12 +0,0 @@
1
- extend type Query {
2
- currentCartId: CurrentCartId
3
- }
4
-
5
- type CurrentCartId {
6
- id: String
7
- locked: Boolean
8
- }
9
-
10
- input RegisterCartIdInput {
11
- cart_id: String!
12
- }
@@ -1,71 +0,0 @@
1
- import { fromPromise, globalApolloClient, Operation } from '@graphcommerce/graphql'
2
- import { onError } from '@graphcommerce/graphql/apollo'
3
- import { ErrorCategory } from '@graphcommerce/magento-graphql'
4
- import type { GraphQLError } from 'graphql'
5
- import { writeCartId } from '../hooks'
6
- import { CreateEmptyCartDocument } from '../hooks/CreateEmptyCart.gql'
7
-
8
- type CartOperation = Operation & { variables: { cartId: string } }
9
- function isCartOperation(operation: Operation): operation is CartOperation {
10
- return typeof operation.variables.cartId === 'string'
11
- }
12
-
13
- function errorIsIncluded(errorPath: readonly (string | number)[] | undefined, keys: string[]) {
14
- const error = errorPath?.join()
15
- return keys.some((value) => value === error)
16
- }
17
-
18
- export const cartErrorLink = onError(({ graphQLErrors, operation, forward }) => {
19
- if (!globalApolloClient.current) return undefined
20
-
21
- const client = globalApolloClient.current
22
- const { cache } = client
23
-
24
- if (!isCartOperation(operation) || !graphQLErrors) return undefined
25
-
26
- const isErrorCategory = (err: GraphQLError, category: ErrorCategory) =>
27
- err.extensions?.category === category
28
-
29
- const isNoSuchEntityError = (err: GraphQLError) =>
30
- isErrorCategory(err, 'graphql-no-such-entity') &&
31
- errorIsIncluded(err.path, [
32
- 'cart',
33
- 'addProductsToCart',
34
- /**
35
- * These mutations can also throw the graphql-no-such-entity exception, however, we're not
36
- * sure if it also throws for other types of entities.
37
- */
38
- // 'removeItemFromCart',
39
- // 'setBillingAddressOnCart',
40
- // 'setGuestEmailOnCart',
41
- // 'setPaymentMethodOnCart',
42
- // 'setShippingAddressesOnCart',
43
- // 'setShippingMethodsOnCart',
44
- // 'updateCartItems',
45
- // 'applyCouponToCart',
46
- // 'removeCouponFromCart'
47
- ])
48
- const cartErr = graphQLErrors.find((err) => isNoSuchEntityError(err))
49
-
50
- if (!cartErr) return undefined
51
-
52
- if (globalThis.location?.search) {
53
- const urlParams = new URLSearchParams(window.location.search)
54
- if (urlParams.get('cart_id')) return forward(operation)
55
- }
56
-
57
- return fromPromise(client?.mutate({ mutation: CreateEmptyCartDocument }))
58
- .filter((value) => Boolean(value))
59
- .flatMap((cartData) => {
60
- const cartId = cartData.data?.createEmptyCart
61
- if (!cartId) return forward(operation)
62
-
63
- writeCartId(cache, cartId)
64
- operation.variables = { ...operation.variables, cartId }
65
-
66
- // retry the request, returning the new observable
67
- return forward(operation)
68
- })
69
- })
70
-
71
- export const createCartErrorLink = () => cartErrorLink