@graphcommerce/magento-cart-shipping-address 3.0.22 → 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,15 @@
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
+
3
13
  ## 3.0.22
4
14
 
5
15
  ### 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,27 +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'
9
- import {
10
- ActionCardItem,
11
- ActionCardListForm,
12
- } from '@graphcommerce/next-ui/ActionCard/ActionCardListForm'
8
+ import { Form } from '@graphcommerce/next-ui'
9
+ import { ActionCardListForm } from '@graphcommerce/next-ui/ActionCard/ActionCardListForm'
13
10
  import {
14
11
  useFormPersist,
15
12
  useFormCompose,
16
13
  UseFormComposeOptions,
17
- Controller,
14
+ useFormAutoSubmit,
18
15
  } from '@graphcommerce/react-hook-form'
19
- import { Trans } from '@lingui/react'
20
- import { Box, FormControl, NoSsr } from '@mui/material'
21
- import { useRouter } from 'next/router'
22
- import React, { useMemo } from 'react'
16
+ import React, { useEffect } from 'react'
23
17
  import { isSameAddress } from '../../utils/isSameAddress'
24
18
  import { GetAddressesDocument } from '../ShippingAddressForm/GetAddresses.gql'
19
+ import { CustomerAddressActionCard } from './CustomerAddressActionCard'
25
20
  import { SetCustomerShippingAddressOnCartDocument } from './SetCustomerShippingAddressOnCart.gql'
26
21
  import { SetCustomerShippingBillingAddressOnCartDocument } from './SetCustomerShippingBillingAddressOnCart.gql'
27
22
 
@@ -31,8 +26,6 @@ export function CustomerAddressForm(props: CustomerAddressListProps) {
31
26
  const customerAddresses = useQuery(CustomerDocument)
32
27
  const addresses = customerAddresses.data?.customer?.addresses
33
28
  const { step, children } = props
34
- const { push } = useRouter()
35
-
36
29
  const { data: cartQuery } = useCartQuery(GetAddressesDocument)
37
30
 
38
31
  const shippingAddress = cartQuery?.cart?.shipping_addresses?.[0]
@@ -61,61 +54,56 @@ export function CustomerAddressForm(props: CustomerAddressListProps) {
61
54
  // Mutation = SetCustomerBillingAddressOnCartDocument
62
55
  // }
63
56
 
64
- const form = useFormGqlMutationCart(Mutation, { mode: 'onChange' })
65
- const { handleSubmit, error, control, watch } = form
57
+ const form = useFormGqlMutationCart(Mutation, {
58
+ defaultValues: {
59
+ customerAddressId: found?.id ?? undefined,
60
+ },
61
+ onBeforeSubmit: (vars) => {
62
+ if (vars.customerAddressId === -1) return false
63
+ return vars
64
+ },
65
+ })
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
93
  <Form onSubmit={submit} noValidate>
80
- <FormControl>
81
- <ActionCardListForm
82
- control={control}
83
- name='customerAddressId'
84
- items={[
85
- ...(addresses ?? []).map((address) => ({
86
- value: Number(address?.id),
87
- title: `${address?.firstname} ${address?.lastname}`,
88
- image: <IconSvg src={iconHome} size='large' />,
89
- details: (
90
- <Box>
91
- {address?.street?.join(' ')}, {address?.postcode}, {address?.city},{' '}
92
- {address?.country_code}
93
- </Box>
94
- ),
95
- secondaryAction: (
96
- <Button
97
- disableRipple
98
- color='secondary'
99
- variant='text'
100
- onClick={(e) => {
101
- e.stopPropagation()
102
- // eslint-disable-next-line @typescript-eslint/no-floating-promises
103
- push(`/checkout/customer/addresses/edit?addressId=${address?.id}`)
104
- }}
105
- >
106
- <Trans id='Edit address' />
107
- </Button>
108
- ),
109
- })),
110
- {
111
- value: '-1',
112
- title: <Trans id='New address' />,
113
- details: <Trans id='Add new address' />,
114
- image: <IconSvg src={iconHome} size='large' />,
115
- },
116
- ]}
117
- />
118
- </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
+ />
119
107
  <ApolloCartErrorAlert error={error} />
120
108
  </Form>
121
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.22",
5
+ "version": "3.0.23",
6
6
  "sideEffects": false,
7
7
  "prettier": "@graphcommerce/prettier-config-pwa",
8
8
  "eslintConfig": {
@@ -20,10 +20,10 @@
20
20
  "dependencies": {
21
21
  "@graphcommerce/graphql": "3.1.3",
22
22
  "@graphcommerce/image": "3.1.6",
23
- "@graphcommerce/magento-cart": "4.3.2",
24
- "@graphcommerce/magento-customer": "4.4.0",
25
- "@graphcommerce/magento-store": "4.2.6",
26
- "@graphcommerce/next-ui": "4.8.2",
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": {