@nuskin/address-lookup 1.0.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/README.md ADDED
@@ -0,0 +1,103 @@
1
+ # NPM Library Template
2
+
3
+ ### This template is for creating NPM module libraries
4
+
5
+ ----
6
+ ### What this template does for you
7
+
8
+ - Provides a `.gitlab-ci.yml` to manage the CI/CD pipeline
9
+ - Runs your *Unit Tests* with every push to the remote repository
10
+ - Analyzes your code with:
11
+ - linting rules
12
+ - run a *SAST* check
13
+ - Pushes your code coverage analysis to SonarQube
14
+ - Ensures your code passes the SonarQube Quality Gate
15
+ - Utilizes *Semantic Release*, which means the pipeline will handle versioning
16
+ - Publishes your module to *npmjs*
17
+
18
+ ----
19
+ ### Follow these steps to create a new project using this template:
20
+
21
+ #### 1. Clone this project to your local machine and remove the git control file
22
+ Note: We use 'my-project' as the name of your new project
23
+ ```bash
24
+ git clone git@code.tls.nuskin.io:ns-am/templates/npm-library-template.git <my-project>
25
+ cd <my-project>
26
+ rm -rf .git
27
+ ```
28
+
29
+ #### 2. Create your new project in Gitlab
30
+
31
+ 1. In the appropriate sub-group select **"New project"**
32
+ 2. Name your project
33
+ 3. Select a project description (optional)
34
+ 4. Select **"Create project"**
35
+
36
+ #### 3. Connect your local project to the gitlab remote project
37
+ You can copy and paste the section in the gitlab command line instructions of your new
38
+ project into the command line of your local project. It will look like the following
39
+ but will have your project specific details.
40
+ ```bash
41
+ cd <your project folder if you are not already there>
42
+ git init
43
+ git remote add origin <your gitlab project url>
44
+ git add .
45
+ git commit -m "Chore: Initial commit"
46
+ git push -u origin master
47
+ ```
48
+
49
+ #### 4. Add rules to your new project repository
50
+
51
+ - Under *Settings* Select *Repository*
52
+ - Select *Push Rules* ([See Sample](./push-rules.png))
53
+ 1. Check *Do not allow users to remove git tags with `git push`*
54
+ - Click on **Expand** in the *Protected Branches* section ([See Sample](./protected-branches.png))
55
+ - **master** should already be set as your default branch. For **master** do the following:
56
+ 1. Set *Allowed to merge* to **Developers + Maintainers**
57
+ 2. Set *Allowed to push* to **Maintainers**
58
+ 3. Set *Code owner approval* to **Off**
59
+
60
+
61
+ #### 5. Update your new project with your project specific settings and information
62
+
63
+ 1. Replace the `README.md` with a proper readme that will be displayed on *npmjs* ([See Sample](./README-sample.md))
64
+ 2. Update these settings in your `package.json`
65
+ - Note: All module names should be created in the *@nuskin* namespace.
66
+ ```JavaScript
67
+ {
68
+ "name": "@nuskin/npm-library-template",
69
+ "description": "The description that will amaze and astound your audience when they read it",
70
+ "repository": {
71
+ "type": "git",
72
+ "url": "git@code.tls.nuskin.io:ns-am/templates/npm-library-template.git"
73
+ },
74
+ "author": "Ian Harisay <imharisa@nuskin.com>",
75
+ "homepage": "https://code.tls.nuskin.io/ns-am/templates/npm-library-template/blob/master/README.md"
76
+ }
77
+ ```
78
+
79
+ #### 6. Determine if your module should be public or private
80
+ If your module should be public and published to *npmjs.com*, nothing needs to be done. This is the default
81
+ behavior. If you need to publish to the private npm repository *nexus3.nuskin.net*, inside `gitlab-ci.yml`
82
+ update **PRIVATE_NPM** to `true`
83
+ ```yaml
84
+ variables:
85
+ PRIVATE_NPM: "true"
86
+ ```
87
+ #### 7. Turning on your CI/CD pipeline
88
+
89
+ Once you are ready for your project to start running the CI/CD pipeline, you should rename the `gitlab-ci.yml`
90
+ to `.gitlab-ci.yml`.
91
+ ```bash
92
+ git mv gitlab-ci.yml .gitlab-ci.yml
93
+ git commit -am"Chore: renaming gitlab-ci.yml to .gitlab-ci.yml so my pipeline runs"
94
+ git push
95
+ ```
96
+
97
+ ## TODO: Write documentation about Semantic Release (don't forget prereleases)
98
+
99
+ #### How to use Semantic Release in your pipeline
100
+
101
+ Link to another page or write up instructions on how Semantic Release works with the pipeline
102
+
103
+ [eslint commit-analyzer](https://github.com/conventional-changelog/conventional-changelog/tree/master/packages/conventional-changelog-eslint) rules.
package/package.json ADDED
@@ -0,0 +1,55 @@
1
+ {
2
+ "name": "@nuskin/address-lookup",
3
+ "version": "1.0.0",
4
+ "description": "React component for address autocomplete using Egon and Smarty services",
5
+ "main": "src/index.js",
6
+ "scripts": {
7
+ "audit": "yarn audit --level high",
8
+ "jsdoc": "node_modules/.bin/jsdoc -a all -c jsdoc.json -r -d jsdocs",
9
+ "lint": "eslint src __tests__",
10
+ "test": "jest --coverage",
11
+ "dev": "cd test && npm run dev"
12
+ },
13
+ "publishConfig": {
14
+ "access": "public"
15
+ },
16
+ "repository": {
17
+ "type": "git",
18
+ "url": "git@code.tls.nuskin.io:nextgen-development/shipping/npm/address-lookup.git"
19
+ },
20
+ "keywords": [
21
+ "react",
22
+ "address",
23
+ "autocomplete",
24
+ "lookup",
25
+ "egon",
26
+ "smarty"
27
+ ],
28
+ "author": "Mr. Roboto",
29
+ "license": "MIT",
30
+ "homepage": "https://code.tls.nuskin.io/nextgen-development/shipping/npm/address-lookup/blob/master/README.md",
31
+ "peerDependencies": {
32
+ "react": ">=18.0.0",
33
+ "react-dom": ">=18.0.0"
34
+ },
35
+ "devDependencies": {
36
+ "@nuskin/docdash": "1.0.1",
37
+ "axios": "^1.6.0",
38
+ "@babel/eslint-parser": "7.28.6",
39
+ "eslint": "7.21.0",
40
+ "eslint-config-google": "0.14.0",
41
+ "eslint-config-prettier": "8.1.0",
42
+ "eslint-plugin-json": "2.1.2",
43
+ "eslint-plugin-prettier": "3.3.1",
44
+ "eslint-plugin-react": "7.37.5",
45
+ "jest": "26.6.3",
46
+ "jest-sonar-reporter": "2.0.0",
47
+ "jsdoc": "3.6.6",
48
+ "prettier": "2.2.1",
49
+ "react": "^18.0.0",
50
+ "react-dom": "^18.0.0"
51
+ },
52
+ "files": [
53
+ "src/"
54
+ ]
55
+ }
@@ -0,0 +1,32 @@
1
+ import React, { useState } from 'react';
2
+ import EgonResults from './components/EgonResults.jsx';
3
+ import SmartyResults from './components/SmartyResults.jsx';
4
+ import { COUNTRY_MAP } from './countries';
5
+
6
+ const AddressLookup = ({ onAddressSelect }) => {
7
+ const [countryCode, setCountryCode] = useState('');
8
+
9
+ return (
10
+ <div>
11
+ <select
12
+ value={countryCode}
13
+ onChange={(e) => setCountryCode(e.target.value)}
14
+ style={{ marginBottom: '20px', padding: '8px' }}
15
+ >
16
+ <option value="">Select Country</option>
17
+ {Object.entries(COUNTRY_MAP).map(([code, country]) => (
18
+ <option key={code} value={code}>
19
+ {country.name}
20
+ </option>
21
+ ))}
22
+ </select>
23
+
24
+ <div style={{ display: 'flex', gap: '20px' }}>
25
+ <EgonResults countryCode={countryCode} onSelect={onAddressSelect} />
26
+ <SmartyResults countryCode={countryCode} onSelect={onAddressSelect} />
27
+ </div>
28
+ </div>
29
+ );
30
+ };
31
+
32
+ export default AddressLookup;
@@ -0,0 +1,102 @@
1
+ import React, { useState } from 'react';
2
+ import useEgonLookup from './hooks/useEgonLookup.jsx';
3
+ import useEgonNormalize from './hooks/useEgonNormalize.jsx';
4
+
5
+ const EgonResults = ({ countryCode, onSelect }) => {
6
+ const [query, setQuery] = useState('');
7
+ const [selectedAddress, setSelectedAddress] = useState(null);
8
+ const { suggestions, loading, error, responseTime } = useEgonLookup(query, countryCode);
9
+ const {
10
+ normalizedAddress,
11
+ loading: normalizeLoading,
12
+ error: normalizeError,
13
+ responseTime: normalizeResponseTime
14
+ } = useEgonNormalize(selectedAddress, countryCode);
15
+
16
+ const handleSelect = (address) => {
17
+ setSelectedAddress(address);
18
+ onSelect?.({ type: 'egon', address });
19
+ };
20
+
21
+ const displayAddress = normalizedAddress || selectedAddress;
22
+
23
+ return (
24
+ <div style={{ flex: 1 }}>
25
+ <h4>Egon</h4>
26
+ <input
27
+ type="text"
28
+ value={query}
29
+ onChange={(e) => {
30
+ setSelectedAddress(null);
31
+ setQuery(e.target.value);
32
+ }}
33
+ placeholder="Enter address..."
34
+ style={{ marginBottom: '10px', padding: '8px', width: '100%' }}
35
+ />
36
+ {responseTime && (
37
+ <div style={{ fontSize: '12px', color: '#666', marginBottom: '10px' }}>
38
+ Response time: {responseTime}ms
39
+ </div>
40
+ )}
41
+ {loading && <div>Loading...</div>}
42
+ {error && <div>Error: {error}</div>}
43
+ {selectedAddress ? (
44
+ <div style={{ border: '1px solid #ccc', padding: '10px', borderRadius: '5px' }}>
45
+ {normalizeLoading && <div>Normalizing...</div>}
46
+ {normalizeError && (
47
+ <div style={{ color: 'red' }}>Normalize Error: {normalizeError}</div>
48
+ )}
49
+ {normalizeResponseTime && (
50
+ <div style={{ fontSize: '12px', color: '#666', marginBottom: '10px' }}>
51
+ Normalize time: {normalizeResponseTime}ms
52
+ </div>
53
+ )}
54
+ {displayAddress?.standard ? (
55
+ <>
56
+ <div><strong>Address:</strong> {displayAddress.standard.address}</div>
57
+ <div><strong>Full Address:</strong> {displayAddress.standard.full_address}</div>
58
+ <div><strong>Street:</strong> {displayAddress.standard.street}</div>
59
+ <div><strong>City:</strong> {displayAddress.standard.city}</div>
60
+ <div><strong>Province:</strong> {displayAddress.standard.province}</div>
61
+ <div><strong>Region:</strong> {displayAddress.standard.region}</div>
62
+ <div><strong>Postal Code:</strong> {displayAddress.standard.zipcode}</div>
63
+ <div><strong>Country:</strong> {displayAddress.standard.country}</div>
64
+ </>
65
+ ) : displayAddress ? (
66
+ <>
67
+ <div>
68
+ <strong>Street:</strong> {displayAddress.hn_num}
69
+ {displayAddress.hn_exp || ''} {displayAddress.street}
70
+ </div>
71
+ <div><strong>City:</strong> {displayAddress.city}</div>
72
+ <div><strong>Province:</strong> {displayAddress.province}</div>
73
+ <div><strong>Region:</strong> {displayAddress.region}</div>
74
+ <div><strong>Postal Code:</strong> {displayAddress.zipcode}</div>
75
+ <div><strong>Country:</strong> {displayAddress.country}</div>
76
+ </>
77
+ ) : null}
78
+ <button
79
+ onClick={() => setSelectedAddress(null)}
80
+ style={{ marginTop: '8px', padding: '4px 8px' }}
81
+ >
82
+ Clear
83
+ </button>
84
+ </div>
85
+ ) : (
86
+ <ul>
87
+ {suggestions.map((suggestion, index) => (
88
+ <li
89
+ key={index}
90
+ onClick={() => handleSelect(suggestion)}
91
+ style={{ cursor: 'pointer' }}
92
+ >
93
+ {suggestion.displayText}
94
+ </li>
95
+ ))}
96
+ </ul>
97
+ )}
98
+ </div>
99
+ );
100
+ };
101
+
102
+ export default EgonResults;
@@ -0,0 +1,143 @@
1
+ import React, { useState, useEffect } from 'react';
2
+ import useSmartyUSLookup from './hooks/useSmartyUSLookup.jsx';
3
+ import useSmartyInternationalLookup from './hooks/useSmartyInternationalLookup.jsx';
4
+
5
+ const SmartyResults = ({ countryCode, onSelect }) => {
6
+ const [query, setQuery] = useState('');
7
+ const [selectedAddress, setSelectedAddress] = useState(null);
8
+ const [selectedUSAddress, setSelectedUSAddress] = useState(null);
9
+ const [selectedIntlAddressId, setSelectedIntlAddressId] = useState(null);
10
+
11
+ // Use appropriate Smarty hook based on country
12
+ const isUS = countryCode === 'US';
13
+ const {
14
+ suggestions: smartyUSSuggestions,
15
+ loading: smartyUSLoading,
16
+ error: smartyUSError,
17
+ responseTime: smartyUSResponseTime
18
+ } = useSmartyUSLookup(isUS ? query : '', selectedUSAddress, 300);
19
+ const {
20
+ suggestions: smartyIntlSuggestions,
21
+ loading: smartyIntlLoading,
22
+ error: smartyIntlError,
23
+ responseTime: smartyIntlResponseTime
24
+ } = useSmartyInternationalLookup(!isUS ? query : '', countryCode, selectedIntlAddressId, 300);
25
+
26
+ const suggestions = isUS ? smartyUSSuggestions : smartyIntlSuggestions;
27
+ const loading = isUS ? smartyUSLoading : smartyIntlLoading;
28
+ const error = isUS ? smartyUSError : smartyIntlError;
29
+ const responseTime = isUS ? smartyUSResponseTime : smartyIntlResponseTime;
30
+
31
+ // Auto-select detailed results (don't show as list)
32
+ useEffect(() => {
33
+ if (suggestions.length === 1 && suggestions[0].isDetailed) {
34
+ setSelectedAddress(suggestions[0]);
35
+ onSelect?.({ type: 'smarty', address: suggestions[0] });
36
+ } else if (suggestions.length > 0 && !suggestions[0].isDetailed) {
37
+ // Clear selection when we get new summary results
38
+ setSelectedAddress(null);
39
+ }
40
+ }, [suggestions, onSelect]);
41
+
42
+ const handleInputChange = (e) => {
43
+ setSelectedAddress(null);
44
+ setSelectedUSAddress(null);
45
+ setSelectedIntlAddressId(null);
46
+ setQuery(e.target.value);
47
+ };
48
+
49
+ const handleSelect = (address) => {
50
+ if (isUS) {
51
+ // US flow - check if entries > 1 for secondary expansion
52
+ if (address.entries > 1) {
53
+ setSelectedUSAddress(address);
54
+ const baseAddress = `${address.street_line} ${address.secondary}`.trim();
55
+ setQuery(baseAddress);
56
+ } else {
57
+ setSelectedUSAddress(null);
58
+ setSelectedAddress(address);
59
+ onSelect?.({ type: 'smarty', address });
60
+ }
61
+ } else {
62
+ // International flow - check if we need to query with address_id
63
+ if (address.entries > 1 || (address.entries === 1 && address.isDetailed === false)) {
64
+ setSelectedIntlAddressId(address.address_id);
65
+ } else {
66
+ setSelectedIntlAddressId(null);
67
+ setSelectedAddress(address);
68
+ onSelect?.({ type: 'smarty', address });
69
+ }
70
+ }
71
+ };
72
+
73
+ return (
74
+ <div style={{ flex: 1 }}>
75
+ <h4>Smarty</h4>
76
+ <input
77
+ type="text"
78
+ value={query}
79
+ onChange={handleInputChange}
80
+ placeholder="Enter address..."
81
+ style={{ marginBottom: '10px', padding: '8px', width: '100%' }}
82
+ />
83
+ {responseTime && (
84
+ <div style={{ fontSize: '12px', color: '#666', marginBottom: '10px' }}>
85
+ Response time: {responseTime}ms
86
+ </div>
87
+ )}
88
+ {loading && <div>Loading...</div>}
89
+ {error && <div>Error: {error}</div>}
90
+ {selectedAddress ? (
91
+ <div style={{ border: '1px solid #ccc', padding: '10px', borderRadius: '5px' }}>
92
+ {selectedAddress.street_line ? (
93
+ // US format
94
+ <>
95
+ <div><strong>Street:</strong> {selectedAddress.street_line}</div>
96
+ {selectedAddress.secondary && (
97
+ <div><strong>Secondary:</strong> {selectedAddress.secondary}</div>
98
+ )}
99
+ <div><strong>City:</strong> {selectedAddress.city}</div>
100
+ <div><strong>State:</strong> {selectedAddress.state}</div>
101
+ <div><strong>Zipcode:</strong> {selectedAddress.zipcode}</div>
102
+ {selectedAddress.entries > 0 && (
103
+ <div><strong>Entries:</strong> {selectedAddress.entries}</div>
104
+ )}
105
+ </>
106
+ ) : (
107
+ // International format
108
+ <>
109
+ <div><strong>Street:</strong> {selectedAddress.street}</div>
110
+ <div><strong>Locality:</strong> {selectedAddress.locality}</div>
111
+ <div>
112
+ <strong>Administrative Area:</strong>{' '}
113
+ {selectedAddress.administrative_area_long || selectedAddress.administrative_area}
114
+ </div>
115
+ <div><strong>Postal Code:</strong> {selectedAddress.postal_code}</div>
116
+ <div><strong>Country:</strong> {selectedAddress.country_iso3}</div>
117
+ </>
118
+ )}
119
+ <button
120
+ onClick={() => setSelectedAddress(null)}
121
+ style={{ marginTop: '8px', padding: '4px 8px' }}
122
+ >
123
+ Clear
124
+ </button>
125
+ </div>
126
+ ) : (
127
+ <ul>
128
+ {suggestions.map((suggestion, index) => (
129
+ <li
130
+ key={index}
131
+ onClick={() => handleSelect(suggestion)}
132
+ style={{ cursor: 'pointer' }}
133
+ >
134
+ {suggestion.displayText}
135
+ </li>
136
+ ))}
137
+ </ul>
138
+ )}
139
+ </div>
140
+ );
141
+ };
142
+
143
+ export default SmartyResults;
@@ -0,0 +1,24 @@
1
+ export const COUNTRY_MAP = {
2
+ 'US': { name: 'United States', iso2: 'US', iso3: 'USA' },
3
+ 'CA': { name: 'Canada', iso2: 'CA', iso3: 'CAN' },
4
+ 'MX': { name: 'Mexico', iso2: 'MX', iso3: 'MEX' },
5
+ 'PE': { name: 'Peru', iso2: 'PE', iso3: 'PER' },
6
+ 'CO': { name: 'Colombia', iso2: 'CO', iso3: 'COL' },
7
+ 'CL': { name: 'Chile', iso2: 'CL', iso3: 'CHL' },
8
+ 'AR': { name: 'Argentina', iso2: 'AR', iso3: 'ARG' },
9
+ 'DE': { name: 'Germany', iso2: 'DE', iso3: 'DEU' },
10
+ 'FR': { name: 'France', iso2: 'FR', iso3: 'FRA' },
11
+ 'IT': { name: 'Italy', iso2: 'IT', iso3: 'ITA' },
12
+ 'ES': { name: 'Spain', iso2: 'ES', iso3: 'ESP' },
13
+ 'GB': { name: 'United Kingdom', iso2: 'GB', iso3: 'GBR' },
14
+ 'NL': { name: 'Netherlands', iso2: 'NL', iso3: 'NLD' },
15
+ 'BE': { name: 'Belgium', iso2: 'BE', iso3: 'BEL' },
16
+ 'AT': { name: 'Austria', iso2: 'AT', iso3: 'AUT' },
17
+ 'CH': { name: 'Switzerland', iso2: 'CH', iso3: 'CHE' },
18
+ 'PL': { name: 'Poland', iso2: 'PL', iso3: 'POL' },
19
+ 'PT': { name: 'Portugal', iso2: 'PT', iso3: 'PRT' },
20
+ 'SE': { name: 'Sweden', iso2: 'SE', iso3: 'SWE' },
21
+ 'NO': { name: 'Norway', iso2: 'NO', iso3: 'NOR' },
22
+ 'DK': { name: 'Denmark', iso2: 'DK', iso3: 'DNK' },
23
+ 'FI': { name: 'Finland', iso2: 'FI', iso3: 'FIN' }
24
+ };
@@ -0,0 +1,85 @@
1
+ import { useState, useEffect } from 'react';
2
+ import axios from 'axios';
3
+ import { COUNTRY_MAP } from '../countries';
4
+
5
+ const useEgonLookup = (query, countryCode = '', debounceMs = 300) => {
6
+ const [suggestions, setSuggestions] = useState([]);
7
+ const [loading, setLoading] = useState(false);
8
+ const [error, setError] = useState(null);
9
+ const [responseTime, setResponseTime] = useState(null);
10
+
11
+ useEffect(() => {
12
+ if (!query.trim() || !countryCode || !COUNTRY_MAP[countryCode]) {
13
+ setSuggestions([]);
14
+ setLoading(false);
15
+ setError(null);
16
+ setResponseTime(null);
17
+ return;
18
+ }
19
+
20
+ setLoading(true);
21
+ setError(null);
22
+
23
+ const timer = setTimeout(async () => {
24
+ try {
25
+ const iso3 = COUNTRY_MAP[countryCode].iso3;
26
+
27
+ const startTime = performance.now();
28
+ const response = await axios.post(
29
+ 'https://egonapis.egoncloud.com:1257/Egon/api/single-line/full-address',
30
+ {
31
+ par: {
32
+ iso3: iso3
33
+ },
34
+ data: {
35
+ query: query
36
+ }
37
+ },
38
+ {
39
+ headers: {
40
+ 'token': 'WPT04CNSK01@X04DXCIB'
41
+ }
42
+ }
43
+ );
44
+ const endTime = performance.now();
45
+ setResponseTime(Math.round(endTime - startTime));
46
+
47
+ const results = response.data.data?.results || [];
48
+ const formattedSuggestions = results.map(result => {
49
+ const parts = [
50
+ result.hn_num,
51
+ result.street,
52
+ result.city,
53
+ result.state,
54
+ result.zipcode
55
+ ].filter(Boolean);
56
+
57
+ return {
58
+ ...result,
59
+ displayText: parts.join(' ')
60
+ };
61
+ });
62
+
63
+ setSuggestions(formattedSuggestions);
64
+ } catch (err) {
65
+ console.error('Egon lookup failed:', err);
66
+ setError(err.message);
67
+ setSuggestions([]);
68
+ setResponseTime(null);
69
+ } finally {
70
+ setLoading(false);
71
+ }
72
+ }, debounceMs);
73
+
74
+ return () => clearTimeout(timer);
75
+ }, [query, countryCode, debounceMs]);
76
+
77
+ return {
78
+ suggestions,
79
+ loading,
80
+ error,
81
+ responseTime
82
+ };
83
+ };
84
+
85
+ export default useEgonLookup;
@@ -0,0 +1,109 @@
1
+ import { useState, useEffect } from 'react';
2
+ import axios from 'axios';
3
+ import { COUNTRY_MAP } from '../countries';
4
+
5
+ const useEgonNormalize = (address, countryCode) => {
6
+ const [normalizedAddress, setNormalizedAddress] = useState(null);
7
+ const [loading, setLoading] = useState(false);
8
+ const [error, setError] = useState(null);
9
+ const [responseTime, setResponseTime] = useState(null);
10
+
11
+ useEffect(() => {
12
+ if (!address || !countryCode || !COUNTRY_MAP[countryCode]) {
13
+ setNormalizedAddress(null);
14
+ setLoading(false);
15
+ setError(null);
16
+ setResponseTime(null);
17
+ return;
18
+ }
19
+
20
+ setLoading(true);
21
+ setError(null);
22
+
23
+ const normalizeAddress = async () => {
24
+ try {
25
+ const iso3 = COUNTRY_MAP[countryCode].iso3;
26
+
27
+ // Build address object with all available fields from suggestion
28
+ const addressData = {};
29
+ const fieldMap = {
30
+ egoncode_place: 'egoncode_place',
31
+ egoncode_hn: 'egoncode_hn',
32
+ country: 'country',
33
+ state: 'state',
34
+ region: 'region',
35
+ province: 'province',
36
+ city: 'city',
37
+ district1: 'district1',
38
+ district2: 'district2',
39
+ district3: 'district3',
40
+ zipcode: 'zipcode',
41
+ street_type: 'street_type',
42
+ street: 'street',
43
+ address: 'address',
44
+ hn: 'hn',
45
+ building: 'building',
46
+ sub_building: 'sub_building',
47
+ organization: 'organization',
48
+ street_type_str2: 'street_type_str2',
49
+ street_2: 'street_2',
50
+ hn_2: 'hn_2'
51
+ };
52
+
53
+ // Add all available fields from the address
54
+ Object.keys(address).forEach(key => {
55
+ if (address[key] && fieldMap[key]) {
56
+ addressData[fieldMap[key]] = address[key];
57
+ }
58
+ });
59
+
60
+ // Special handling for house number fields
61
+ if (address.hn_num || address.hn_exp) {
62
+ const hnValue = address.hn_num || '';
63
+ const hnExpValue = address.hn_exp || '';
64
+ addressData.hn = hnExpValue ? `${hnValue}/${hnExpValue}` : hnValue;
65
+ }
66
+
67
+ const startTime = performance.now();
68
+ const response = await axios.post(
69
+ 'https://egonapis.egoncloud.com:1257/Egon/api/norm',
70
+ {
71
+ par: {
72
+ iso3: iso3
73
+ },
74
+ data: {
75
+ address: addressData
76
+ }
77
+ },
78
+ {
79
+ headers: {
80
+ 'token': 'WPT04CNSK01@X04DXCIB'
81
+ }
82
+ }
83
+ );
84
+ const endTime = performance.now();
85
+ setResponseTime(Math.round(endTime - startTime));
86
+
87
+ setNormalizedAddress(response.data.data?.address);
88
+ } catch (err) {
89
+ console.error('Egon normalize failed:', err);
90
+ setError(err.message);
91
+ setNormalizedAddress(null);
92
+ setResponseTime(null);
93
+ } finally {
94
+ setLoading(false);
95
+ }
96
+ };
97
+
98
+ normalizeAddress();
99
+ }, [address, countryCode]);
100
+
101
+ return {
102
+ normalizedAddress,
103
+ loading,
104
+ error,
105
+ responseTime
106
+ };
107
+ };
108
+
109
+ export default useEgonNormalize;
@@ -0,0 +1,98 @@
1
+ import { useState, useEffect } from 'react';
2
+ import axios from 'axios';
3
+ import { COUNTRY_MAP } from '../countries';
4
+
5
+ const useSmartyInternationalLookup = (
6
+ query,
7
+ countryCode = '',
8
+ selectedAddressId = null,
9
+ debounceMs = 300
10
+ ) => {
11
+ const [suggestions, setSuggestions] = useState([]);
12
+ const [loading, setLoading] = useState(false);
13
+ const [error, setError] = useState(null);
14
+ const [responseTime, setResponseTime] = useState(null);
15
+
16
+ useEffect(() => {
17
+ if (
18
+ (!query.trim() && !selectedAddressId) ||
19
+ !countryCode ||
20
+ !COUNTRY_MAP[countryCode]
21
+ ) {
22
+ setSuggestions([]);
23
+ setLoading(false);
24
+ setError(null);
25
+ return;
26
+ }
27
+
28
+ setLoading(true);
29
+ setError(null);
30
+
31
+ const timer = setTimeout(async () => {
32
+ try {
33
+ const iso3 = COUNTRY_MAP[countryCode].iso3;
34
+
35
+ let smartyUrl;
36
+ if (selectedAddressId) {
37
+ // Query with address_id for detailed or subunit results
38
+ smartyUrl = 'https://international-autocomplete.api.smarty.com/v2/lookup/' +
39
+ `${selectedAddressId}?key=22361352819063501&country=${iso3}`;
40
+ } else {
41
+ // Regular search query
42
+ smartyUrl = 'https://international-autocomplete.api.smarty.com/v2/lookup?' +
43
+ `key=22361352819063501&country=${iso3}&search=${encodeURIComponent(query)}`;
44
+ }
45
+
46
+ const startTime = performance.now();
47
+ const response = await axios.get(smartyUrl);
48
+ const endTime = performance.now();
49
+ setResponseTime(Math.round(endTime - startTime));
50
+
51
+ const results = response.data.candidates || [];
52
+ const formattedSuggestions = results.map(result => {
53
+ // Check if this is a detailed result (has street field) or summary result
54
+ if (result.street) {
55
+ // Detailed result - format for display
56
+ const displayText = `${result.street}, ${result.locality}, ` +
57
+ `${result.administrative_area} ${result.postal_code}`;
58
+ return {
59
+ ...result,
60
+ displayText: displayText,
61
+ isDetailed: true
62
+ };
63
+ } else {
64
+ // Summary result - use address_text and append entries if > 1
65
+ let displayText = result.address_text;
66
+ if (result.entries > 1) {
67
+ displayText += ` (${result.entries})`;
68
+ }
69
+ return {
70
+ ...result,
71
+ displayText: displayText,
72
+ isDetailed: false
73
+ };
74
+ }
75
+ });
76
+
77
+ setSuggestions(formattedSuggestions);
78
+ } catch (err) {
79
+ console.error('Smarty International lookup failed:', err);
80
+ setError(err.message);
81
+ setSuggestions([]);
82
+ } finally {
83
+ setLoading(false);
84
+ }
85
+ }, debounceMs);
86
+
87
+ return () => clearTimeout(timer);
88
+ }, [query, countryCode, selectedAddressId, debounceMs]);
89
+
90
+ return {
91
+ suggestions,
92
+ loading,
93
+ error,
94
+ responseTime
95
+ };
96
+ };
97
+
98
+ export default useSmartyInternationalLookup;
@@ -0,0 +1,83 @@
1
+ import { useState, useEffect } from 'react';
2
+ import axios from 'axios';
3
+
4
+ const useSmartyUSLookup = (query, selectedAddress = null, debounceMs = 300) => {
5
+ const [suggestions, setSuggestions] = useState([]);
6
+ const [loading, setLoading] = useState(false);
7
+ const [error, setError] = useState(null);
8
+ const [responseTime, setResponseTime] = useState(null);
9
+
10
+ useEffect(() => {
11
+ if (!query.trim()) {
12
+ setSuggestions([]);
13
+ setLoading(false);
14
+ setError(null);
15
+ setResponseTime(null);
16
+ return;
17
+ }
18
+
19
+ setLoading(true);
20
+ setError(null);
21
+
22
+ const timer = setTimeout(async () => {
23
+ try {
24
+ let smartyUrl = 'https://us-autocomplete-pro.api.smarty.com/' +
25
+ `lookup?key=22361352819063501&search=${encodeURIComponent(query)}`;
26
+
27
+ // Add selected parameter if provided (for secondary expansion)
28
+ if (selectedAddress) {
29
+ const selectedParam = `${selectedAddress.street_line} ` +
30
+ `${selectedAddress.secondary} (${selectedAddress.entries}) ` +
31
+ `${selectedAddress.city} ${selectedAddress.state} ${selectedAddress.zipcode}`;
32
+ smartyUrl += `&selected=${encodeURIComponent(selectedParam)}`;
33
+ }
34
+
35
+ const startTime = performance.now();
36
+ const response = await axios.get(smartyUrl);
37
+ const endTime = performance.now();
38
+ setResponseTime(Math.round(endTime - startTime));
39
+
40
+ const results = response.data.suggestions || [];
41
+ const formattedSuggestions = results.map(result => {
42
+ let whiteSpace = "";
43
+ let secondary = result.secondary || "";
44
+
45
+ if (secondary) {
46
+ if (result.entries > 1) {
47
+ secondary += " (" + result.entries + " entries)";
48
+ }
49
+ whiteSpace = " ";
50
+ }
51
+
52
+ const displayText = result.street_line + whiteSpace + secondary +
53
+ " " + result.city + ", " + result.state + " " + result.zipcode;
54
+
55
+ return {
56
+ ...result,
57
+ displayText: displayText
58
+ };
59
+ });
60
+
61
+ setSuggestions(formattedSuggestions);
62
+ } catch (err) {
63
+ console.error('Smarty US lookup failed:', err);
64
+ setError(err.message);
65
+ setSuggestions([]);
66
+ setResponseTime(null);
67
+ } finally {
68
+ setLoading(false);
69
+ }
70
+ }, debounceMs);
71
+
72
+ return () => clearTimeout(timer);
73
+ }, [query, selectedAddress, debounceMs]);
74
+
75
+ return {
76
+ suggestions,
77
+ loading,
78
+ error,
79
+ responseTime
80
+ };
81
+ };
82
+
83
+ export default useSmartyUSLookup;
package/src/index.js ADDED
@@ -0,0 +1,2 @@
1
+ export { default as AddressLookup } from './AddressLookup.jsx';
2
+ export { default } from './AddressLookup.jsx';