@graphcommerce/magento-cart-shipping-address 3.2.5 → 3.3.0

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,42 @@
1
1
  # Change Log
2
2
 
3
+ ## 3.3.0
4
+
5
+ ### Minor Changes
6
+
7
+ - [#1553](https://github.com/graphcommerce-org/graphcommerce/pull/1553) [`db5d485a2`](https://github.com/graphcommerce-org/graphcommerce/commit/db5d485a23b6b90cfabc9a1320d3c05acbaeb8e2) Thanks [@NickdeK](https://github.com/NickdeK)! - Pickup in store functionality added to checkout
8
+
9
+ ### Patch Changes
10
+
11
+ - Updated dependencies [[`1afc6a547`](https://github.com/graphcommerce-org/graphcommerce/commit/1afc6a5473d6e31f47b5d0188801803b31865290), [`03d01c06c`](https://github.com/graphcommerce-org/graphcommerce/commit/03d01c06c6dc13df8d38ab5b40bd100c567a9e8d), [`4a4579bb2`](https://github.com/graphcommerce-org/graphcommerce/commit/4a4579bb2f7da378f3fcc504405caf2560dc10f6), [`afcd8e4bf`](https://github.com/graphcommerce-org/graphcommerce/commit/afcd8e4bfb7010da4d5faeed85b61991ed7975f4), [`02e1988e5`](https://github.com/graphcommerce-org/graphcommerce/commit/02e1988e5f361c6f66ae30d3bbee38ef2ac062df), [`323fdee4b`](https://github.com/graphcommerce-org/graphcommerce/commit/323fdee4b15ae23e0e84dd0588cb2c6446dcfd50), [`d03f0860b`](https://github.com/graphcommerce-org/graphcommerce/commit/d03f0860b882db4f280d9467aef9d66e56c1c030), [`b68d0b44a`](https://github.com/graphcommerce-org/graphcommerce/commit/b68d0b44a87688c80fb0aa4a5c840f262ce48d2f)]:
12
+ - @graphcommerce/graphql@3.4.2
13
+ - @graphcommerce/magento-cart@4.6.0
14
+ - @graphcommerce/magento-customer@4.8.0
15
+ - @graphcommerce/react-hook-form@3.3.1
16
+ - @graphcommerce/next-ui@4.14.0
17
+ - @graphcommerce/magento-store@4.2.18
18
+
19
+ ## 3.2.7
20
+
21
+ ### Patch Changes
22
+
23
+ - Updated dependencies [[`18054c441`](https://github.com/graphcommerce-org/graphcommerce/commit/18054c441962ba750bed3acc39ab46c8d3a341ce), [`c5c539c44`](https://github.com/graphcommerce-org/graphcommerce/commit/c5c539c44eeac524cd62ce649e132d2e00333794), [`6f69bc54c`](https://github.com/graphcommerce-org/graphcommerce/commit/6f69bc54c6e0224452817c532ae58d9c332b61ea), [`21886d6fa`](https://github.com/graphcommerce-org/graphcommerce/commit/21886d6fa64a48d9e932bfaf8d138c9b13c36e43)]:
24
+ - @graphcommerce/graphql@3.4.1
25
+ - @graphcommerce/magento-customer@4.7.2
26
+ - @graphcommerce/next-ui@4.13.1
27
+ - @graphcommerce/magento-cart@4.5.2
28
+ - @graphcommerce/magento-store@4.2.17
29
+
30
+ ## 3.2.6
31
+
32
+ ### Patch Changes
33
+
34
+ - Updated dependencies [[`8d8fda262`](https://github.com/graphcommerce-org/graphcommerce/commit/8d8fda2623e561cb43441110c67ffa34b692668a), [`d41cff721`](https://github.com/graphcommerce-org/graphcommerce/commit/d41cff7211230561ceeb7786cf75790efd6377cd), [`cefa7b365`](https://github.com/graphcommerce-org/graphcommerce/commit/cefa7b3652b55108d2178927e3c5d98a111cf373)]:
35
+ - @graphcommerce/next-ui@4.13.0
36
+ - @graphcommerce/magento-store@4.2.16
37
+ - @graphcommerce/magento-cart@4.5.1
38
+ - @graphcommerce/magento-customer@4.7.1
39
+
3
40
  ## 3.2.5
4
41
 
5
42
  ### Patch Changes
@@ -1,7 +1,6 @@
1
1
  import { CustomerAddressFragment } from '@graphcommerce/magento-customer/components/CreateCustomerAddressForm/CustomerAddress.gql'
2
2
  import { useFindCountry } from '@graphcommerce/magento-store'
3
- import { ActionCard, IconSvg, iconHome } from '@graphcommerce/next-ui'
4
- import { ActionCardItemRenderProps } from '@graphcommerce/next-ui/ActionCard/ActionCardListForm'
3
+ import { ActionCard, IconSvg, iconHome, ActionCardItemRenderProps } from '@graphcommerce/next-ui'
5
4
  import { Trans } from '@lingui/react'
6
5
  import { Button } from '@mui/material'
7
6
  import { useRouter } from 'next/router'
@@ -5,14 +5,14 @@ import {
5
5
  useFormGqlMutationCart,
6
6
  } from '@graphcommerce/magento-cart'
7
7
  import { CustomerDocument } from '@graphcommerce/magento-customer'
8
- import { Form } from '@graphcommerce/next-ui'
9
- import { ActionCardListForm } from '@graphcommerce/next-ui/ActionCard/ActionCardListForm'
8
+ import { Form, ActionCardListForm } from '@graphcommerce/next-ui'
10
9
  import {
11
10
  useFormPersist,
12
11
  useFormCompose,
13
12
  UseFormComposeOptions,
14
13
  useFormAutoSubmit,
15
14
  } from '@graphcommerce/react-hook-form'
15
+ import { Box, SxProps, Theme } from '@mui/material'
16
16
  import React, { useEffect } from 'react'
17
17
  import { isSameAddress } from '../../utils/isSameAddress'
18
18
  import { GetAddressesDocument } from '../ShippingAddressForm/GetAddresses.gql'
@@ -20,12 +20,17 @@ import { CustomerAddressActionCard } from './CustomerAddressActionCard'
20
20
  import { SetCustomerShippingAddressOnCartDocument } from './SetCustomerShippingAddressOnCart.gql'
21
21
  import { SetCustomerShippingBillingAddressOnCartDocument } from './SetCustomerShippingBillingAddressOnCart.gql'
22
22
 
23
- type CustomerAddressListProps = Pick<UseFormComposeOptions, 'step'> & { children?: React.ReactNode }
23
+ type CustomerAddressListProps = Pick<UseFormComposeOptions, 'step'> & {
24
+ children?: React.ReactNode
25
+ sx?: SxProps<Theme>
26
+ }
24
27
 
25
28
  export function CustomerAddressForm(props: CustomerAddressListProps) {
29
+ const { step, children, sx } = props
30
+
26
31
  const customerAddresses = useQuery(CustomerDocument)
27
32
  const addresses = customerAddresses.data?.customer?.addresses
28
- const { step, children } = props
33
+
29
34
  const { data: cartQuery } = useCartQuery(GetAddressesDocument)
30
35
 
31
36
  const shippingAddress = cartQuery?.cart?.shipping_addresses?.[0]
@@ -90,7 +95,7 @@ export function CustomerAddressForm(props: CustomerAddressListProps) {
90
95
 
91
96
  return (
92
97
  <>
93
- <Form onSubmit={submit} noValidate>
98
+ <Box component='form' onSubmit={submit} noValidate sx={sx}>
94
99
  <ActionCardListForm
95
100
  control={control}
96
101
  name='customerAddressId'
@@ -105,7 +110,7 @@ export function CustomerAddressForm(props: CustomerAddressListProps) {
105
110
  render={CustomerAddressActionCard}
106
111
  />
107
112
  <ApolloCartErrorAlert error={error} />
108
- </Form>
113
+ </Box>
109
114
  {customerAddressId === -1 && children}
110
115
  </>
111
116
  )
@@ -0,0 +1,44 @@
1
+ mutation SetBillingAddress(
2
+ $cartId: String!
3
+ $company: String
4
+ $firstname: String!
5
+ $lastname: String!
6
+ $postcode: String!
7
+ $city: String!
8
+ $countryCode: String!
9
+ $regionId: Int
10
+ $region: String
11
+ $street: String!
12
+ $houseNumber: String!
13
+ $addition: String
14
+ $telephone: String = "000 - 000 0000"
15
+ $saveInAddressBook: Boolean!
16
+ ) {
17
+ setBillingAddressOnCart(
18
+ input: {
19
+ cart_id: $cartId
20
+ billing_address: {
21
+ address: {
22
+ company: $company
23
+ firstname: $firstname
24
+ lastname: $lastname
25
+ postcode: $postcode
26
+ city: $city
27
+ country_code: $countryCode
28
+ region: $region
29
+ region_id: $regionId
30
+ street: [$street, $houseNumber, $addition]
31
+ telephone: $telephone
32
+ save_in_address_book: $saveInAddressBook
33
+ }
34
+ same_as_shipping: false
35
+ }
36
+ }
37
+ ) {
38
+ cart {
39
+ id
40
+ __typename
41
+ ...BillingAddress
42
+ }
43
+ }
44
+ }
@@ -4,6 +4,7 @@ import {
4
4
  useCartQuery,
5
5
  useFormGqlMutationCart,
6
6
  } from '@graphcommerce/magento-cart'
7
+ import { CartAddressFragment } from '@graphcommerce/magento-cart/components/CartAddress/CartAddress.gql'
7
8
  import { AddressFields, CustomerDocument, NameFields } from '@graphcommerce/magento-customer'
8
9
  import { CountryRegionsDocument, StoreConfigDocument } from '@graphcommerce/magento-store'
9
10
  import { Form, FormRow, InputCheckmark } from '@graphcommerce/next-ui'
@@ -16,19 +17,22 @@ import {
16
17
  } from '@graphcommerce/react-hook-form'
17
18
  import { i18n } from '@lingui/core'
18
19
  import { Trans } from '@lingui/react'
19
- import { TextField } from '@mui/material'
20
+ import { SxProps, TextField, Theme } from '@mui/material'
20
21
  import { AnimatePresence } from 'framer-motion'
22
+ import React from 'react'
21
23
  import { isSameAddress } from '../../utils/isSameAddress'
22
24
  import { GetAddressesDocument } from './GetAddresses.gql'
25
+ import { SetBillingAddressDocument } from './SetBillingAddress.gql'
23
26
  import { SetShippingAddressDocument } from './SetShippingAddress.gql'
24
27
  import { SetShippingBillingAddressDocument } from './SetShippingBillingAddress.gql'
25
28
 
26
29
  export type ShippingAddressFormProps = Pick<UseFormComposeOptions, 'step'> & {
27
30
  ignoreCache?: boolean
31
+ sx?: SxProps<Theme>
28
32
  }
29
33
 
30
- export function ShippingAddressForm(props: ShippingAddressFormProps) {
31
- const { step, ignoreCache = false } = props
34
+ export const ShippingAddressForm = React.memo<ShippingAddressFormProps>((props) => {
35
+ const { step, sx, ignoreCache = false } = props
32
36
  const { data: cartQuery } = useCartQuery(GetAddressesDocument)
33
37
  const { data: config } = useQuery(StoreConfigDocument)
34
38
  const { data: countriesData } = useQuery(CountryRegionsDocument)
@@ -36,26 +40,44 @@ export function ShippingAddressForm(props: ShippingAddressFormProps) {
36
40
 
37
41
  const shopCountry = config?.storeConfig?.locale?.split('_')?.[1].toUpperCase()
38
42
 
39
- const currentAddress = cartQuery?.cart?.shipping_addresses?.[0]
40
- const currentCustomer = customerQuery?.customer
41
- const currentCountryCode = currentAddress?.country.code ?? shopCountry
42
-
43
43
  const shippingAddress = cartQuery?.cart?.shipping_addresses?.[0]
44
44
  const billingAddress = cartQuery?.cart?.billing_address
45
+ let currentAddress: CartAddressFragment | undefined | null = shippingAddress
46
+
47
+ let Mutation = SetShippingBillingAddressDocument
48
+
49
+ // Customer has a different shipping address than the billing address we only need to update the shipping address here.
50
+ if (!isSameAddress(shippingAddress, billingAddress)) Mutation = SetShippingAddressDocument
45
51
 
46
- const Mutation = isSameAddress(shippingAddress, billingAddress)
47
- ? SetShippingBillingAddressDocument
48
- : SetShippingAddressDocument
52
+ // TODO: This is a hard dependency on Magento's MSI and should be removed, how can we get to this value?
53
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
54
+ // @ts-ignore The pickup_location_code is optional
55
+ const isAddressAPickupLocation = Boolean(shippingAddress?.pickup_location_code)
56
+ const isShippingMethodPickup =
57
+ shippingAddress?.selected_shipping_method?.carrier_code === 'instore'
49
58
 
50
- // If address is an existing customer address then this form is rendered to add a new address so we don't want any default values.
59
+ // If the address is a pick up location, we never want to show it in this form.
60
+ // TODO: Do we want to restore the previous shipping address here, how would we go about that?
61
+ if (isAddressAPickupLocation) currentAddress = billingAddress
62
+
63
+ // The customer is choosing to pick up the order from a store, therefor we are never allowed to update the shipping address.
64
+ // TODO: instead of submitting the billing address here, we can also choose to make the form readOnly and show a message to the customer.
65
+ if (isAddressAPickupLocation && isShippingMethodPickup) Mutation = SetBillingAddressDocument
66
+
67
+ // TODO: For a virtual cart this form should is only interested in setting the billing address.
68
+ const isVirtual = cartQuery?.cart?.is_virtual ?? false
69
+ if (isVirtual) {
70
+ currentAddress = billingAddress
71
+ Mutation = SetBillingAddressDocument
72
+ }
51
73
 
52
74
  const form = useFormGqlMutationCart(Mutation, {
53
75
  defaultValues: ignoreCache
54
76
  ? { saveInAddressBook: true }
55
77
  : {
56
78
  // todo(paales): change to something more sustainable
57
- firstname: currentAddress?.firstname ?? currentCustomer?.firstname ?? '',
58
- lastname: currentAddress?.lastname ?? currentCustomer?.lastname ?? '',
79
+ firstname: currentAddress?.firstname ?? customerQuery?.customer?.firstname ?? '',
80
+ lastname: currentAddress?.lastname ?? customerQuery?.customer?.lastname ?? '',
59
81
  telephone:
60
82
  currentAddress?.telephone !== '000 - 000 0000' ? currentAddress?.telephone : '',
61
83
  city: currentAddress?.city ?? '',
@@ -65,7 +87,7 @@ export function ShippingAddressForm(props: ShippingAddressFormProps) {
65
87
  houseNumber: currentAddress?.street?.[1] ?? '',
66
88
  addition: currentAddress?.street?.[2] ?? '',
67
89
  regionId: currentAddress?.region?.region_id ?? null,
68
- countryCode: currentCountryCode, // todo: replace by the default shipping country of the store + geoip,
90
+ countryCode: currentAddress?.country.code ?? shopCountry, // todo: replace by the default shipping country of the store + geoip,
69
91
  saveInAddressBook: true,
70
92
  },
71
93
  mode: 'onChange',
@@ -96,7 +118,7 @@ export function ShippingAddressForm(props: ShippingAddressFormProps) {
96
118
  const readOnly = formState.isSubmitting && !autoSubmitting
97
119
 
98
120
  return (
99
- <Form onSubmit={submit} noValidate>
121
+ <Form onSubmit={submit} noValidate sx={sx}>
100
122
  <AnimatePresence initial={false}>
101
123
  <NameFields form={form} key='name' readOnly={readOnly} />
102
124
  <AddressFields form={form} key='addressfields' readOnly={readOnly} />
@@ -122,4 +144,4 @@ export function ShippingAddressForm(props: ShippingAddressFormProps) {
122
144
  </AnimatePresence>
123
145
  </Form>
124
146
  )
125
- }
147
+ })
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.2.5",
5
+ "version": "3.3.0",
6
6
  "sideEffects": false,
7
7
  "prettier": "@graphcommerce/prettier-config-pwa",
8
8
  "eslintConfig": {
@@ -12,19 +12,19 @@
12
12
  }
13
13
  },
14
14
  "devDependencies": {
15
- "@graphcommerce/eslint-config-pwa": "^4.1.8",
15
+ "@graphcommerce/eslint-config-pwa": "^4.1.9",
16
16
  "@graphcommerce/prettier-config-pwa": "^4.0.6",
17
- "@graphcommerce/typescript-config-pwa": "^4.0.3",
17
+ "@graphcommerce/typescript-config-pwa": "^4.0.4",
18
18
  "@playwright/test": "^1.21.1"
19
19
  },
20
20
  "dependencies": {
21
- "@graphcommerce/graphql": "3.4.0",
21
+ "@graphcommerce/graphql": "3.4.2",
22
22
  "@graphcommerce/image": "3.1.7",
23
- "@graphcommerce/magento-cart": "4.5.0",
24
- "@graphcommerce/magento-customer": "4.7.0",
25
- "@graphcommerce/magento-store": "4.2.15",
26
- "@graphcommerce/next-ui": "4.12.0",
27
- "@graphcommerce/react-hook-form": "3.3.0"
23
+ "@graphcommerce/magento-cart": "4.6.0",
24
+ "@graphcommerce/magento-customer": "4.8.0",
25
+ "@graphcommerce/magento-store": "4.2.18",
26
+ "@graphcommerce/next-ui": "4.14.0",
27
+ "@graphcommerce/react-hook-form": "3.3.1"
28
28
  },
29
29
  "peerDependencies": {
30
30
  "@lingui/react": "^3.13.2",