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 optional wave (history) solution #4

Open
wants to merge 2 commits into
base: history-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
14 changes: 12 additions & 2 deletions src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,23 @@ import axios from 'axios';
import SearchForm from './components/SearchForm';
import SearchResult from './components/SearchResult';
import SearchError from './components/SearchError';
import HistoryList from './components/HistoryList';
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 [results, setResults] = useState([]);
const [error, setError] = useState("");

const addResult = (result) => {
setResults(current => {
return [...current, result];
});
};

const clearError = () => { setError(""); };

const performSearchAsync = (location) => {
Expand All @@ -26,7 +33,7 @@ function App() {
}})
.then(response => {
const { lat, lon } = response.data[0];
setResult({
addResult({
location,
latitude: Number(lat),
longitude: Number(lon),
Expand All @@ -42,6 +49,8 @@ function App() {
performSearchAsync(location);
};

const result = results[results.length - 1];

return (
<div className="App">
<header className="App-header">
Expand All @@ -51,6 +60,7 @@ function App() {
<main>
<SearchResult result={result} />
<SearchError error={error} />
<HistoryList entries={results} />
</main>
</div>
);
Expand Down
9 changes: 9 additions & 0 deletions src/components/History.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.History {
display: block;
padding: 0;
margin: 0;
}

.History span {
margin-right: 1em;
}
24 changes: 24 additions & 0 deletions src/components/History.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import PropTypes from 'prop-types';
import './History.css';

const History = (props) => {
const entry = props.entry;

return (
<li className="History">
<h3>{ entry.location }</h3>
<span className="lat">Latitude: { entry.latitude }</span>
<span className="lon">Longitude: { entry.longitude }</span>
</li>
);
};

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

export default History;
3 changes: 3 additions & 0 deletions src/components/HistoryList.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.HistoryList ul {
padding: 0;
}
31 changes: 31 additions & 0 deletions src/components/HistoryList.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import PropTypes from 'prop-types';
import History from './History';
import './HistoryList.css';

const HistoryList = (props) => {
const entries = props.entries;

return (
<div className="HistoryList">
<h2>Search History</h2>
<ul>
{ entries.map((entry, i) => (
<History
key={i}
entry={entry}
/>
)) }
</ul>
</div>
);
};

HistoryList.propTypes = {
entries: PropTypes.arrayOf(PropTypes.shape({
location: PropTypes.string.isRequired,
latitude: PropTypes.number.isRequired,
longitude: PropTypes.number.isRequired,
})).isRequired,
};

export default HistoryList;