forked from CodeYourFuture/cyf-weather
-
Notifications
You must be signed in to change notification settings - Fork 12
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
zkhing
wants to merge
20
commits into
CodeYourFuture:main
Choose a base branch
from
zkhing:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 7 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
08fa292
Adding weather image
zkhing dac08f8
Fetching API
zkhing 87d8e92
Button onclick
zkhing a18dd57
Setting temp
zkhing be36012
Fetch temp from API
zkhing 85d523f
Matching up the weather icon images
zkhing ebb724f
Merge branch 'main' into main
LucyMac accfe59
Future weather and refactor code
zkhing 544ee29
Merge branch 'main' of https://github.com/zkhing/RainyPlaytime into f…
zkhing 2cbd25e
Save to latest changes
zkhing 8b2d947
Merge branch 'main' into main
LucyMac fa398df
Try to fetch current weather
zkhing dc4e844
Merge branch 'main' of https://github.com/zkhing/RainyPlaytime into f…
zkhing f47a206
Current weather working
zkhing 925f7de
Delete FutureWeather in App.js
zkhing 7c3b35c
Move weather icon inside CurrentWeather
zkhing 050e635
Make future weather items in column and center
zkhing 4a6f098
Make image-icons bigger
zkhing b458d4b
Make future-icons center
zkhing 42f58a9
Indent code and complete the project
zkhing File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,61 @@ | ||
@use "theme/utilities.scss"; | ||
@use "theme/global.scss"; | ||
|
||
|
||
.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; | ||
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,59 @@ | ||
import React from "react"; | ||
import './Header.scss' | ||
import "./Header.scss"; | ||
import WeatherIcon from "../Picture/WeatherIcon"; | ||
import React, { useState} from "react"; | ||
|
||
const Header = ({title}) => | ||
|
||
const Header = ({ title }) => { | ||
const [weatherData, setWeatherData] = useState([]); | ||
const [city, setCity] = useState(""); | ||
|
||
|
||
function getNewWeather (){ | ||
fetch( | ||
`https://api.openweathermap.org/data/2.5/weather?q=${city}&units=metric&appid=3b86046cce0de3be7cfa8369f4540b37` | ||
) | ||
.then((res) => res.json()) | ||
.then((data) => { | ||
setWeatherData(data); | ||
}) | ||
} | ||
|
||
return ( | ||
<header className="c-site-header"> | ||
<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> | ||
|
||
<WeatherIcon weatherId={weatherData?.weather?.[0]?.id} /> | ||
<div className="description"> | ||
<h3>{weatherData?.weather?.[0]?.description}</h3> | ||
</div> | ||
|
||
<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; | ||
|
||
|
||
export default Header; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
const WeatherIcon = ({weatherId}) =>{ | ||
|
||
const image = selectImage(weatherId) | ||
|
||
return ( | ||
<div className="weather-img"> | ||
<img src={image.src} alt={image.alt} /> | ||
</div> | ||
); | ||
} | ||
|
||
export default WeatherIcon; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 calledunknown.svg
so you could show this as your final 'else' condition.There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.