Skip to content

Commit

Permalink
added weather hook. added loading and error states handling in the ui
Browse files Browse the repository at this point in the history
  • Loading branch information
eduardomcv committed Jul 20, 2020
1 parent e854507 commit 663842d
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions src/components/MainContent/MainContent.tsx
Original file line number Diff line number Diff line change
@@ -1,30 +1,44 @@
import React, { FunctionComponent, useState, ChangeEventHandler } from 'react';
import React, { FunctionComponent, useState } from 'react';
import LocationSelector from '../LocationSelector';
import TemperatureToggle from '../TemperatureToggle';
import { TemperatureUnit, Location } from '../../sharedTypes';
import useOpenWeatherQuery from '../../hooks/useOpenWeather';
import './MainContent.css';

const MainContent: FunctionComponent = () => {
const [location, setLocation] = useState<Location>('lisbon');
const [temperatureUnit, setTemperatureUnit] = useState<TemperatureUnit>('celsius');

const handleLocationChange: ChangeEventHandler<HTMLSelectElement> = (event) => {
const value = event.target.value as Location;
setLocation(value);
};
const {
data,
error,
loading,
} = useOpenWeatherQuery({ location, temperatureUnit });

return (
<main className="main-content">
<section className="controls-section">
<LocationSelector
value={location}
onChange={handleLocationChange}
onChange={(event) => {
const value = event.target.value as Location;
setLocation(value);
}}
/>
<TemperatureToggle
value={temperatureUnit}
onToggle={setTemperatureUnit}
/>
</section>
<section className="data-display-section">
{loading && <p>loading...</p>}
{(error || !data) && (
<>
<p>Something went wrong...</p>
{error && <p>{error.message}</p>}
</>
)}
</section>
</main>
);
};
Expand Down

0 comments on commit 663842d

Please sign in to comment.