@graphcommerce/magento-cart 3.0.1

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 (55) hide show
  1. package/Api/CartItemCountChanged.gql.ts +4 -0
  2. package/Api/CartItemCountChanged.graphql +9 -0
  3. package/CHANGELOG.md +324 -0
  4. package/README.md +102 -0
  5. package/components/AddToCartButton/AddToCartButton.tsx +127 -0
  6. package/components/ApolloCartError/ApolloCartErrorAlert.tsx +36 -0
  7. package/components/ApolloCartError/ApolloCartErrorFullPage.tsx +1 -0
  8. package/components/CartFab/CartFab.gql.ts +12 -0
  9. package/components/CartFab/CartFab.graphql +6 -0
  10. package/components/CartFab/CartFab.tsx +67 -0
  11. package/components/CartFab/CartTotalQuantity.gql.ts +4 -0
  12. package/components/CartFab/CartTotalQuantity.graphql +3 -0
  13. package/components/CartItemSummary/GetCartItemSummary.gql.ts +12 -0
  14. package/components/CartItemSummary/GetCartItemSummary.graphql +6 -0
  15. package/components/CartItemSummary/index.tsx +133 -0
  16. package/components/CartStartCheckout/CartStartCheckout.gql.ts +4 -0
  17. package/components/CartStartCheckout/CartStartCheckout.graphql +7 -0
  18. package/components/CartStartCheckout/CartStartCheckout.tsx +63 -0
  19. package/components/CartSummary/CartSummary.gql.ts +4 -0
  20. package/components/CartSummary/CartSummary.graphql +15 -0
  21. package/components/CartSummary/GetCartSummary.gql.ts +12 -0
  22. package/components/CartSummary/GetCartSummary.graphql +5 -0
  23. package/components/CartSummary/index.tsx +139 -0
  24. package/components/CartTotals/CartTotals.gql.ts +4 -0
  25. package/components/CartTotals/CartTotals.graphql +51 -0
  26. package/components/CartTotals/CartTotals.tsx +187 -0
  27. package/components/CartTotals/GetCartTotals.gql.ts +12 -0
  28. package/components/CartTotals/GetCartTotals.graphql +5 -0
  29. package/components/EmptyCart/EmptyCart.tsx +56 -0
  30. package/components/index.ts +24 -0
  31. package/hooks/CreateEmptyCart.gql.ts +10 -0
  32. package/hooks/CreateEmptyCart.graphql +3 -0
  33. package/hooks/CurrentCartId.gql.ts +10 -0
  34. package/hooks/CurrentCartId.graphql +6 -0
  35. package/hooks/CurrentCartId.graphqls +11 -0
  36. package/hooks/CustomerCart.gql.ts +10 -0
  37. package/hooks/CustomerCart.graphql +7 -0
  38. package/hooks/UseCartRedirect.gql.ts +12 -0
  39. package/hooks/UseCartRedirect.graphql +5 -0
  40. package/hooks/UseMergeCustomerCart.gql.ts +13 -0
  41. package/hooks/UseMergeCustomerCart.graphql +6 -0
  42. package/hooks/index.ts +8 -0
  43. package/hooks/useAssignCurrentCartId.ts +18 -0
  44. package/hooks/useCartIdCreate.ts +22 -0
  45. package/hooks/useCartQuery.ts +37 -0
  46. package/hooks/useClearCurrentCartId.ts +18 -0
  47. package/hooks/useCurrentCartId.ts +6 -0
  48. package/hooks/useDisplayInclTax.ts +7 -0
  49. package/hooks/useFormGqlMutationCart.ts +28 -0
  50. package/hooks/useMergeCustomerCart.ts +49 -0
  51. package/index.ts +4 -0
  52. package/next-env.d.ts +4 -0
  53. package/package.json +42 -0
  54. package/tsconfig.json +5 -0
  55. package/typePolicies.ts +56 -0
