-
Notifications
You must be signed in to change notification settings - Fork 85
redux-weather #79
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
base: master
Are you sure you want to change the base?
redux-weather #79
Changes from 5 commits
80dcff0
1cd6743
036a4bc
e0c8fa9
41147c0
111777f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,25 +1,30 @@ | ||
| import logo from './logo.svg'; | ||
| import './App.css'; | ||
| import React from 'react'; | ||
| import { Container } from 'react-bootstrap'; | ||
| import Row from 'react-bootstrap/Row'; | ||
| import Col from 'react-bootstrap/Col'; | ||
| import Header from '.components/header'; | ||
|
||
| import CitySearch from '.components/city-search'; | ||
| import CreateDisplay from '.components/weather-display'; | ||
| import 'bootstrap/dist/css/bootstrap.min.css'; | ||
|
|
||
| function App() { | ||
| return ( | ||
| <div className="App"> | ||
| <header className="App-header"> | ||
| <img src={logo} className="App-logo" alt="logo" /> | ||
| <p> | ||
| Edit <code>src/App.js</code> and save to reload. | ||
| </p> | ||
| <a | ||
| className="App-link" | ||
| href="https://reactjs.org" | ||
| target="_blank" | ||
| rel="noopener noreferrer" | ||
| > | ||
| Learn React | ||
| </a> | ||
| </header> | ||
| </div> | ||
| ); | ||
|
|
||
| <Container className='align-item-center'> | ||
| <Row className='ms-5'> | ||
| <Col> | ||
| <CitySearch /> | ||
| <Header /> | ||
| <CreateDisplay/> | ||
| </Col> | ||
| </Row> | ||
| </Container> | ||
| ) | ||
|
|
||
|
|
||
| } | ||
|
|
||
|
|
||
|
|
||
| export default App; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| import axios from "axios"; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. axios is missing from your package.json |
||
| export const FETCH_WEATHER = "FETCH_WEATHER"; | ||
| export const FETCH_AVERAGE = "FETCH_AVERAGE"; | ||
|
|
||
|
|
||
| export function fetchWeather (city) { | ||
|
|
||
| const search = axios.get(`api.openweathermap.org/data/2.5/forecast?q=${city}&appid=40a22fcb01995614a7b68804376359eb`) | ||
|
||
|
|
||
| return { | ||
| type:FETCH_WEATHER, | ||
| payload: search | ||
| }; | ||
| } | ||
|
|
||
| export function featherAvg (math) { | ||
|
|
||
| return { | ||
| type:FETCH_AVERAGE, | ||
| payload: { math | ||
| } | ||
| } | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| import { fetchWeather } from "../actions/actions"; | ||
| import { useState } from "react"; | ||
| import { useDispatch } from "react"; | ||
|
||
| import { Container } from 'react-bootstrap'; | ||
| import Row from 'react-bootstrap/Row'; | ||
| import Col from 'react-bootstrap/Col'; | ||
|
|
||
|
|
||
| const CitySearch = () => { | ||
| const [city, citySet] = useState(''); | ||
| const dispatch = useDispatch(''); | ||
|
|
||
| const handleWeatherDisplay = () => { | ||
| dispatch(fetchWeather(city)) | ||
| } | ||
|
|
||
| return ( | ||
| <Container> | ||
| <Row> | ||
| <Col> | ||
| <input className="form-control" type="text" placeholder="City" aria-label="City" value={city} onChange={(e) => citySet(e.target.value)}/> | ||
| <button type="button" onClick={handleWeatherDisplay} class="btn btn-primary">Submit</button> | ||
| </Col> | ||
| </Row> | ||
| </Container> | ||
| ) | ||
|
|
||
| } | ||
|
|
||
|
|
||
| export default CitySearch; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| import React from 'react' | ||
| import { Container } from 'react-bootstrap'; | ||
| import Row from 'react-bootstrap/Row'; | ||
| import Col from 'react-bootstrap/Col'; | ||
|
|
||
|
|
||
|
|
||
| const Header = () => { | ||
| return ( | ||
| <Container className='table table-bordered align-items-center'> | ||
| <Row className='my-5 mx-5 '> | ||
| <Col><h5 style={{textAlign: "center"}}>City</h5></Col> | ||
| <Col><h5 style={{textAlign: "center"}}>Temperature(F)</h5></Col> | ||
| <Col><h5 style={{textAlign: "center"}}>Pressure(hPa)</h5></Col> | ||
| <Col><h5 style={{textAlign: "center"}}>Humidity(%)</h5></Col> | ||
| </Row> | ||
| </Container> | ||
|
|
||
| ) | ||
| } | ||
|
|
||
| export default Header; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| import { useSelector } from "react-redux"; | ||
| import { Sparklines, SparklinesLine, SparklinesReferenceLine } from 'react-sparklines'; | ||
| import Container from 'react-bootstrap/Container'; | ||
| import Row from 'react-bootstrap/Row'; | ||
| import Col from 'react-bootstrap/Col'; | ||
|
|
||
|
|
||
|
|
||
| const weatherDisplay = () => { | ||
| // eslint-disable-next-line react-hooks/rules-of-hooks | ||
| let weather = useSelector((state) => state.weather); | ||
|
|
||
|
|
||
| const handleAvg = (data, measurement) => { | ||
| const average = array => array.reduce((a, b) => a+b) / array.length | ||
| let correct = 0; | ||
|
|
||
| if(measurement === 'temperature') { | ||
| correct = Math.round(average(data)) | ||
| return <h6>{correct}°F</h6> | ||
| } else if (measurement === 'pressure') { | ||
| correct = Math.round(average(data)) | ||
| return <h6>{correct} <small>hPa</small></h6> | ||
| } else if (measurement === 'humidity') { | ||
| correct = Math.round(average(data)) | ||
| return <h6>{correct}%</h6> | ||
|
|
||
| }; | ||
| } | ||
|
|
||
| const handleWeatherDisplay = () => { | ||
| console.log(weather[0]) | ||
| let data = []; | ||
| let cityName = ''; | ||
| if(weather.length >= 1) { | ||
| weather.map((city, index) => { | ||
| let cityInfo = []; | ||
| for (const key in city) { | ||
| if(key === 'name') { | ||
| cityName = <Col key={index.toString()} style={{textAlign: 'center'}}>{city[key]}</Col> | ||
| } else { | ||
| cityInfo.push( | ||
| <Col key={city.id}> | ||
| <Sparklines data={city[key]}> | ||
| <SparklinesLine /> | ||
| <SparklinesReferenceLine type="avg" /> | ||
| </Sparklines> | ||
| <div key={city.toString()} style={{textAlign: 'center'}}>{handleAvg(city[key], key)}</div> | ||
| </Col> | ||
| ) | ||
| } | ||
| } | ||
| data.push(<Row className="align-items-center mt-4 ms-4">{cityName}{cityInfo}</Row>) | ||
| }) | ||
| }; | ||
| return data; | ||
| } | ||
|
|
||
| return ( | ||
| <Container> | ||
| {handleWeatherDisplay()} | ||
| </Container> | ||
| ); | ||
| } | ||
|
|
||
| export default weatherDisplay; | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| import { FETCH_WEATHER} from "../actions/actions"; | ||
|
|
||
| const cityReducer = function (state = [], action) { | ||
| // eslint-disable-next-line default-case | ||
| switch(action.type) { | ||
| case FETCH_WEATHER: | ||
| let temperature = []; | ||
| let humidity = []; | ||
| let pressure = []; | ||
| let cityName = ''; | ||
| action.payload.data.list.map((section, index) => { | ||
| temperature.push(section.main.temp) | ||
| humidity.pudh(section.main.humidity) | ||
|
||
| pressure.push(section.main.pressure) | ||
| cityName = action.payload.data.cityName | ||
| }); | ||
|
|
||
| temperature = temperature.map((day) => Math.round(((day) - 273 * (9/5) + 32))) | ||
| humidity = humidity.map((day) => Math.round(((day))) ) | ||
| pressure = pressure.map((day) => Math.round(((day))) ) | ||
|
|
||
| return [{ | ||
| cityName, temperature, humidity, pressure | ||
|
|
||
| }, state] | ||
|
|
||
| default: | ||
| return state; | ||
| }; | ||
| }; | ||
|
|
||
| export default cityReducer; | ||
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.
lots of packages missing