Skip to content

Commit a07df7a

Browse files
committed
Building weather forecast component
1 parent d093309 commit a07df7a

File tree

3 files changed

+154
-0
lines changed

3 files changed

+154
-0
lines changed

src/App.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import './App.css';
44
import CurrentWeather from './components/current-weather/CurrentWeather';
55
import Search from './components/search/Search';
66
import { WEATHER_API_URL, WEATHER_API_KEY } from './api';
7+
import Forecast from './components/forecast/Forecast';
78

89
function App() {
910
const [currentWeather, setCurrrentWeather] = useState(null);
@@ -39,6 +40,7 @@ function App() {
3940
<div className="container">
4041
<Search onSearchChange={handleOnSearchChange} />
4142
{currentWeather && <CurrentWeather data={currentWeather} />}
43+
{forecast && <Forecast data={forecast} />}
4244
</div>
4345
);
4446
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import './forecast.css';
2+
3+
import {
4+
Accordion,
5+
AccordionItem,
6+
AccordionItemButton,
7+
AccordionItemHeading,
8+
AccordionItemPanel,
9+
} from 'react-accessible-accordion';
10+
11+
const WEEK_DAYS = [
12+
'Monday',
13+
'Tuesday',
14+
'Wednesday',
15+
'Thursday',
16+
'Friday',
17+
'Saturday',
18+
'Sunday',
19+
];
20+
21+
const Forecast = ({ data }) => {
22+
const dayInAWeek = new Date().getDay();
23+
const forecastDays = WEEK_DAYS.slice(dayInAWeek, WEEK_DAYS.length).concat(
24+
WEEK_DAYS.slice(0, dayInAWeek)
25+
);
26+
27+
return (
28+
<>
29+
<label className="title">Daily</label>
30+
<Accordion allowZeroExpanded>
31+
{data.list.splice(0, 7).map((item, idx) => (
32+
<AccordionItem key={idx}>
33+
<AccordionItemHeading>
34+
<AccordionItemButton>
35+
<div className="daily-item">
36+
<img
37+
className="icon-small"
38+
src={`icons/${item.weather[0].icon}.png`}
39+
alt="weather"
40+
/>
41+
<label className="day">{forecastDays[idx]}</label>
42+
<label className="description">
43+
{item.weather[0].description}
44+
</label>
45+
<label className="min-max">
46+
{Math.round(item.main.temp_min)}°C /{' '}
47+
{Math.round(item.main.temp_max)}°C
48+
</label>
49+
</div>
50+
</AccordionItemButton>
51+
</AccordionItemHeading>
52+
<AccordionItemPanel>
53+
<div className="daily-details-grid">
54+
<div className="daily-details-grid-item">
55+
<label>Pressure</label>
56+
<label>{item.main.pressure} hPa</label>
57+
</div>
58+
<div className="daily-details-grid-item">
59+
<label>Humidity</label>
60+
<label>{item.main.humidity}%</label>
61+
</div>
62+
<div className="daily-details-grid-item">
63+
<label>Clouds</label>
64+
<label>{item.clouds.all}%</label>
65+
</div>
66+
<div className="daily-details-grid-item">
67+
<label>Wind speed:</label>
68+
<label>{item.wind.speed} m/s</label>
69+
</div>
70+
<div className="daily-details-grid-item">
71+
<label>Sea level:</label>
72+
<label>{item.main.sea_level}m</label>
73+
</div>
74+
<div className="daily-details-grid-item">
75+
<label>Feels like:</label>
76+
<label>{Math.round(item.main.feels_like)}°C</label>
77+
</div>
78+
</div>
79+
</AccordionItemPanel>
80+
</AccordionItem>
81+
))}
82+
</Accordion>
83+
</>
84+
);
85+
};
86+
export default Forecast;
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
.title {
2+
font-size: 23px;
3+
font-weight: 700;
4+
}
5+
6+
.daily-item {
7+
background-color: #f5f5f5;
8+
border-radius: 15px;
9+
height: 40px;
10+
margin: 5px;
11+
align-items: center;
12+
cursor: pointer;
13+
display: flex;
14+
font-size: 14px;
15+
padding: 5px 20px;
16+
}
17+
18+
.icon-small {
19+
width: 40px;
20+
}
21+
22+
.daily-item .day {
23+
cursor: inherit;
24+
color: #212121;
25+
flex: 1 1;
26+
font-weight: 600;
27+
margin-left: 15px;
28+
}
29+
30+
.description {
31+
cursor: inherit;
32+
flex: 1 1;
33+
margin-right: 15px;
34+
text-align: right;
35+
}
36+
37+
.min-max {
38+
color: #757575;
39+
}
40+
41+
.daily-details-grid {
42+
grid-row-gap: 0;
43+
grid-column-gap: 15px;
44+
-webkit-column-gap: 15px;
45+
column-gap: 15px;
46+
display: grid;
47+
flex: 1 1;
48+
grid-template-columns: auto auto;
49+
padding: 5px 15px;
50+
row-gap: 0;
51+
}
52+
53+
.daily-details-grid-item {
54+
align-items: center;
55+
display: flex;
56+
height: 30px;
57+
justify-content: space-between;
58+
}
59+
60+
.daily-details-grid-item label:first-child {
61+
color: #757575;
62+
}
63+
64+
.daily-details-grid-item label:last-child {
65+
color: #212121;
66+
}

0 commit comments

Comments
 (0)