@graphcommerce/magento-cart-shipping-address 3.0.20 → 3.0.23

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,36 @@
1
1
  # Change Log
2
2
 
3
+ ## 3.0.23
4
+
5
+ ### Patch Changes
6
+
7
+ - Updated dependencies [[`238aa4d34`](https://github.com/graphcommerce-org/graphcommerce/commit/238aa4d3478773b8cb0973f4112c9829e59e16d6), [`afc67103d`](https://github.com/graphcommerce-org/graphcommerce/commit/afc67103d0e00583e274465036fd287537f95e79)]:
8
+ - @graphcommerce/magento-customer@4.4.1
9
+ - @graphcommerce/next-ui@4.8.3
10
+ - @graphcommerce/magento-cart@4.3.3
11
+ - @graphcommerce/magento-store@4.2.7
12
+
13
+ ## 3.0.22
14
+
15
+ ### Patch Changes
16
+
17
+ - Updated dependencies [[`d6262de71`](https://github.com/graphcommerce-org/graphcommerce/commit/d6262de71d2254a2b0b492e1a60f9e141767470e), [`c8c246b8a`](https://github.com/graphcommerce-org/graphcommerce/commit/c8c246b8aaab0621b68a2fca2a1c529a56fad962), [`e3005fe63`](https://github.com/graphcommerce-org/graphcommerce/commit/e3005fe6306093d47b08c6756c21c8175649e30b)]:
18
+ - @graphcommerce/magento-customer@4.4.0
19
+ - @graphcommerce/magento-cart@4.3.2
20
+ - @graphcommerce/next-ui@4.8.2
21
+ - @graphcommerce/magento-store@4.2.6
22
+
23
+ ## 3.0.21
24
+
25
+ ### Patch Changes
26
+
27
+ - Updated dependencies [[`a9df81310`](https://github.com/graphcommerce-org/graphcommerce/commit/a9df81310c051876dd82fb2819105dece47cc213), [`f167f9963`](https://github.com/graphcommerce-org/graphcommerce/commit/f167f99630966a7de43717937d43669e66132494)]:
28
+ - @graphcommerce/next-ui@4.8.1
29
+ - @graphcommerce/magento-cart@4.3.1
30
+ - @graphcommerce/magento-customer@4.3.2
31
+ - @graphcommerce/magento-store@4.2.5
32
+ - @graphcommerce/image@3.1.6
33
+
3
34
  ## 3.0.20
4
35
 
5
36
  ### Patch Changes
@@ -0,0 +1,96 @@
1
+ import { useQuery } from '@graphcommerce/graphql'
2
+ import { CustomerAddressFragment } from '@graphcommerce/magento-customer/components/CreateCustomerAddressForm/CustomerAddress.gql'
3
+ import { CountryRegionsDocument, useFindCountry } from '@graphcommerce/magento-store'
4
+ import { ActionCard, IconSvg, iconHome } from '@graphcommerce/next-ui'
5
+ import { ActionCardItemRenderer } from '@graphcommerce/next-ui/ActionCard/ActionCardListForm'
6
+ import { Trans } from '@lingui/react'
7
+ import { Box, Button } from '@mui/material'
8
+ import { useRouter } from 'next/router'
9
+
10
+ type CustomerAddressActionCardProps = ActionCardItemRenderer<
11
+ CustomerAddressFragment | null | undefined
12
+ >
13
+
14
+ export function CustomerAddressActionCard(props: CustomerAddressActionCardProps) {
15
+ const {
16
+ onReset,
17
+ company,
18
+ firstname,
19
+ lastname,
20
+ street,
21
+ postcode,
22
+ city,
23
+ country_code,
24
+ region,
25
+ id,
26
+ ...cardProps
27
+ } = props
28
+ const { push } = useRouter()
29
+ const country = useFindCountry(country_code)
30
+
31
+ if (cardProps.value === -1) {
32
+ return (
33
+ <ActionCard
34
+ {...cardProps}
35
+ image={<IconSvg src={iconHome} size='large' />}
36
+ title={<Trans id='New address' />}
37
+ details={<Trans id='Add new address' />}
38
+ action={
39
+ <Button disableRipple variant='text' color='secondary'>
40
+ <Trans id='Select' />
41
+ </Button>
42
+ }
43
+ reset={
44
+ <Button disableRipple variant='text' color='secondary' onClick={onReset}>
45
+ <Trans id='Change' />
46
+ </Button>
47
+ }
48
+ />
49
+ )
50
+ }
51
+
52
+ return (
53
+ <ActionCard
54
+ {...cardProps}
55
+ image={<IconSvg src={iconHome} size='large' />}
56
+ title={
57
+ <>
58
+ {street?.join(' ')}, {city}
59
+ </>
60
+ }
61
+ details={
62
+ <>
63
+ {company} {firstname} {lastname} ({postcode}, {region?.region_code}{' '}
64
+ {country?.full_name_locale})
65
+ </>
66
+ }
67
+ action={
68
+ <Button disableRipple variant='text' color='secondary'>
69
+ <Trans id='Select' />
70
+ </Button>
71
+ }
72
+ reset={
73
+ <Button disableRipple variant='text' color='secondary' onClick={onReset}>
74
+ <Trans id='Change' />
75
+ </Button>
76
+ }
77
+ secondaryAction={
78
+ <Button
79
+ color='secondary'
80
+ variant='text'
81
+ onMouseDown={(e) => {
82
+ e.stopPropagation()
83
+ }}
84
+ onClick={(e) => {
85
+ e.preventDefault()
86
+ e.stopPropagation()
87
+ // eslint-disable-next-line @typescript-eslint/no-floating-promises
88
+ push(`/checkout/customer/addresses/edit?addressId=${id}`)
89
+ }}
90
+ >
91
+ <Trans id='Edit address' />
92
+ </Button>
93
+ }
94
+ />
95
+ )
96
+ }
@@ -1,24 +1,22 @@
1
- import { useQuery } from '@graphcommerce/graphql'
1
+ import { fallbackHttpConfig, useQuery } from '@graphcommerce/graphql'
2
2
  import {
3
3
  ApolloCartErrorAlert,
4
4
  useCartQuery,
5
5
  useFormGqlMutationCart,
6
6
  } from '@graphcommerce/magento-cart'
7
7
  import { CustomerDocument } from '@graphcommerce/magento-customer'
8
- import { iconHome, IconSvg, ActionCardList, ActionCard, Form, Button } from '@graphcommerce/next-ui'
8
+ import { Form } from '@graphcommerce/next-ui'
9
+ import { ActionCardListForm } from '@graphcommerce/next-ui/ActionCard/ActionCardListForm'
9
10
  import {
10
11
  useFormPersist,
11
12
  useFormCompose,
12
13
  UseFormComposeOptions,
13
- Controller,
14
+ useFormAutoSubmit,
14
15
  } from '@graphcommerce/react-hook-form'
15
- import { Trans } from '@lingui/react'
16
- import { Box, FormControl, NoSsr } from '@mui/material'
17
- import { useRouter } from 'next/router'
18
- import React from 'react'
16
+ import React, { useEffect } from 'react'
19
17
  import { isSameAddress } from '../../utils/isSameAddress'
20
18
  import { GetAddressesDocument } from '../ShippingAddressForm/GetAddresses.gql'
21
- import { SetCustomerBillingAddressOnCartDocument } from './SetCustomerBillingAddressOnCart.gql'
19
+ import { CustomerAddressActionCard } from './CustomerAddressActionCard'
22
20
  import { SetCustomerShippingAddressOnCartDocument } from './SetCustomerShippingAddressOnCart.gql'
23
21
  import { SetCustomerShippingBillingAddressOnCartDocument } from './SetCustomerShippingBillingAddressOnCart.gql'
24
22
 
@@ -28,8 +26,6 @@ export function CustomerAddressForm(props: CustomerAddressListProps) {
28
26
  const customerAddresses = useQuery(CustomerDocument)
29
27
  const addresses = customerAddresses.data?.customer?.addresses
30
28
  const { step, children } = props
31
- const router = useRouter()
32
-
33
29
  const { data: cartQuery } = useCartQuery(GetAddressesDocument)
34
30
 
35
31
  const shippingAddress = cartQuery?.cart?.shipping_addresses?.[0]
@@ -59,132 +55,55 @@ export function CustomerAddressForm(props: CustomerAddressListProps) {
59
55
  // }
60
56
 
61
57
  const form = useFormGqlMutationCart(Mutation, {
62
- // defaultValues: { customerAddressId: defaultCustomerAddressId },
63
- mode: 'onChange',
58
+ defaultValues: {
59
+ customerAddressId: found?.id ?? undefined,
60
+ },
61
+ onBeforeSubmit: (vars) => {
62
+ if (vars.customerAddressId === -1) return false
63
+ return vars
64
+ },
64
65
  })
65
- const { handleSubmit, error, control, watch } = form
66
+
67
+ const { handleSubmit, error, control, watch, setValue } = form
66
68
 
67
69
  // If customer selects 'new address' then we do not have to submit anything so we provide an empty submit function.
68
70
  const customerAddressId = watch('customerAddressId')
69
71
 
70
- const submit = customerAddressId === -1 ? async () => {} : handleSubmit(() => {})
72
+ const submit = handleSubmit(() => {})
71
73
 
74
+ // We want to persist the form because we can't send the 'new address' state to the server, but we do want to keep this selection.
72
75
  useFormPersist({ form, name: 'CustomerAddressForm' })
73
76
  useFormCompose({ form, step, submit, key: 'CustomerAddressForm' })
74
77
 
75
- if (customerAddresses.loading || !addresses || addresses.length === 0) return null
78
+ // We want to autosubmit the CustomerAddressFrom because the shipping methods depend on it.
79
+ useFormAutoSubmit({ form, submit })
80
+
81
+ // When there is no address set on the cart set before
82
+ const defaultAddr =
83
+ (!shippingAddress && addresses?.find((a) => a?.default_shipping)?.id) || undefined
84
+
85
+ useEffect(() => {
86
+ if (defaultAddr) setValue('customerAddressId', defaultAddr)
87
+ }, [defaultAddr, setValue])
88
+
89
+ if (customerAddresses.loading || !addresses || addresses.length === 0) return <>{children}</>
76
90
 
77
91
  return (
78
92
  <>
79
- {' '}
80
93
  <Form onSubmit={submit} noValidate>
81
- <FormControl>
82
- <Controller
83
- control={control}
84
- defaultValue={found && found.id ? found.id : 0}
85
- name='customerAddressId'
86
- rules={{ required: true }}
87
- render={({ field: { onChange, value }, fieldState, formState }) => (
88
- <NoSsr>
89
- <ActionCardList
90
- required
91
- value={String(value)}
92
- onChange={(event, incomming) => {
93
- onChange(Number(incomming))
94
- }}
95
- error={formState.isSubmitted && !!fieldState.error}
96
- >
97
- {addresses.map((address) => (
98
- <ActionCard
99
- value={String(address?.id)}
100
- key={address?.id}
101
- action={
102
- <Button disableRipple variant='text' color='secondary'>
103
- <Trans id='Select' />
104
- </Button>
105
- }
106
- image={<IconSvg src={iconHome} size='large' />}
107
- title={`${address?.firstname} ${address?.lastname}`}
108
- details={
109
- <Box>
110
- {address?.street?.join(' ')}, {address?.postcode}, {address?.city},{' '}
111
- {address?.country_code}
112
- </Box>
113
- }
114
- onClick={onChange}
115
- secondaryAction={
116
- <Button
117
- disableRipple
118
- color='secondary'
119
- variant='text'
120
- onClick={(e) => {
121
- e.stopPropagation()
122
-
123
- // eslint-disable-next-line @typescript-eslint/no-floating-promises
124
- router.push(
125
- `/checkout/customer/addresses/edit?addressId=${address?.id}`,
126
- )
127
- }}
128
- >
129
- <Trans id='Edit address' />
130
- </Button>
131
- }
132
- selected={value === address?.id}
133
- hidden={!!value && value !== address?.id}
134
- reset={
135
- <Button
136
- disableRipple
137
- variant='text'
138
- color='secondary'
139
- onClick={(e) => {
140
- e.preventDefault()
141
- onChange(null)
142
- }}
143
- >
144
- <Trans id='Change' />
145
- </Button>
146
- }
147
- />
148
- ))}
149
- <ActionCard
150
- value='-1'
151
- key='new-address'
152
- title={<Trans id='New address' />}
153
- selected={value === -1}
154
- hidden={!!value && value !== -1}
155
- details={<Trans id='Add new address' />}
156
- image={<IconSvg src={iconHome} size='large' />}
157
- action={
158
- <Button
159
- disableRipple
160
- variant='text'
161
- color='secondary'
162
- onClick={(e) => {
163
- e.preventDefault()
164
- }}
165
- >
166
- <Trans id='Select' />
167
- </Button>
168
- }
169
- reset={
170
- <Button
171
- disableRipple
172
- variant='text'
173
- color='secondary'
174
- onClick={(e) => {
175
- e.preventDefault()
176
- onChange(null)
177
- }}
178
- >
179
- <Trans id='Change' />
180
- </Button>
181
- }
182
- />
183
- </ActionCardList>
184
- </NoSsr>
185
- )}
186
- />
187
- </FormControl>
94
+ <ActionCardListForm
95
+ control={control}
96
+ name='customerAddressId'
97
+ errorMessage='Please select a shipping address'
98
+ items={[
99
+ ...(addresses ?? []).filter(Boolean).map((address) => ({
100
+ ...address,
101
+ value: Number(address?.id),
102
+ })),
103
+ { value: -1 },
104
+ ]}
105
+ render={CustomerAddressActionCard}
106
+ />
188
107
  <ApolloCartErrorAlert error={error} />
189
108
  </Form>
190
109
  {customerAddressId === -1 && children}
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@graphcommerce/magento-cart-shipping-address",
3
3
  "homepage": "https://www.graphcommerce.org/",
4
4
  "repository": "github:graphcommerce-org/graphcommerce",
5
- "version": "3.0.20",
5
+ "version": "3.0.23",
6
6
  "sideEffects": false,
7
7
  "prettier": "@graphcommerce/prettier-config-pwa",
8
8
  "eslintConfig": {
@@ -19,11 +19,11 @@
19
19
  },
20
20
  "dependencies": {
21
21
  "@graphcommerce/graphql": "3.1.3",
22
- "@graphcommerce/image": "3.1.5",
23
- "@graphcommerce/magento-cart": "4.3.0",
24
- "@graphcommerce/magento-customer": "4.3.1",
25
- "@graphcommerce/magento-store": "4.2.4",
26
- "@graphcommerce/next-ui": "4.8.0",
22
+ "@graphcommerce/image": "3.1.6",
23
+ "@graphcommerce/magento-cart": "4.3.3",
24
+ "@graphcommerce/magento-customer": "4.4.1",
25
+ "@graphcommerce/magento-store": "4.2.7",
26
+ "@graphcommerce/next-ui": "4.8.3",
27
27
  "@graphcommerce/react-hook-form": "3.1.3"
28
28
  },
29
29
  "peerDependencies": {