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 refactoring wave solution #5

Open
wants to merge 2 commits into
base: refactored-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
39 changes: 18 additions & 21 deletions src/App.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useState } from 'react';
import axios from 'axios';
import LocationIQ from './api/LocationIQ';
import SearchForm from './components/SearchForm';
import SearchResult from './components/SearchResult';
import SearchError from './components/SearchError';
Expand All @@ -8,7 +8,6 @@ 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 [results, setResults] = useState([]);
Expand All @@ -24,27 +23,25 @@ function App() {

const performSearchAsync = (location) => {
clearError();
return axios.get(
`${BASE_URL}${SEARCH_URL}`, { params:
{
key: API_KEY,
q: location,
format: 'json',
}})
.then(response => {
const { lat, lon } = response.data[0];
addResult({
location,
latitude: Number(lat),
longitude: Number(lon),
});
})
.catch(error => {
const message = error.response.data.error;
setError(message);
});
const api = new LocationIQ(API_KEY, BASE_URL);

return api.getLatLonAsync(location)
.then(result => addResult(result))
.catch(error => setError(error.message));
};

// same method in async/await style
// const performSearchAsync = async (location) => {
// clearError();
// const api = new LocationIQ(API_KEY);
// try {
// const result = await api.getLatLonAsync(location);
// addResult(result);
// } catch (error) {
// setError(error.message);
// }
// };

Comment on lines +33 to +44

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think folks will really appreciate seeing these async/await notes!

const locationSubmitted = (location) => {
performSearchAsync(location);
};
Expand Down
51 changes: 51 additions & 0 deletions src/api/LocationIQ.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import axios from 'axios';

const US_BASE_URL = "https://us1.locationiq.com/v1/";
const SEARCH_URL = 'search.php';

class LocationIQ {

constructor(apiKey, baseUrl) {
this.apiKey = apiKey;
this.baseUrl = baseUrl || US_BASE_URL;
}

getLatLonAsync(location) {
return axios.get(
`${this.baseUrl}${SEARCH_URL}`, { params:
{
key: this.apiKey,
q: location,
format: 'json'
}})
.then(response => {
const { lat, lon } = response.data[0];
return { location, latitude: Number(lat), longitude: Number(lon) };
})
.catch(error => {
const message = error.response.data.error;
throw { message };
});
}

// same method in async/await style
// async getLatLonAsync(location) {
// try {
// const response = await axios.get(
// `${this.baseUrl}${SEARCH_URL}`, { params:
// {
// key: this.apiKey,
// q: location,
// format: 'json'
// }});

// const { lat: latitude, lon: longitude } = response.data[0];
// return { location, latitude, longitude };
// } catch (error) {
// const message = error.response.data.error;
// throw { message };
// }
// }
}

export default LocationIQ;
12 changes: 3 additions & 9 deletions src/components/History.jsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import PropTypes from 'prop-types';
import { ResultType } from '../types';
import './History.css';

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

const History = ({ entry }) => {
return (
<li className="History">
<h3>{ entry.location }</h3>
Expand All @@ -14,11 +12,7 @@ const History = (props) => {
};

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

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

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

const HistoryList = ({ entries }) => {
return (
<div className="HistoryList">
<h2>Search History</h2>
Expand All @@ -21,11 +20,7 @@ const HistoryList = (props) => {
};

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

export default HistoryList;
4 changes: 1 addition & 3 deletions src/components/SearchError.jsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import PropTypes from 'prop-types';
import './SearchError.css';

const SearchError = (props) => {
const error = props.error;

const SearchError = ({ error }) => {
if (!error) {
return null;
}
Expand Down
8 changes: 4 additions & 4 deletions src/components/SearchForm.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import PropTypes from 'prop-types';
import './SearchForm.css';

const DEFAULT_STATE = {
location: '',
};
location: '',
};

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

const textInput = (e) => {
Expand All @@ -19,7 +19,7 @@ const SearchForm = (props) => {

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

return (
Expand Down
18 changes: 6 additions & 12 deletions src/components/SearchResult.jsx
Original file line number Diff line number Diff line change
@@ -1,26 +1,20 @@
import PropTypes from 'prop-types';
import { ResultType } from '../types';
import './SearchResult.css';

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

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

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

export default SearchResult;
7 changes: 7 additions & 0 deletions src/types/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { shape, string, number } from 'prop-types';

export const ResultType = shape({
location: string.isRequired,
latitude: number.isRequired,
longitude: number.isRequired,
});