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

Connect to Weather API #28

Open
wants to merge 20 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 2 additions & 2 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ function App() {
<Header title={siteTitle} />

<main className="c-site-main" tabIndex="0">
<div>
{/* <div>
<img
className="weather-img"
src="https://cdn2.iconfinder.com/data/icons/weather-flat-14/64/weather02-512.png"
alt="Current Weather"
/>
</div>
</div> */}
Copy link

Choose a reason for hiding this comment

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

You can delete this

Copy link
Author

Choose a reason for hiding this comment

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

Hi Lucy, sorry for late reply! yes, I have deleted it.



{/* {FakeWeatherData.list.map((data) => (
Expand Down
53 changes: 50 additions & 3 deletions src/App.scss
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,60 @@
@use "theme/global.scss";


.c-site-main{
background-color: skyblue;
.App{
background-color: #8ecae6;
}

.c-site-header{
padding: 20px;
}

.heading{
background-color: #219ebc;
padding: 10px;
}

.c-site-header__title{
color: #023047;
padding: 20px;
}

.city{
margin: 15px;
padding: 15px;
}

.search-btn{
background-color: #023047;
color: white;
margin-left: 15px;
padding: 15px;
}

.weather-img{
margin: auto;
width: 200px;
height: 200px;
// max-width: 100%;
object-fit: cover;
}

.description{
display: flex;
justify-content: center;
color: white;
}

.temp {
display: flex;
justify-content: center;
margin: 20px;
}

.box{
display: flex;
justify-content: center;
}

.sub-box{
padding: 30px;
}
41 changes: 30 additions & 11 deletions src/components/Header/Header.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import "./Header.scss";
import WeatherIcon from "../Picture/WeatherIcon";
import React, { useState} from "react";


const Header = ({ title }) => {
const [weatherData, setWeatherData] = useState([]);
const [city, setCity] = useState("");
Expand All @@ -18,23 +20,40 @@ const Header = ({ title }) => {

return (
<header className="c-site-header">
<h1 className="c-site-header__title">{title}</h1>

<input
className="city"
type="text"
value={city}
onChange={(event) => setCity(event.target.value)}
/>
<div className="heading">
<h1 className="c-site-header__title">{title}</h1>
<input
className="city"
type="text"
placeholder="City"
value={city}
onChange={(event) => setCity(event.target.value)}
/>
<button className="search-btn" onClick={getNewWeather}>FIND WEATHER</button>
</div>

<button onClick={getNewWeather}>Search</button>
<WeatherIcon weatherId={weatherData?.weather?.[0]?.id} />
<div className="description">
<h3>{weatherData?.weather?.[0]?.description}</h3>
</div>

<div>
<h2>Temp: {weatherData?.main?.temp?.toFixed()}°C</h2>
<div className="temp">
<h2>Temperature : {weatherData?.main?.temp?.toFixed()}°C</h2>
</div>

<div className="box">
<div className="sub-box">
<h4>Humidity : {weatherData?.main?.humidity}% </h4>
</div>

<div className="sub-box">
<h4>Pressure : {weatherData?.main?.pressure}</h4>
</div>
</div>
</header>
);
};

export default Header;


53 changes: 53 additions & 0 deletions src/components/Picture/WeatherIcon.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import React from "react";
import storm from "../../img/weather-icons/storm.svg";
import drizzle from "../../img/weather-icons/drizzle.svg";
import rain from "../../img/weather-icons/rain.svg";
import snow from "../../img/weather-icons/snow.svg";
import fog from "../../img/weather-icons/fog.svg";
import clear from "../../img/weather-icons/clear.svg";
import partlyCloudy from "../../img/weather-icons/partlycloudy.svg";
import mostlyCloudy from "../../img/weather-icons/mostlycloudy.svg";

const images = {
storm: { src: storm, alt: "storm" },
drizzle: { src: drizzle, alt: "drizzle" },
rain: { src: rain, alt: "rain" },
snow: { src: snow, alt: "snow" },
fog: { src: fog, alt: "fog" },
clear: { src: clear, alt: "clear" },
partlyCloudy: { src: partlyCloudy, alt: "partly cloudy" },
mostlyCloudy: { src: mostlyCloudy, alt: "mostly cloudy" },
};


function selectImage(weatherId){
if (weatherId < 300) {
return images.storm;
} else if (weatherId < 499) {
return images.drizzle;
} else if (weatherId < 599) {
return images.rain;
} else if (weatherId < 699) {
return images.snow;
} else if (weatherId < 799) {
return images.fog;
} else if (weatherId === 800) {
return images.clear;
} else if (weatherId === 801) {
return images.partlyCloudy;
}
return images.mostlyCloudy;
Copy link

Choose a reason for hiding this comment

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

Nice code here @zkhing 🙌🏼

mostlycloudy should really be shown between 802 and 805. There is also an icon called unknown.svg so you could show this as your final 'else' condition.

Copy link

Choose a reason for hiding this comment

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

Please make this small change, then I will approve this PR, and we can merge it in.

Copy link
Author

Choose a reason for hiding this comment

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

Thank you. Actually, my tech buddy kindly helped me with that part. :) And I added unknown.svg image now.

}

const WeatherIcon = ({weatherId}) =>{

const image = selectImage(weatherId)

return (
<div className="weather-img">
<img src={image.src} alt={image.alt} />
</div>
);
}

export default WeatherIcon;