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

create button and add the style #22

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
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
2 changes: 2 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
REACT_APP_SITE_TITLE="CYF Weather App"
REACT_APP_API_KEY="f1423eabd2681d82efee065160102eae"
28,720 changes: 28,720 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"@testing-library/jest-dom": "^5.16.5",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",
"axios": "^1.3.4",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-scripts": "5.0.1",
Expand Down
29 changes: 26 additions & 3 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,40 @@
import "./App.scss";
import Header from "./components/Header/Header";
import Search from "./components/Search/Search";
import Footer from "./components/Footer/Footer";
import CurrentWeather from "./components/CurrentWeather/CurrentWeather";
import FutureWeather from "./components/FutureWeather/FutureWeather";
// import WeatherInfo from "./weatherInfo.json"

import { useState } from "react";

//configs
const siteTitle = process.env.REACT_APP_SITE_TITLE ?? "CYF Weather";

function App() {

// A state variable to hold weather Array
const [weatherInfo, setWeatherInfo] = useState([]) //Initialized as an Empty Array
console.log('This is Weather Info: ', weatherInfo);
if(weatherInfo.length > 0){
let infWeather = weatherInfo[0].weather[0].id
}
// <sup>o</sup>
return (
<div className="App">
<Header title={siteTitle} />
<Search title={siteTitle} setWeatherInfo={setWeatherInfo} />
<div className="weather-container "
// {
// true ? style={{backgroundColor: "This"}}:
// }
>
<CurrentWeather weatherInfo={weatherInfo} />
<FutureWeather weatherInfo={weatherInfo} />
Comment on lines +30 to +31
Copy link

Choose a reason for hiding this comment

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

Do you need to pass ALL the data to both these components? It's always best to pass down just what a component needs.
In this case: CurrentWeather only needs one item from the array of weather in the API response. And FutureWeather needs 7 items (according to the design which is our source of truth here), so you need to make a small array of 7 items here, and pass down that small array to FutureWeather instead of the whole thing

</div>
{/* <button> FIND WEATHER</button> */}
{/*
<main className="c-site-main" tabIndex="0">
</main>
<Footer />
<Footer /> */}
</div>
);
}
Expand Down
23 changes: 23 additions & 0 deletions src/App.scss
Original file line number Diff line number Diff line change
@@ -1,2 +1,25 @@
@use "theme/utilities.scss";
@use "theme/global.scss";


:root {
--theme-color--paper: #9CCEF4 ;
--theme-color--ink: #141A5B;
--theme-color--highlight: #fff ;
--theme-color--block: #759EDA;
--theme-color--button: #5879C7;
//speculative / guessed weather colours please update values
--weather--storm: #888;
--weather--drizzle: #efefef;
--weather--rain: #555;
--weather--snow: #bbb;
--weather--fog: #333;
--weather--clear: #3257ea;
--weather--partlycloudy: #4259b8;
--weather--mostlycloudy: #888;
}


.weather-container {
background-color: var(--theme-color--paper);
}
30 changes: 30 additions & 0 deletions src/WeatherImages/WeatherImages.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import React from 'react'
import WeatherImg from "../img/weather-icons/clear.svg"
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 WeatherImages = ({infWeather}) => {
return (
<>
{
infWeather < 300 ? <img src={Storm} />
: infWeather >= 300 && infWeather <= 499 ? <img
src={Drizzle} /> : infWeather >= 500 && infWeather <= 599 ? <img
src={Rain} /> : infWeather >= 600 && infWeather <= 699 ? <img
src={Snow} /> : infWeather >= 700 && infWeather <= 799 ? <img
src={Fog} /> : infWeather === 800 ? <img
src={Clear} /> : infWeather === 801 ? <img
src={PartlyCloudy} /> : infWeather >= 802 && infWeather <= 805 ? <img
src={MostlyCloudy} /> :
<p>No Image Found</p>
}
Copy link

Choose a reason for hiding this comment

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

Good logic here. As a side note on best practices, when there is a complex conditional statement like this one, it's best to move it outside the component's JSX because it makes it hard to read. You could move this to a function (inside the component but before the return) like for example:

const selectIconById = (id) => {
  // move your logic here and return either the icon name or 
  // the whole <img /> element with the correct icon in the src
}

</>
)
}

export default WeatherImages
43 changes: 43 additions & 0 deletions src/components/CurrentWeather/CurrentWeather.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import React from 'react'
import "./CurrentWeather.scss"
// Importing Image
import WeatherImages from '../../WeatherImages/WeatherImages'

const CurrentWeather = ({weatherInfo}) => {

if(weatherInfo.length > 0){

const infWeather = weatherInfo[0].weather[0].id
Comment on lines +6 to +10
Copy link

Choose a reason for hiding this comment

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

As mentioned in my comment in App.js, this component should only receive weatherInfo[0] here, so then you will be able to extract what you need from that object directly in here


// const WeatherIcons = (infWeather)=>{
// return (
// infWeather < 300 ? <img src={Storm} />
// : infWeather >= 300 && infWeather <= 499 ? <img
// src={Drizzle} /> : infWeather >= 500 && infWeather <= 599 ? <img
// src={Rain} /> : infWeather >= 600 && infWeather <= 699 ? <img
// src={Snow} /> : infWeather >= 700 && infWeather <= 799 ? <img
// src={Fog} /> : infWeather === 800 ? <img
// src={Clear} /> : infWeather === 801 ? <img
// src={PartlyCloudy} /> : infWeather >= 802 && infWeather <= 805 ? <img
// src={MostlyCloudy} /> :
// <p>No Image Found</p>
// )}
Comment on lines +12 to +24
Copy link

Choose a reason for hiding this comment

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

It's best not to commit commented out code like this. I would delete it.

Especially because your WeatherImage component is fulfilling this function on line 29 :)

return (

<div className='current-weather-container'>
<div className='weather-details'>
<WeatherImages infWeather={infWeather}/>

<h2>{weatherInfo[0].weather[0].description}</h2>
<h3>Temperature {weatherInfo[0].main.temp_min} to {weatherInfo[0].main.temp_max} C</h3>
<p>Humidity {weatherInfo[0].main.humidity} Pressure {weatherInfo[0].main.pressure}</p>
</div>
</div>


)} else {
return (<p>Not data found</p>)
}
}

