-
Notifications
You must be signed in to change notification settings - Fork 251
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
96 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import React, { useState } from "react"; | ||
import styles from "./styles.module.css"; | ||
|
||
interface PostcodeLookupProps { | ||
endpointTemplate: string; | ||
headingText: string; | ||
longPlaceholder: string; | ||
latPlaceholder: string; | ||
} | ||
|
||
const GetGeocode: React.FC<PostcodeLookupProps> = ({ | ||
endpointTemplate, | ||
headingText, | ||
longPlaceholder, | ||
latPlaceholder, | ||
}) => { | ||
const [longitude, setLongitude] = useState<string>(""); | ||
const [latitude, setLatitude] = useState<string>(""); | ||
const [apiResult, setApiResult] = useState<string>(""); | ||
const [hasSearched, setHasSearched] = useState(false); | ||
const [fullEndpoint, setFullEndpoint] = useState<string>(""); | ||
|
||
const handleLongitudeChange = (e: React.ChangeEvent<HTMLInputElement>) => { | ||
setLongitude(e.target.value); | ||
}; | ||
|
||
const handleLatitudeChange = (e: React.ChangeEvent<HTMLInputElement>) => { | ||
setLatitude(e.target.value); | ||
}; | ||
|
||
const fetchPostcodeData = async () => { | ||
const encodedLongitude = encodeURIComponent(longitude); | ||
const encodedLatitude = encodeURIComponent(latitude); | ||
const endpoint = `https://${endpointTemplate}?lon=${encodedLongitude}&lat=${encodedLatitude}`; | ||
setFullEndpoint(endpoint); | ||
try { | ||
const response = await fetch(endpoint); | ||
const data = await response.json(); | ||
setApiResult(JSON.stringify(data, null, 2)); | ||
setHasSearched(true); | ||
console.log(endpoint); | ||
} catch (error) { | ||
setHasSearched(true); | ||
} | ||
}; | ||
|
||
return ( | ||
<div className={styles.container}> | ||
<div className={styles.endpointContainer}> | ||
<h3 className={styles.endpointLabel}>{headingText}</h3> | ||
<div className={styles.requestContainer}> | ||
<span className={styles.httpMethod}>GET</span> | ||
<p className={styles.request}>{endpointTemplate}?lon=</p> | ||
<input | ||
className={styles.postcodeInput} | ||
type="text" | ||
value={longitude} | ||
onChange={handleLongitudeChange} | ||
placeholder={longPlaceholder} | ||
/> | ||
<p className={styles.request}>&lat=</p> | ||
<input | ||
className={styles.postcodeInput} | ||
type="text" | ||
value={latitude} | ||
onChange={handleLatitudeChange} | ||
placeholder={latPlaceholder} | ||
/> | ||
<button onClick={fetchPostcodeData}>Request</button> | ||
</div> | ||
{hasSearched && ( | ||
<div className={styles.result}> | ||
<pre>{apiResult}</pre> | ||
</div> | ||
)} | ||
</div> | ||
</div> | ||
); | ||
}; | ||
|
||
export default GetGeocode; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters