Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handling Forms/Problem Set: Forms and APIs wave 1 (search) solution #2

Open
wants to merge 2 commits into
base: search-solution
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 43 additions & 2 deletions src/App.jsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,49 @@
import { useState } from 'react';
import axios from 'axios';
import SearchForm from './components/SearchForm';
import SearchResult from './components/SearchResult';
import './App.css';

const API_KEY = import.meta.env.VITE_API_KEY;
const BASE_URL = import.meta.env.VITE_BASE_URL;
const SEARCH_URL = 'search.php';

function App() {
const [result, setResult] = useState(null);

const performSearchAsync = (location) => {
return axios.get(
`${BASE_URL}${SEARCH_URL}`, { params:
{
key: API_KEY,
q: location,
format: 'json',
}})
.then(response => {
const { lat, lon } = response.data[0];
setResult({
location,
latitude: Number(lat),
longitude: Number(lon),
});
});
};

const locationSubmitted = (location) => {
performSearchAsync(location);
};

return null;
return (
<div className="App">
<header className="App-header">
<h1>Get Latitude and Longitude</h1>
<SearchForm onLocationSubmit={locationSubmitted} />
</header>
<main>
<SearchResult result={result} />
</main>
</div>
);
}

export default App;
export default App;
Empty file added src/components/SearchForm.css
Empty file.
39 changes: 39 additions & 0 deletions src/components/SearchForm.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { useState } from 'react';
import PropTypes from 'prop-types';
import './SearchForm.css';

const DEFAULT_STATE = {
location: '',
};

const SearchForm = (props) => {
const [formValues, setFormValues] = useState(DEFAULT_STATE);

const textInput = (e) => {
const fieldName = e.target.name;
const value = e.target.value;
setFormValues(current => {
return { ...current, [fieldName]: value };
});
};

const formSubmitted = (event) => {
event.preventDefault();
props.onLocationSubmit(formValues.location);
};

return (
<div className="SearchForm">
<form onSubmit={formSubmitted}>
<input type="text" name="location" value={formValues.location} onInput={textInput} />
<input type="submit" value="Search Now!" />
</form>
</div>
);
};

SearchForm.propTypes = {
onLocationSubmit: PropTypes.func.isRequired,
};

export default SearchForm;
Empty file added src/components/SearchResult.css
Empty file.
26 changes: 26 additions & 0 deletions src/components/SearchResult.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import PropTypes from 'prop-types';
import './SearchResult.css';

const SearchResult = (props) => {
const result = props.result;

return (
<div className="SearchResult">
<h2>Results for: { result && result.location }</h2>
<ul>
<li>Latitude: { result && result.latitude }</li>
<li>Longitude: { result && result.longitude }</li>
</ul>
</div>
);
};

SearchResult.propTypes = {
result: PropTypes.shape({
location: PropTypes.string.isRequired,
latitude: PropTypes.number.isRequired,
longitude: PropTypes.number.isRequired,
}),
};

export default SearchResult;