Skip to content

Commit

Permalink
split data and controls sections. added icon for weather condition
Browse files Browse the repository at this point in the history
  • Loading branch information
eduardomcv committed Jul 20, 2020
1 parent 9b72afd commit 2ad5005
Show file tree
Hide file tree
Showing 8 changed files with 145 additions and 49 deletions.
6 changes: 6 additions & 0 deletions src/components/ControlsSection/ControlsSection.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.controls-section {
display: flex;
justify-content: space-evenly;
width: 100%;
margin-top: 2rem;
}
32 changes: 32 additions & 0 deletions src/components/ControlsSection/ControlsSection.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import React, { FunctionComponent } from 'react';
import LocationSelector from '../LocationSelector';
import TemperatureToggle from '../TemperatureToggle';
import { TemperatureUnit, Location } from '../../sharedTypes';
import './ControlsSection.css';

interface ControlsSectionProps {
selectedLocation: Location;
selectedTemperatureUnit: TemperatureUnit;
onTemperatureToggle: (unit: TemperatureUnit) => void;
onLocationChange: (location: Location) => void;
}

const ControlsSection: FunctionComponent<ControlsSectionProps> = ({
selectedLocation,
selectedTemperatureUnit,
onTemperatureToggle,
onLocationChange,
}) => (
<section className="controls-section">
<LocationSelector
value={selectedLocation}
onChange={onLocationChange}
/>
<TemperatureToggle
value={selectedTemperatureUnit}
onToggle={onTemperatureToggle}
/>
</section>
);

export default ControlsSection;
1 change: 1 addition & 0 deletions src/components/ControlsSection/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from './ControlsSection';
33 changes: 33 additions & 0 deletions src/components/DataSection/DataSection.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
.data-section {
display: flex;
flex-direction: column;
align-items: center;
margin-top: 2rem;
width: 100%;
color: white;
text-align: center;
font-size: 8rem;
font-weight: bold;
}

.data-section.placeholder {
font-size: 3rem;
}

.weather-icon {
margin-top: 2rem;
height: 8rem;
width: auto;
}

@media only screen and (max-width: 600px) {
.data-section {
font-size: 6rem;
}

.weather-icon {
margin-top: 1rem;
height: 6rem;
width: auto;
}
}
60 changes: 60 additions & 0 deletions src/components/DataSection/DataSection.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import React, { FunctionComponent } from 'react';
import { TemperatureUnit, Location } from '../../sharedTypes';
import useOpenWeatherQuery from '../../hooks/useOpenWeather';
import './DataSection.css';
import { OPEN_WEATHER_ICONS_ENDPOINT } from '../../config';

interface DataSectionProps {
selectedLocation: Location;
selectedTemperatureUnit: TemperatureUnit;
}

const DataSection: FunctionComponent<DataSectionProps> = ({
selectedLocation,
selectedTemperatureUnit,
}) => {
const {
data,
error,
loading,
} = useOpenWeatherQuery({
location: selectedLocation,
temperatureUnit: selectedTemperatureUnit,
});

if (loading) {
return (
<section className="data-section placeholder">
<p>Loading...</p>
</section>
);
}

if (error || !data) {
return (
<section className="data-section placeholder">
<p>Something went wrong...</p>
{error && <p>{error.message}</p>}
</section>
);
}

const { main, weather } = data;

const tempValue = main.temp.toFixed();
const tempSymbol = selectedTemperatureUnit === 'celsius' ? '°C' : '°F';
const weatherIcon = weather[0].icon; // first element is the "primary"

return (
<section className="data-section">
<span>{`${tempValue} ${tempSymbol}`}</span>
<img
className="weather-icon"
alt="weather-icon"
src={`${OPEN_WEATHER_ICONS_ENDPOINT}${weatherIcon}@2x.png`}
/>
</section>
);
};

export default DataSection;
1 change: 1 addition & 0 deletions src/components/DataSection/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from './DataSection';
18 changes: 0 additions & 18 deletions src/components/MainContent/MainContent.css
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,3 @@
max-width: 60rem;
margin: 0 auto; /* will center automatically */
}

.main-content > section {
width: 100%;
margin-top: 2rem;
}

.controls-section {
display: flex;
justify-content: space-evenly;
}

.data-display-section {
display: flex;
flex-direction: column;
align-items: center;
color: white;
text-align: center;
}
43 changes: 12 additions & 31 deletions src/components/MainContent/MainContent.tsx
Original file line number Diff line number Diff line change
@@ -1,44 +1,25 @@
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 ControlsSection from '../ControlsSection';
import DataSection from '../DataSection';
import './MainContent.css';

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

const {
data,
error,
loading,
} = useOpenWeatherQuery({ location, temperatureUnit });

return (
<main className="main-content">
<section className="controls-section">
<LocationSelector
value={location}
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>
<ControlsSection
selectedLocation={location}
selectedTemperatureUnit={temperatureUnit}
onLocationChange={setLocation}
onTemperatureToggle={setTemperatureUnit}
/>
<DataSection
selectedLocation={location}
selectedTemperatureUnit={temperatureUnit}
/>
</main>
);
};
Expand Down

0 comments on commit 2ad5005

Please sign in to comment.