@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.
- package/Api/CartItemCountChanged.graphql +1 -1
- package/CHANGELOG.md +247 -54
- package/Config.graphqls +17 -0
- package/components/ApolloCartError/ApolloCartErrorAlert.tsx +1 -46
- package/components/CartAddress/CartAddress.graphql +1 -0
- package/components/CartAgreementsForm/CartAgreementsForm.tsx +60 -33
- package/components/CartFab/CartFab.tsx +8 -2
- package/components/CartStartCheckout/CartStartCheckout.graphql +1 -1
- package/components/CartStartCheckout/CartStartCheckout.tsx +23 -7
- package/components/CartStartCheckout/CartStartCheckoutLinkOrButton.tsx +4 -2
- package/components/CartTotals/CartTotals.graphql +8 -4
- package/components/CartTotals/CartTotals.tsx +9 -6
- package/components/EmptyCart/EmptyCart.tsx +8 -4
- package/components/InlineAccount/InlineAccount.tsx +6 -6
- package/components/OrderSucces/OrderSuccesPage.graphql +1 -1
- package/hooks/index.ts +5 -3
- package/hooks/useCartPermissions.ts +21 -0
- package/hooks/useCartQuery.ts +24 -3
- package/hooks/useCheckoutPermissions.ts +21 -0
- package/hooks/useFormGqlMutationCart.ts +40 -3
- package/index.ts +4 -3
- package/link/cartLink.ts +127 -0
- package/link/isProtectedCartOperation.ts +5 -0
- package/package.json +16 -15
- package/plugins/MagentoCartGraphqlProvider.tsx +15 -3
- package/plugins/useSignInFormMergeCart.ts +8 -7
- package/typePolicies.ts +1 -0
- package/utils/cartPermissions.ts +23 -0
- package/utils/checkoutPermissions.ts +13 -0
- package/utils/index.ts +2 -0
- package/hooks/CurrentCartId.graphqls +0 -12
- 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": "
|
|
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": "^
|
|
16
|
-
"@graphcommerce/eslint-config-pwa": "^
|
|
17
|
-
"@graphcommerce/framer-next-pages": "^
|
|
18
|
-
"@graphcommerce/framer-scroller": "^
|
|
19
|
-
"@graphcommerce/framer-utils": "^
|
|
20
|
-
"@graphcommerce/graphql": "^
|
|
21
|
-
"@graphcommerce/image": "^
|
|
22
|
-
"@graphcommerce/magento-customer": "^
|
|
23
|
-
"@graphcommerce/magento-graphql": "^
|
|
24
|
-
"@graphcommerce/magento-store": "^
|
|
25
|
-
"@graphcommerce/next-ui": "^
|
|
26
|
-
"@graphcommerce/prettier-config-pwa": "^
|
|
27
|
-
"@graphcommerce/react-hook-form": "^
|
|
28
|
-
"@graphcommerce/typescript-config-pwa": "^
|
|
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 {
|
|
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
|
-
|
|
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 {
|
|
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
|
|
9
|
-
|
|
8
|
+
export const config: PluginConfig = {
|
|
9
|
+
type: 'function',
|
|
10
|
+
module: '@graphcommerce/magento-customer',
|
|
11
|
+
}
|
|
10
12
|
|
|
11
|
-
const
|
|
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
|
@@ -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
|