Skip to content
Open
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: 7 additions & 7 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,25 @@ class App extends React.Component {
e.preventDefault();
const city = e.target.elements.city.value;
const country = e.target.elements.country.value;
const api_call = await fetch(`http://api.openweathermap.org/data/2.5/weather?q=${city},${country}&appid=${API_KEY}&units=metric`);
const data = await api_call.json();
if (city && country) {
// This takes care of whenever a user mistypes those query words, which api can't handle
try {
const api_call = await fetch(`http://api.openweathermap.org/data/2.5/weather?q=${city},${country}&appid=${API_KEY}&units=metric`);
const data = await api_call.json();
this.setState({
temperature: data.main.temp,
city: data.name,
country: data.sys.country,
humidity: data.main.humidity,
description: data.weather[0].description,
error: ""
});
} else {
} catch(err) {
this.setState({
temperature: undefined,
city: undefined,
country: undefined,
humidity: undefined,
description: undefined,
error: "Please enter the values."
error: "Please enter the values correctly."
});
}
}
Expand Down Expand Up @@ -71,4 +71,4 @@ class App extends React.Component {
}
};

export default App;
export default App;