generated from chingu-voyages/voyage-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #38 from chingu-voyages/address-validation
Address validation
- Loading branch information
Showing
5 changed files
with
191 additions
and
24 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,115 @@ | ||
"use client"; | ||
|
||
import { useState, useEffect } from "react"; | ||
import { | ||
FormControl, | ||
FormHelperText, | ||
InputLabel, | ||
Input, | ||
Container, | ||
TextField, | ||
MenuItem, | ||
Stack, | ||
Button, | ||
Alert, | ||
} from "@mui/material"; | ||
import CheckIcon from "@mui/icons-material/Check"; | ||
|
||
export default function AddressInput() { | ||
const [addresses, setAddresses] = useState([]); | ||
const [providedAddress, setProvidedAddress] = useState({ | ||
hse_nbr: "", | ||
hse_dir: "", | ||
str_nm: "", | ||
str_sfx: "", | ||
zip_cd: "", | ||
}); | ||
const [isAddressFound, setIsAddressFound] = useState(false); | ||
|
||
const handleChange = (e) => { | ||
setProvidedAddress({ | ||
...providedAddress, | ||
[e.target.name]: e.target.value, | ||
}); | ||
}; | ||
|
||
async function fetchAddresses() { | ||
try { | ||
const response = await fetch( | ||
`https://data.lacity.org/resource/4ca8-mxuh.json?hse_nbr=${providedAddress.hse_nbr}&hse_dir_cd=${providedAddress.hse_dir}&str_nm=${providedAddress.str_nm}&str_sfx_cd=${providedAddress.str_sfx}&zip_cd=${providedAddress.zip_cd}` | ||
); | ||
const data = await response.json(); | ||
setAddresses(data); | ||
setIsAddressFound(true); | ||
} catch (error) { | ||
console.error("Error fetching addresses:", error); | ||
} | ||
} | ||
|
||
return ( | ||
<FormControl> | ||
<Container> | ||
<Stack direction="row" spacing={2} padding={0}> | ||
<TextField | ||
id="hse_nbr" | ||
name="hse_nbr" | ||
label="House Number" | ||
variant="outlined" | ||
value={providedAddress.hse_nbr} | ||
onChange={handleChange} | ||
/> | ||
<TextField | ||
id="hse_dir" | ||
name="hse_dir" | ||
select | ||
label="Direction" | ||
defaultValue="" | ||
value={providedAddress.hse_dir} | ||
helperText="if applicable" | ||
onChange={handleChange} | ||
> | ||
<MenuItem value=""></MenuItem> | ||
<MenuItem value="N">North</MenuItem> | ||
<MenuItem value="E">East</MenuItem> | ||
<MenuItem value="W">West</MenuItem> | ||
<MenuItem value="S">South</MenuItem> | ||
</TextField> | ||
<TextField | ||
id="str_nm" | ||
name="str_nm" | ||
label="Street Name" | ||
variant="outlined" | ||
value={providedAddress.str_nm} | ||
onChange={handleChange} | ||
/> | ||
<TextField | ||
id="str_sfx" | ||
name="str_sfx" | ||
label="Suffix" | ||
variant="outlined" | ||
value={providedAddress.str_sfx} | ||
onChange={handleChange} | ||
/> | ||
<TextField | ||
id="zip_cd" | ||
label="Zip Code" | ||
name="zip_cd" | ||
variant="outlined" | ||
value={providedAddress.zip_cd} | ||
onChange={handleChange} | ||
/> | ||
</Stack> | ||
<Button variant="contained" onClick={fetchAddresses}> | ||
Validate | ||
</Button> | ||
{isAddressFound ? ( | ||
<Alert icon={<CheckIcon fontSize="inherit" />} severity="success"> | ||
Address verified | ||
</Alert> | ||
) : ( | ||
<Alert severity="error">Address not found.</Alert> | ||
)} | ||
</Container> | ||
</FormControl> | ||
); | ||
} |
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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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