@graphcommerce/address-fields-nl 8.1.0-canary.3 → 8.1.0-canary.6

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,8 +1,80 @@
1
1
  # @graphcommerce/address-fields-nl
2
2
 
3
- ## 8.1.0-canary.3
3
+ ## 8.1.0-canary.6
4
4
 
5
- ## 8.1.0-canary.2
5
+ ## 8.1.0-canary.5
6
+
7
+ ## 8.0.6-canary.4
8
+
9
+ ## 8.0.6-canary.3
10
+
11
+ ## 8.0.6-canary.2
12
+
13
+ ## 8.0.6-canary.1
14
+
15
+ ## 8.0.6-canary.0
16
+
17
+ ## 8.0.5
18
+
19
+ ### Patch Changes
20
+
21
+ - [#2244](https://github.com/graphcommerce-org/graphcommerce/pull/2244) [`b988ecb`](https://github.com/graphcommerce-org/graphcommerce/commit/b988ecb3809c422e6871cd29905f6f495bda49f1) - Fix export name
22
+ ([@JoshuaS98](https://github.com/JoshuaS98))
23
+
24
+ ## 8.0.5-canary.10
25
+
26
+ ### Patch Changes
27
+
28
+ - [#2244](https://github.com/graphcommerce-org/graphcommerce/pull/2244) [`b988ecb`](https://github.com/graphcommerce-org/graphcommerce/commit/b988ecb3809c422e6871cd29905f6f495bda49f1) - Fix export name
29
+ ([@JoshuaS98](https://github.com/JoshuaS98))
30
+
31
+ ## 8.0.5-canary.9
32
+
33
+ ## 8.0.5-canary.8
34
+
35
+ ## 8.0.5-canary.7
36
+
37
+ ## 8.0.5-canary.6
38
+
39
+ ## 8.0.5-canary.5
40
+
41
+ ## 8.0.5-canary.4
42
+
43
+ ## 8.0.5-canary.3
44
+
45
+ ## 8.0.5-canary.2
46
+
47
+ ## 8.0.5-canary.1
48
+
49
+ ## 8.0.5-canary.0
50
+
51
+ ## 8.0.4
52
+
53
+ ## 8.0.4-canary.1
54
+
55
+ ## 8.0.4-canary.0
56
+
57
+ ## 8.0.3
58
+
59
+ ### Patch Changes
60
+
61
+ - [#2212](https://github.com/graphcommerce-org/graphcommerce/pull/2212) [`b9d92c8`](https://github.com/graphcommerce-org/graphcommerce/commit/b9d92c8bcdb9c3d0fdbd9c004b864b97bc52fcb1) - Refactor the AddressFields and use FormAutoSubmit and separate AddressField components
62
+ ([@paales](https://github.com/paales))
63
+
64
+ ## 8.0.3-canary.6
65
+
66
+ ## 8.0.3-canary.5
67
+
68
+ ### Patch Changes
69
+
70
+ - [#2212](https://github.com/graphcommerce-org/graphcommerce/pull/2212) [`b9d92c8`](https://github.com/graphcommerce-org/graphcommerce/commit/b9d92c8bcdb9c3d0fdbd9c004b864b97bc52fcb1) - Refactor the AddressFields and use FormAutoSubmit and separate AddressField components
71
+ ([@paales](https://github.com/paales))
72
+
73
+ ## 8.0.3-canary.4
74
+
75
+ ## 8.0.3-canary.3
76
+
77
+ ## 8.0.3-canary.2
6
78
 
7
79
  ## 8.0.3-canary.1
8
80
 
@@ -0,0 +1,53 @@
1
+ import { FieldPath, FieldValues, FormAutoSubmit, PathValue } from '@graphcommerce/ecommerce-ui'
2
+ import { useApolloClient } from '@graphcommerce/graphql'
3
+ import { AddressFieldsOptions, useAddressFieldsForm } from '@graphcommerce/magento-customer'
4
+ import { useEventCallback } from '@mui/material'
5
+ import { PostCodeNLDocument } from '../graphql/PostcodeNL.gql'
6
+
7
+ const postCodeRegex = /^[1-9][0-9]{3}[a-z]{2}$/i
8
+
9
+ export function PostcodeNLAutoFill<
10
+ TFieldValues extends FieldValues = FieldValues,
11
+ TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>,
12
+ >(options: AddressFieldsOptions<TFieldValues, TName>) {
13
+ const { control, name, getValues, setValue } = useAddressFieldsForm(options)
14
+
15
+ const client = useApolloClient()
16
+
17
+ const submit = useEventCallback(async () => {
18
+ const postcodeRaw = getValues(name.postcode) as string
19
+ const housenumber = getValues(name.houseNumber) as string
20
+ const addition = getValues(name.addition) as string
21
+ const country = getValues(name.countryCode) as string
22
+
23
+ if (!housenumber || !postcodeRaw || country !== 'NL') return
24
+
25
+ const postcode = postcodeRaw.trim().replace(/ /g, '')
26
+ if (!postCodeRegex.test(postcode)) return
27
+
28
+ const result = await client.query({
29
+ query: PostCodeNLDocument,
30
+ variables: { postcode, housenumber: `${housenumber}${addition ?? ''}` },
31
+ // fetchPolicy: process.env.NODE_ENV !== 'production' ? 'no-cache' : 'cache-first',
32
+ })
33
+
34
+ const { street, city } = result.data?.postcodeNL ?? {}
35
+ if (street && city) {
36
+ setValue(name.street, street as PathValue<TFieldValues, TName>)
37
+ setValue(name.city, city as PathValue<TFieldValues, TName>)
38
+ } else {
39
+ setValue(name.street, '' as PathValue<TFieldValues, TName>)
40
+ setValue(name.city, '' as PathValue<TFieldValues, TName>)
41
+ }
42
+ })
43
+
44
+ return (
45
+ <FormAutoSubmit
46
+ initialWait={0}
47
+ control={control}
48
+ submit={submit}
49
+ noValidate
50
+ name={[name.postcode, name.houseNumber, name.addition, name.countryCode]}
51
+ />
52
+ )
53
+ }
@@ -1,6 +1,6 @@
1
- query postcodeNL($postcode: String!, $housenumber: String!){
2
- postcodeNL(postcode: $postcode, housenumber: $housenumber){
1
+ query PostCodeNL($postcode: String!, $housenumber: String!) {
2
+ postcodeNL(postcode: $postcode, housenumber: $housenumber) {
3
3
  street
4
4
  city
5
5
  }
6
- }
6
+ }
package/index.ts CHANGED
@@ -1 +1 @@
1
- export * from './components/PostcodeNLAddressFields'
1
+ export * from './components/PostcodeNLAutoFill'
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@graphcommerce/address-fields-nl",
3
3
  "homepage": "https://www.graphcommerce.org/",
4
4
  "repository": "github:graphcommerce-org/graphcommerce",
5
- "version": "8.1.0-canary.3",
5
+ "version": "8.1.0-canary.6",
6
6
  "sideEffects": false,
7
7
  "prettier": "@graphcommerce/prettier-config-pwa",
8
8
  "eslintConfig": {
@@ -12,14 +12,14 @@
12
12
  }
13
13
  },
14
14
  "peerDependencies": {
15
- "@graphcommerce/ecommerce-ui": "^8.1.0-canary.3",
16
- "@graphcommerce/eslint-config-pwa": "^8.1.0-canary.3",
17
- "@graphcommerce/graphql": "^8.1.0-canary.3",
18
- "@graphcommerce/magento-customer": "^8.1.0-canary.3",
19
- "@graphcommerce/magento-store": "^8.1.0-canary.3",
20
- "@graphcommerce/next-ui": "^8.1.0-canary.3",
21
- "@graphcommerce/prettier-config-pwa": "^8.1.0-canary.3",
22
- "@graphcommerce/typescript-config-pwa": "^8.1.0-canary.3",
15
+ "@graphcommerce/ecommerce-ui": "^8.1.0-canary.6",
16
+ "@graphcommerce/eslint-config-pwa": "^8.1.0-canary.6",
17
+ "@graphcommerce/graphql": "^8.1.0-canary.6",
18
+ "@graphcommerce/magento-customer": "^8.1.0-canary.6",
19
+ "@graphcommerce/magento-store": "^8.1.0-canary.6",
20
+ "@graphcommerce/next-ui": "^8.1.0-canary.6",
21
+ "@graphcommerce/prettier-config-pwa": "^8.1.0-canary.6",
22
+ "@graphcommerce/typescript-config-pwa": "^8.1.0-canary.6",
23
23
  "@lingui/core": "^4.2.1",
24
24
  "@lingui/macro": "^4.2.1",
25
25
  "@lingui/react": "^4.2.1",
@@ -1,18 +1,49 @@
1
- import { AddressFieldsProps } from '@graphcommerce/magento-customer'
1
+ import { FieldPath, FieldValues, useWatch } from '@graphcommerce/ecommerce-ui'
2
+ import {
3
+ useAddressFieldsForm,
4
+ AddressAddition,
5
+ AddressCity,
6
+ AddressCountryRegion,
7
+ AddressFieldsProps,
8
+ AddressHousenumber,
9
+ AddressPostcode,
10
+ AddressStreet,
11
+ } from '@graphcommerce/magento-customer'
2
12
  import type { PluginProps } from '@graphcommerce/next-config'
3
- import { PostcodeNLAddressFields } from '../components/PostcodeNLAddressFields'
13
+ import { FormRow } from '@graphcommerce/next-ui'
14
+ import { PostcodeNLAutoFill } from '../components/PostcodeNLAutoFill'
4
15
 
5
16
  export const component = 'AddressFields'
17
+ export const exported = '@graphcommerce/magento-customer'
6
18
 
7
- export const exported = '@graphcommerce/magento-customer/components/AddressFields/AddressFields'
19
+ function AddPostcodeNLAddressFields<
20
+ TFieldValues extends FieldValues = FieldValues,
21
+ TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>,
22
+ >(props: PluginProps<AddressFieldsProps<TFieldValues, TName>>) {
23
+ const { Prev } = props
24
+ const { control, name } = useAddressFieldsForm(props)
25
+ const country = useWatch({ control, name: name.countryCode })
8
26
 
9
- function AddPostcodeNLAddressFields(props: PluginProps<AddressFieldsProps>) {
10
- const { form, Prev } = props
11
- const country = form.watch('countryCode')
12
- return country === 'NL' ? (
13
- <PostcodeNLAddressFields countryFirst {...props} />
14
- ) : (
15
- <Prev countryFirst {...props} />
27
+ return (
28
+ <>
29
+ {country === 'NL' ? (
30
+ <>
31
+ <AddressCountryRegion {...props} />
32
+ <FormRow>
33
+ <AddressPostcode {...props} />
34
+ <AddressHousenumber {...props} />
35
+ <AddressAddition {...props} />
36
+ </FormRow>
37
+ <FormRow>
38
+ <AddressStreet {...props} />
39
+ <AddressCity {...props} />
40
+ </FormRow>
41
+ </>
42
+ ) : (
43
+ <Prev countryFirst {...props} />
44
+ )}
45
+ <PostcodeNLAutoFill {...props} />
46
+ </>
16
47
  )
17
48
  }
18
49
 
@@ -1,167 +0,0 @@
1
- import {
2
- TextFieldElement,
3
- SelectElement,
4
- assertFormGqlOperation,
5
- houseNumberPattern,
6
- } from '@graphcommerce/ecommerce-ui'
7
- import { useQuery } from '@graphcommerce/graphql'
8
- import { AddressFieldsProps, AddressFieldValues } from '@graphcommerce/magento-customer'
9
- import { CountryRegionsDocument } from '@graphcommerce/magento-store'
10
- import { FormRow, InputCheckmark, filterNonNullableKeys } from '@graphcommerce/next-ui'
11
- import { i18n } from '@lingui/core'
12
- import { Trans } from '@lingui/react'
13
- import { useMemo } from 'react'
14
- import { usePostcodeService } from '../helpers/usePostcodeService'
15
-
16
- export function PostcodeNLAddressFields(props: AddressFieldsProps) {
17
- const { form, readOnly, countryFirst } = props
18
-
19
- const countryQuery = useQuery(CountryRegionsDocument, { fetchPolicy: 'cache-and-network' })
20
- const countries = countryQuery.data?.countries ?? countryQuery.previousData?.countries
21
-
22
- assertFormGqlOperation<AddressFieldValues>(form)
23
- const { watch, required, valid, control } = form
24
-
25
- const country = watch('countryCode')
26
-
27
- usePostcodeService(form)
28
-
29
- const countryList = useMemo(() => {
30
- const countriesWithLocale = (countries ?? [])?.filter((c) => c?.full_name_locale)
31
- return countriesWithLocale.sort((a, b) =>
32
- (a?.full_name_locale ?? '')?.localeCompare(b?.full_name_locale ?? ''),
33
- )
34
- }, [countries])
35
-
36
- const regionList = useMemo(() => {
37
- const availableRegions = Object.values(
38
- countryList.find((c) => c?.two_letter_abbreviation === country)?.available_regions ?? {},
39
- )
40
- type Region = (typeof availableRegions)[0]
41
-
42
- const compare: (a: Region, b: Region) => number = (a, b) =>
43
- (a?.name ?? '')?.localeCompare(b?.name ?? '')
44
-
45
- return availableRegions?.sort(compare)
46
- }, [country, countryList])
47
-
48
- const countryFields = (
49
- <FormRow>
50
- <SelectElement
51
- control={control}
52
- name='countryCode'
53
- SelectProps={{ autoWidth: true }}
54
- variant='outlined'
55
- label={<Trans id='Country' />}
56
- required={required.countryCode}
57
- InputProps={{
58
- readOnly,
59
- endAdornment: <InputCheckmark show={valid.countryCode} select />,
60
- }}
61
- options={filterNonNullableKeys(countryList, [
62
- 'two_letter_abbreviation',
63
- 'full_name_locale',
64
- ]).map(({ two_letter_abbreviation: id, full_name_locale: label }) => ({ id, label }))}
65
- />
66
-
67
- {regionList.length > 0 && (
68
- <SelectElement
69
- control={control}
70
- name='regionId'
71
- // SelectProps={{ native: true, displayEmpty: true }}
72
- variant='outlined'
73
- label={<Trans id='Region' />}
74
- required
75
- InputProps={{
76
- readOnly,
77
- endAdornment: <InputCheckmark show={valid.regionId} select />,
78
- }}
79
- options={filterNonNullableKeys(regionList, ['id', 'name']).map(({ id, name: label }) => ({
80
- id,
81
- label,
82
- }))}
83
- />
84
- )}
85
- </FormRow>
86
- )
87
-
88
- return (
89
- <>
90
- {countryFirst && countryFields}
91
- <FormRow>
92
- <TextFieldElement
93
- control={control}
94
- name='postcode'
95
- variant='outlined'
96
- type='text'
97
- required={required.postcode}
98
- label={<Trans id='Postcode' />}
99
- InputProps={{
100
- readOnly,
101
- endAdornment: <InputCheckmark show={valid.postcode} />,
102
- }}
103
- />
104
- <TextFieldElement
105
- control={control}
106
- name='houseNumber'
107
- required={required.houseNumber}
108
- validation={{
109
- pattern: {
110
- value: houseNumberPattern,
111
- message: i18n._(/* i18n */ 'Please provide a valid house number'),
112
- },
113
- }}
114
- variant='outlined'
115
- type='text'
116
- label={<Trans id='Housenumber' />}
117
- autoComplete='address-line2'
118
- InputProps={{
119
- readOnly,
120
- endAdornment: <InputCheckmark show={valid.houseNumber} />,
121
- }}
122
- />
123
- <TextFieldElement
124
- control={control}
125
- name='addition'
126
- variant='outlined'
127
- type='text'
128
- required={required.addition}
129
- label={<Trans id='Addition' />}
130
- autoComplete='address-line3'
131
- InputProps={{
132
- readOnly,
133
- endAdornment: <InputCheckmark show={valid.addition} />,
134
- }}
135
- />
136
- </FormRow>
137
- <FormRow>
138
- <TextFieldElement
139
- variant='outlined'
140
- control={control}
141
- required={required.street}
142
- name='street'
143
- type='text'
144
- label={<Trans id='Street' />}
145
- autoComplete='address-line1'
146
- InputProps={{
147
- readOnly,
148
- endAdornment: <InputCheckmark show={valid.street} />,
149
- }}
150
- />
151
- <TextFieldElement
152
- control={control}
153
- name='city'
154
- variant='outlined'
155
- type='text'
156
- required={required.city}
157
- label={<Trans id='City' />}
158
- InputProps={{
159
- readOnly,
160
- endAdornment: <InputCheckmark show={valid.city} />,
161
- }}
162
- />
163
- </FormRow>
164
- {!countryFirst && countryFields}
165
- </>
166
- )
167
- }
@@ -1,61 +0,0 @@
1
- import { UseFormReturn } from '@graphcommerce/ecommerce-ui'
2
- import { useLazyQuery } from '@graphcommerce/graphql'
3
- import { AddressFieldValues } from '@graphcommerce/magento-customer'
4
- import { useEffect } from 'react'
5
- import { postcodeNLDocument } from '../graphql/PostcodeNL.gql'
6
-
7
- const postCodeRegex = /^[1-9][0-9]{3}[a-z]{2}$/i
8
-
9
- const defaultPostcodeFieldnames = {
10
- postcode: 'postcode',
11
- houseNumber: 'houseNumber',
12
- addition: 'addition',
13
- street: 'street',
14
- city: 'city',
15
- }
16
-
17
- export function usePostcodeService(
18
- form: UseFormReturn<any>,
19
- fieldNames: AddressFieldValues = defaultPostcodeFieldnames,
20
- ) {
21
- const { watch, setValue, resetField } = form
22
-
23
- const postcode = watch(fieldNames.postcode ?? '', '') as string
24
- const houseNumber = watch(fieldNames.houseNumber ?? '', '') as string
25
- const addition = watch(fieldNames.addition ?? '', '') as string
26
- const [execute, result] = useLazyQuery(postcodeNLDocument)
27
-
28
- useEffect(() => {
29
- if (!houseNumber || !postcode || !postCodeRegex.test(postcode.trim().replace(/ /g, '')))
30
- return () => {}
31
-
32
- const handler = () =>
33
- execute({
34
- variables: {
35
- postcode: postcode.trim().replace(/ /g, ''),
36
- housenumber: addition ? houseNumber + addition : houseNumber,
37
- },
38
- })
39
- const clear = setTimeout(handler, 300)
40
-
41
- return () => clearInterval(clear)
42
- }, [execute, houseNumber, postcode, addition])
43
-
44
- if (process.env.NODE_ENV !== 'production') {
45
- if (result.error) {
46
- console.warn(result.error)
47
- }
48
- }
49
-
50
- useEffect(() => {
51
- const { street, city } = result.data?.postcodeNL ?? {}
52
- if (street && city) {
53
- setValue(fieldNames.street ?? '', street, { shouldValidate: true })
54
- setValue(fieldNames.city ?? '', city, { shouldValidate: true })
55
- } else {
56
- resetField(fieldNames.street ?? '')
57
- resetField(fieldNames.city ?? '')
58
- }
59
- }, [result.data?.postcodeNL, setValue, resetField, fieldNames.street, fieldNames.city])
60
- return [result.data?.postcodeNL, result.loading] as const
61
- }