Skip to content

Commit

Permalink
Add retailer address geocoding
Browse files Browse the repository at this point in the history
  • Loading branch information
davewalker5 committed Nov 21, 2023
1 parent 5406bae commit fd754c8
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 13 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,17 +134,18 @@ MusicCatalogue.LookupTool --lookup "John Coltrane" "Blue Train" catalogue

### Configuration

- The UI uses a simple "config.json" file containing the base URL for the Music Catalogue web service, a Google Maps API key for retailer location maps and locale settings used by the UI:
- The UI uses a simple "config.json" file containing the base URL for the Music Catalogue web service and locale settings used by the UI and during address geocoding:

```json
{
"api": {
"baseUrl": "http://localhost:8098",
"mapsApiKey": ""
"baseUrl": "http://localhost:8098"
},
"region": {
"locale": "en-GB",
"currency": "GBP"
"currency": "GBP",
"geocodingLanguage": "en",
"geocodingRegion": "GB"
}
}
```
Expand Down Expand Up @@ -228,8 +229,7 @@ MusicCatalogue.LookupTool --lookup "John Coltrane" "Blue Train" catalogue

<img src="diagrams/retailer-editor.png" alt="Retailer Details" width="600">

- Currently, the latitude and longitude (required to display the map) must be derived using external resources and then entered manually
- The intention is to add postcode geocoding to the next release
- Clicking on the globe icon next to the postcode entry will geocode the current address and populate the latitude and longitude with the results

### Album Lookup

Expand Down
Binary file modified diagrams/retailer-editor.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
51 changes: 45 additions & 6 deletions src/music-catalogue-ui/components/retailerEditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import pages from "../helpers/navigation";
import { useState, useCallback } from "react";
import { apiUpdateRetailer } from "@/helpers/apiRetailers";
import FormInputField from "./formInputField";
import { geocodeAddress } from "@/helpers/geocoder";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { faGlobe } from "@fortawesome/free-solid-svg-icons";

const RetailerEditor = ({ retailer, navigate, logout }) => {
const [name, setName] = useState(retailer.name);
Expand All @@ -17,6 +20,31 @@ const RetailerEditor = ({ retailer, navigate, logout }) => {
const [webSite, setWebSite] = useState(retailer.webSite);
const [error, setError] = useState("");

// Callback to geocode the address
const geocode = useCallback(
async (e) => {
// Prevent the default action associated with the click event
e.preventDefault();

// Geocode the address
const location = await geocodeAddress(
address1,
address2,
town,
county,
postCode,
country
);

// If we have an address, set the latitude and longitude
if (location != null) {
setLatitude(location.lat);
setLongitude(location.lng);
}
},
[address1, address2, town, county, postCode, country]
);

/* Callback to save retailer details */
const saveRetailer = useCallback(
async (e) => {
Expand Down Expand Up @@ -133,12 +161,23 @@ const RetailerEditor = ({ retailer, navigate, logout }) => {
</div>
<div className="row align-items-start">
<div className="col">
<FormInputField
label="Postcode"
name="postCode"
value={postCode}
setValue={setPostCode}
/>
<div className="form-group mt-3">
<label className={styles.retailerEditorInputLabel}>
Postcode
</label>
<FontAwesomeIcon
icon={faGlobe}
className={styles.retailerEditorGeocode}
onClick={(e) => geocode(e)}
/>
<input
className="form-control"
placeholder="Postcode"
name="postcode"
value={postCode}
onChange={(e) => setPostCode(e.target.value)}
/>
</div>
</div>
<div className="col">
<FormInputField
Expand Down
4 changes: 4 additions & 0 deletions src/music-catalogue-ui/components/retailerEditor.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,7 @@
font-weight: bold;
color: red;
}

.retailerEditorGeocode {
margin-left: 10px;
}
4 changes: 3 additions & 1 deletion src/music-catalogue-ui/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
},
"region": {
"locale": "en-GB",
"currency": "GBP"
"currency": "GBP",
"geocodingLanguage": "en",
"geocodingRegion": "GB"
}
}
43 changes: 43 additions & 0 deletions src/music-catalogue-ui/helpers/geocoder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import secrets from "@/helpers/secrets";
import config from "../config.json";
import { setDefaults, geocode, RequestType } from "react-geocode";
import { getStorageValue } from "./storage";

const geocodeAddress = async (
address1,
address2,
town,
county,
postcode,
country
) => {
// This sets default values for language and region for geocoding requests.
const apiKey = getStorageValue(secrets.mapsApiKey);
setDefaults({
key: apiKey,
language: config.region.geocodingLanguage,
region: config.region.geocodingRegion,
});

// Construct an address from its components
const address = [
address1,
address1,
address2,
town,
county,
postcode,
country,
].join(",");

try {
// Geocode the address
const response = await geocode(RequestType.ADDRESS, address);
const location = response.results[0].geometry.location;
return location;
} catch {
return null;
}
};

export { geocodeAddress };
6 changes: 6 additions & 0 deletions src/music-catalogue-ui/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/music-catalogue-ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"react-currency-input-field": "^3.6.12",
"react-datepicker": "^4.21.0",
"react-dom": "^18",
"react-geocode": "^1.0.0-alpha.1",
"react-select": "^5.8.0"
},
"devDependencies": {
Expand Down

0 comments on commit fd754c8

Please sign in to comment.