export default CurrentWeather
36 changes: 36 additions & 0 deletions src/components/CurrentWeather/CurrentWeather.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@


.current-weather-container {
display: flex;
justify-content: center;

.weather-details {
margin: 10px 0;
justify-content: center;
text-align: center;
}

h2 {
color: white;
text-align: center;
font-size: 1.5rem;
font-family: Raleway;

}
h3{

text-align: center;
color: var(--theme-color-ink);
}
p{
color: var(--theme-color-ink);
text-align: center;
font-size: 1.5rem;
}


img {
width: 200px;
}
}

33 changes: 33 additions & 0 deletions src/components/FutureWeather/FutureWeather.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import React from 'react'
import "./FutureWeather.scss"

import SingleFutureWeather from './SingleFutureWeather'


const FutureWeather = (props) => {
const {weatherInfo} = props

console.log('Future Weather Info:', weatherInfo);
// const newArr = weatherInfo.slice(0, 7)
// console.log(newArr);

// Create a Child Comp, WeatherCard. Pass Item as a prop, and sdisplay there
return (
<div className='FutureWeather'>
<p>FutureWeather</p>
<div className="weather-container" style={{display: "flex", justifyContent:"center"}}>
{
weatherInfo.length > 0 ?
weatherInfo.slice(1, 7).map((item) => {
Copy link

Choose a reason for hiding this comment

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

Good that you're slicing the data here 👍🏼
As I said in my comment in App.js, you could be slicing your data up there in the parent, and only passing down the sub-array to FutureWeather

console.log(item);
return (
<SingleFutureWeather item={item}/>
)
}) : <p>No Data</p>
}
</div>
</div>
)
}

export default FutureWeather
7 changes: 7 additions & 0 deletions src/components/FutureWeather/FutureWeather.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.FutureWeather {
background-color: var(--theme-color--paper);
margin-bottom: 10rem;
text-align: center;
margin-top: 2rem;
padding-bottom: 1rem;
}
15 changes: 15 additions & 0 deletions src/components/FutureWeather/SingleFutureWeather.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import React from 'react'
import WeatherImages from '../../WeatherImages/WeatherImages'

const SingleFutureWeather = ({item}) => {
let infWeather = item.weather[0].id
return (
<div>
<p>{item.dt_txt}</p>
<WeatherImages infWeather={infWeather}/>
<p>{(item.main.temp).toFixed(2)} C</p>
</div>
)
}

export default SingleFutureWeather
10 changes: 0 additions & 10 deletions src/components/Header/Header.jsx

This file was deleted.

1 change: 0 additions & 1 deletion src/components/Header/Header.scss

This file was deleted.

57 changes: 57 additions & 0 deletions src/components/Search/Search.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import React, {useState} from "react";
import axios from "axios"
import './Search.scss'

const Search = ({title, setWeatherInfo}) => {
const [city, setCity] = useState("")

const API_KEY = process.env.REACT_APP_API_KEY
console.log('API Key:', API_KEY);
const API_URL = `http://api.openweathermap.org/data/2.5/forecast?q=${city}&cnt=8&units=metric&appid=${API_KEY}`
const handleSubmit = (e) => {
e.preventDefault()

//API Request using Axios
getWeatherInfo()
console.log(city);
}

// This function sends an API Request via Axios
const getWeatherInfo = async () => {
// axios request
// try {

// }
// catch(err){

// }
Comment on lines +21 to +27
Copy link

Choose a reason for hiding this comment

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

Avoid committing comments like this, unless they are particularly useful to the other developers working on the project with you.

try {


const resp = await axios.get(API_URL)
console.log('Response:', resp);
setWeatherInfo(resp.data.list)
}
catch(err){
console.log('We have An Error');
console.log(err.message);
setWeatherInfo([])
alert("Something went wrong!")
}
}

return (
<header className="c-site-header">
<h1 className="c-site-header__title">{title}</h1>
<form onSubmit={(e) => handleSubmit(e)}>
{/* */}
<input type="text" value={city} onChange={(e) => setCity(e.target.value)} />
<button>Search</button>
</form>

</header>
)


}
export default Search;
8 changes: 8 additions & 0 deletions src/components/Search/Search.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
@use "../../theme/utilities.scss";


.c-site-header {
background-color: var(--theme-color--block);
}


8 changes: 8 additions & 0 deletions src/theme/button.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

button {
appearance: none;
background-color: var(--theme-color--button);
color: var(--theme-color--ink);
padding: var(--theme-spacing--2);
text-transform: uppercase;
Comment on lines +3 to +7
Copy link

Choose a reason for hiding this comment

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

Try and make this button looks as much like the design provided as possible. Replicating designs into CSS is a key front-end developer skill.

}
1 change: 1 addition & 0 deletions src/theme/global.scss
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@
//global html styles
@import "./base.scss";
@import "./invisible.scss";
@import "./button.scss";
Loading