@graphcommerce/magento-cart-shipping-method 9.0.0-canary.66 → 9.0.0-canary.68

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/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  # Change Log
2
2
 
3
+ ## 9.0.0-canary.68
4
+
5
+ ### Patch Changes
6
+
7
+ - [#2340](https://github.com/graphcommerce-org/graphcommerce/pull/2340) [`18691c7`](https://github.com/graphcommerce-org/graphcommerce/commit/18691c71e6940e8baa8f93c3b798be4d6e5a8a9a) - Select the only available shipping method as the current cart shipping method when there is only one shipping method available. ([@LaurensFranken](https://github.com/LaurensFranken))
8
+
9
+ ## 9.0.0-canary.67
10
+
3
11
  ## 9.0.0-canary.66
4
12
 
5
13
  ## 9.0.0-canary.65
@@ -21,7 +21,7 @@ import {
21
21
  import { i18n } from '@lingui/core'
22
22
  import { Trans } from '@lingui/react'
23
23
  import { SxProps, Theme } from '@mui/material'
24
- import { useMemo } from 'react'
24
+ import { useEffect, useMemo } from 'react'
25
25
  import { GetShippingMethodsDocument } from './GetShippingMethods.gql'
26
26
  import { ShippingMethodActionCard } from './ShippingMethodActionCard'
27
27
  import {
@@ -45,10 +45,9 @@ function notEmpty<TValue>(value: TValue | null | undefined): value is TValue {
45
45
  export function ShippingMethodForm(props: ShippingMethodFormProps) {
46
46
  const { step, sx, children, onBeforeSubmit = (vars) => vars, ...options } = props
47
47
  const { data: cartQuery } = useCartQuery(GetShippingMethodsDocument)
48
- const availableMethods = (
49
- cartQuery?.cart?.shipping_addresses?.[0]?.available_shipping_methods ?? []
50
- ).filter(notEmpty)
51
- const selectedMethod = cartQuery?.cart?.shipping_addresses?.[0]?.selected_shipping_method
48
+
49
+ const shippingAddress = cartQuery?.cart?.shipping_addresses?.[0]
50
+ const availableMethods = (shippingAddress?.available_shipping_methods ?? []).filter(notEmpty)
52
51
 
53
52
  const items = useMemo(
54
53
  () =>
@@ -67,12 +66,11 @@ export function ShippingMethodForm(props: ShippingMethodFormProps) {
67
66
  [availableMethods],
68
67
  )
69
68
 
70
- // The default: When there is only a single shipping method, select that one.
71
- let carrierMethod: string | undefined = items.length === 1 ? items[0]?.value : undefined
72
-
73
69
  // Override with the currently selected method if there is one.
74
- if (selectedMethod?.method_code)
75
- carrierMethod = `${selectedMethod.carrier_code}-${selectedMethod.method_code}`
70
+ const selectedMethod = cartQuery?.cart?.shipping_addresses?.[0]?.selected_shipping_method
71
+ const carrierMethod = selectedMethod?.method_code
72
+ ? `${selectedMethod.carrier_code}-${selectedMethod.method_code}`
73
+ : undefined
76
74
 
77
75
  const form = useFormGqlMutationCart<
78
76
  ShippingMethodFormMutation,
@@ -87,13 +85,14 @@ export function ShippingMethodForm(props: ShippingMethodFormProps) {
87
85
  ...options,
88
86
  })
89
87
 
90
- const { handleSubmit, control, error } = form
88
+ const { handleSubmit, control, error, setValue } = form
91
89
  const submit = handleSubmit(() => {})
92
90
 
93
91
  useFormCompose({ form, step, submit, key: 'ShippingMethodForm' })
94
92
 
95
- if (items.length === 0) {
96
- items.push({
93
+ const renderItems = [...items]
94
+ if (renderItems.length === 0) {
95
+ renderItems.push({
97
96
  disabled: true,
98
97
  value: '',
99
98
  available: false,
@@ -107,6 +106,18 @@ export function ShippingMethodForm(props: ShippingMethodFormProps) {
107
106
  })
108
107
  }
109
108
 
109
+ const firstCarrierMethod = items.length === 1 ? items[0].value : undefined
110
+ useEffect(() => {
111
+ // If there is a shipping address AND there is only one shipping method
112
+ if (shippingAddress && firstCarrierMethod) {
113
+ // AND the current carrierMethod is not the same (or not set) as the single shipping method
114
+ if (carrierMethod !== firstCarrierMethod) {
115
+ // THEN set the shipping method to the only one available.
116
+ setValue('carrierMethod', firstCarrierMethod, { shouldValidate: true })
117
+ }
118
+ }
119
+ }, [shippingAddress, firstCarrierMethod, carrierMethod, setValue])
120
+
110
121
  return (
111
122
  <FormProvider {...form}>
112
123
  <FormAutoSubmit
@@ -127,7 +138,7 @@ export function ShippingMethodForm(props: ShippingMethodFormProps) {
127
138
  size='large'
128
139
  color='secondary'
129
140
  rules={{ required: i18n._(/* i18n */ 'Please select a shipping method') }}
130
- items={items}
141
+ items={renderItems}
131
142
  render={
132
143
  ShippingMethodActionCard as React.FC<ActionCardItemRenderProps<ActionCardItemBase>>
133
144
  }
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@graphcommerce/magento-cart-shipping-method",
3
3
  "homepage": "https://www.graphcommerce.org/",
4
4
  "repository": "github:graphcommerce-org/graphcommerce",
5
- "version": "9.0.0-canary.66",
5
+ "version": "9.0.0-canary.68",
6
6
  "sideEffects": false,
7
7
  "prettier": "@graphcommerce/prettier-config-pwa",
8
8
  "eslintConfig": {
@@ -12,17 +12,17 @@
12
12
  }
13
13
  },
14
14
  "peerDependencies": {
15
- "@graphcommerce/eslint-config-pwa": "^9.0.0-canary.66",
16
- "@graphcommerce/framer-scroller": "^9.0.0-canary.66",
17
- "@graphcommerce/graphql": "^9.0.0-canary.66",
18
- "@graphcommerce/image": "^9.0.0-canary.66",
19
- "@graphcommerce/magento-cart": "^9.0.0-canary.66",
20
- "@graphcommerce/magento-cart-shipping-address": "^9.0.0-canary.66",
21
- "@graphcommerce/magento-store": "^9.0.0-canary.66",
22
- "@graphcommerce/next-ui": "^9.0.0-canary.66",
23
- "@graphcommerce/prettier-config-pwa": "^9.0.0-canary.66",
24
- "@graphcommerce/react-hook-form": "^9.0.0-canary.66",
25
- "@graphcommerce/typescript-config-pwa": "^9.0.0-canary.66",
15
+ "@graphcommerce/eslint-config-pwa": "^9.0.0-canary.68",
16
+ "@graphcommerce/framer-scroller": "^9.0.0-canary.68",
17
+ "@graphcommerce/graphql": "^9.0.0-canary.68",
18
+ "@graphcommerce/image": "^9.0.0-canary.68",
19
+ "@graphcommerce/magento-cart": "^9.0.0-canary.68",
20
+ "@graphcommerce/magento-cart-shipping-address": "^9.0.0-canary.68",
21
+ "@graphcommerce/magento-store": "^9.0.0-canary.68",
22
+ "@graphcommerce/next-ui": "^9.0.0-canary.68",
23
+ "@graphcommerce/prettier-config-pwa": "^9.0.0-canary.68",
24
+ "@graphcommerce/react-hook-form": "^9.0.0-canary.68",
25
+ "@graphcommerce/typescript-config-pwa": "^9.0.0-canary.68",
26
26
  "@lingui/core": "^4.2.1",
27
27
  "@lingui/macro": "^4.2.1",
28
28
  "@lingui/react": "^4.2.1",