@@ -0,0 +1,49 @@
1
+ import { useMutation, useQuery } from '@apollo/client'
2
+ import { CustomerTokenDocument, useExtractCustomerErrors } from '@graphcommerce/magento-customer'
3
+ import { graphqlErrorByCategory } from '@graphcommerce/magento-graphql'
4
+ import { useEffect } from 'react'
5
+ import { CustomerCartDocument } from './CustomerCart.gql'
6
+ import { UseMergeCustomerCartDocument } from './UseMergeCustomerCart.gql'
7
+ import { useAssignCurrentCartId } from './useAssignCurrentCartId'
8
+ import { useCurrentCartId } from './useCurrentCartId'
9
+
10
+ /**
11
+ * - Automatically assign the customer cart as the current cart
12
+ * - Merge the guest cart into the customer cart
13
+ *
14
+ * @todo: Implement the assignCustomerToGuestCart when available: https://github.com/magento/magento2/pull/33106
15
+ */
16
+ export function useMergeCustomerCart() {
17
+ const sourceCartId = useCurrentCartId()
18
+ const assignCurrentCartId = useAssignCurrentCartId()
19
+ const [merge, { error }] = useMutation(UseMergeCustomerCartDocument, { errorPolicy: 'all' })
20
+
21
+ useExtractCustomerErrors({ error })
22
+ const customerToken = useQuery(CustomerTokenDocument)?.data?.customerToken
23
+ const destinationCartId = useQuery(CustomerCartDocument, {
24
+ skip: !(customerToken?.token && customerToken.valid),
25
+ })?.data?.customerCart.id
26
+
27
+ useEffect(() => {
28
+ // If we don't have a customer cart, we're done
29
+ if (!destinationCartId) return
30
+
31
+ // If the vistor cart is the same as the customer cart, we're done
32
+ if (sourceCartId === destinationCartId) return
33
+
34
+ // If there is no visitor cart, assign the customer cart as the current cart
35
+ if (!sourceCartId) {
36
+ assignCurrentCartId(destinationCartId)
37
+ return
38
+ }
39
+
40
+ // eslint-disable-next-line @typescript-eslint/no-floating-promises
41
+ ;(async () => {
42
+ const result = await merge({ variables: { sourceCartId, destinationCartId } })
43
+
44
+ if (!result.data?.mergeCarts.id) return
45
+
46
+ assignCurrentCartId(result.data?.mergeCarts.id)
47
+ })()
48
+ }, [assignCurrentCartId, sourceCartId, destinationCartId, merge])
49
+ }
package/index.ts ADDED
@@ -0,0 +1,4 @@
1
+ export * from './Api/CartItemCountChanged.gql'
2
+ export * from './hooks'
3
+ export * from './components'
4
+ export * from './typePolicies'
package/next-env.d.ts ADDED
@@ -0,0 +1,4 @@
1
+ /// <reference types="next" />
2
+ /// <reference types="next/types/global" />
3
+ /// <reference types="next/image-types/global" />
4
+ /// <reference types="@graphcommerce/next-ui/types" />
package/package.json ADDED
@@ -0,0 +1,42 @@
1
+ {
2
+ "name": "@graphcommerce/magento-cart",
3
+ "version": "3.0.1",
4
+ "sideEffects": false,
5
+ "prettier": "@graphcommerce/prettier-config-pwa",
6
+ "browserslist": [
7
+ "extends @graphcommerce/browserslist-config-pwa"
8
+ ],
9
+ "eslintConfig": {
10
+ "extends": "@graphcommerce/eslint-config-pwa",
11
+ "parserOptions": {
12
+ "project": "./tsconfig.json"
13
+ }
14
+ },
15
+ "devDependencies": {
16
+ "@graphcommerce/browserslist-config-pwa": "^3.0.1",
17
+ "@graphcommerce/eslint-config-pwa": "^3.0.1",
18
+ "@graphcommerce/prettier-config-pwa": "^3.0.1",
19
+ "@graphcommerce/typescript-config-pwa": "^3.0.1",
20
+ "@playwright/test": "^1.14.1"
21
+ },
22
+ "dependencies": {
23
+ "@apollo/client": "^3.3.21",
24
+ "@graphcommerce/framer-scroller": "^0.2.1",
25
+ "@graphcommerce/graphql": "^2.103.1",
26
+ "@graphcommerce/image": "^2.104.1",
27
+ "@graphcommerce/magento-cart-address": "^2.103.1",
28
+ "@graphcommerce/magento-customer": "^3.0.1",
29
+ "@graphcommerce/magento-graphql": "^2.103.1",
30
+ "@graphcommerce/magento-store": "^3.0.1",
31
+ "@graphcommerce/next-ui": "^3.0.1",
32
+ "@graphcommerce/react-hook-form": "^2.102.1",
33
+ "@graphql-typed-document-node/core": "^3.1.0",
34
+ "@material-ui/core": "^4.12.3",
35
+ "@material-ui/lab": "^4.0.0-alpha.60",
36
+ "clsx": "^1.1.1",
37
+ "framer-motion": "^4.1.17",
38
+ "next": "^11.1.2",
39
+ "react": "^17.0.2",
40
+ "react-dom": "^17.0.2"
41
+ }
42
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,5 @@
1
+ {
2
+ "exclude": ["**/node_modules", "**/.*/"],
3
+ "include": ["**/*.ts", "**/*.tsx"],
4
+ "extends": "@graphcommerce/typescript-config-pwa/nextjs.json"
5
+ }
@@ -0,0 +1,56 @@
1
+ import { ApolloCache, NormalizedCacheObject } from '@apollo/client'
2
+ import type { QueryCartArgs, ShippingCartAddress, TypedTypePolicies } from '@graphcommerce/graphql'
3
+ import { CartPrices } from '@graphcommerce/graphql/generated/types'
4
+ import { CartFabDocument } from './components/CartFab/CartFab.gql'
5
+ import { CurrentCartIdDocument } from './hooks/CurrentCartId.gql'
6
+
7
+ export const cartTypePolicies: TypedTypePolicies = {
8
+ CurrentCartId: { keyFields: [] },
9
+ CartPrices: {
10
+ merge: (exiting, incomming, { mergeObjects }) => mergeObjects(exiting, incomming),
11
+ },
12
+ Cart: {
13
+ fields: {
14
+ shipping_addresses: {
15
+ merge: (
16
+ existing: ShippingCartAddress[] | undefined,
17
+ incoming: ShippingCartAddress[],
18
+ options,
19
+ ) => [options.mergeObjects(existing?.[0] ?? {}, incoming[0])],
20
+ },
21
+ prices: {
22
+ merge: (existing: CartPrices[] | undefined, incoming: CartPrices[], options) =>
23
+ options.mergeObjects(existing ?? {}, incoming),
24
+ },
25
+ },
26
+ },
27
+
28
+ Query: {
29
+ fields: {
30
+ currentCartId: (_, { toReference }) => toReference({ __typename: 'CurrentCartId' }),
31
+ cart: (_, { args, toReference }) =>
32
+ toReference({ __typename: 'Cart', id: (args as QueryCartArgs)?.cart_id }),
33
+ },
34
+ },
35
+ }
36
+
37
+ export const migrateCart = (
38
+ oldCache: ApolloCache<NormalizedCacheObject>,
39
+ newCache: ApolloCache<NormalizedCacheObject>,
40
+ ) => {
41
+ const currentCartId = oldCache.readQuery({ query: CurrentCartIdDocument })
42
+ const cartId = currentCartId?.currentCartId?.id
43
+
44
+ if (cartId) {
45
+ newCache.writeQuery({ query: CurrentCartIdDocument, data: currentCartId, broadcast: true })
46
+
47
+ // We have special handling for the CartFab because it tries to load data only from the cache.
48
+ const cartFab = oldCache.readQuery({ query: CartFabDocument })
49
+ newCache.writeQuery({
50
+ query: CartFabDocument,
51
+ data: cartFab,
52
+ variables: { cartId },
53
+ broadcast: true,
54
+ })
55
+ }
56
+ }