-
Notifications
You must be signed in to change notification settings - Fork 85
Redux Weather Submission #80
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?
Changes from all commits
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 @@ | ||
| import logo from './logo.svg'; | ||
| import './App.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> | ||
| ); | ||
| } | ||
|
|
||
| export default App; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| import axios from 'axios'; | ||
|
|
||
| export const GET_CITY = 'GET_CITY'; | ||
|
|
||
| const apiKey = '536b99823dd5364f3a29be3867644595'; | ||
|
|
||
| export function getCityWeather(value) { | ||
| const data = axios.get( | ||
| `https:/api.openweathermap.org/data/2.5/forecast?q=${value}&appid=${apiKey}&units=imperial` | ||
| ); | ||
|
|
||
| return { | ||
| type: GET_CITY, | ||
| payload: data, | ||
| }; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| /* eslint-disable no-param-reassign */ | ||
| import '../index.css'; | ||
|
|
||
| import { useState } from 'react'; | ||
| import { useDispatch, useSelector } from 'react-redux'; | ||
| import _ from 'lodash'; | ||
| import { | ||
| Sparklines, | ||
| SparklinesLine, | ||
| SparklinesReferenceLine, | ||
| } from 'react-sparklines'; | ||
| import { getCityWeather } from '../actions/actions-index'; | ||
|
|
||
| const SearchCity = () => { | ||
| const [city, setCity] = useState(''); | ||
| const weather = useSelector((state) => state); | ||
|
|
||
| const dispatch = useDispatch(); | ||
|
|
||
| const handleFormSubmit = () => { | ||
| dispatch(getCityWeather(city)); | ||
| setCity(''); | ||
| }; | ||
|
|
||
| function renderWeather() { | ||
|
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. Stay consistent with your functions (expressions vs declarations) |
||
| function renderTemp(temp) { | ||
| temp = temp.map((item) => item.main.temp); | ||
| return ( | ||
| <div> | ||
| <Sparklines data={temp}> | ||
| <SparklinesLine color="orange" /> | ||
| <SparklinesReferenceLine type="mean" /> | ||
| </Sparklines> | ||
| <div>{Math.floor(temp[0])} F</div> | ||
| </div> | ||
| ); | ||
| } | ||
|
|
||
| function renderPressure(pressure) { | ||
|
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. would be cleaner to move the render functionality to its own component |
||
| console.log(pressure); | ||
| pressure = pressure.map((item) => item.main.pressure); | ||
| return ( | ||
| <div> | ||
| <Sparklines data={pressure}> | ||
| <SparklinesLine color="green" /> | ||
| <SparklinesReferenceLine type="mean" /> | ||
| </Sparklines> | ||
| <div className="col-md-4">{pressure[0]} hPa</div> | ||
| </div> | ||
| ); | ||
| } | ||
|
|
||
| function renderHumidity(humidity) { | ||
| humidity = humidity.map((item) => item.main.humidity); | ||
| return ( | ||
| <div> | ||
| <Sparklines data={humidity}> | ||
| <SparklinesLine color="">{humidity}</SparklinesLine> | ||
| <SparklinesReferenceLine type="mean" /> | ||
| </Sparklines> | ||
| <div>{humidity[0]} %</div> | ||
| </div> | ||
| ); | ||
| } | ||
| if (!_.isEmpty(weather)) { | ||
|
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. nice use of lodash |
||
| return weather.map((data) => ( | ||
| <tr key={data.city.id}> | ||
| <td>{data.city.name}</td> | ||
| <td>{renderTemp(data.list)}</td> | ||
| <td>{renderPressure(data.list)}</td> | ||
| <td>{renderHumidity(data.list)}</td> | ||
| </tr> | ||
| )); | ||
| } | ||
| } | ||
|
|
||
| return ( | ||
| <div className="form-group"> | ||
| <form> | ||
| <div className="input-group mb-3"> | ||
| <input | ||
| type="text" | ||
| className="form-control" | ||
| placeholder="Get the Weather for a City" | ||
| onChange={(e) => setCity(e.target.value)} | ||
| value={city} | ||
| /> | ||
| <button | ||
| className="btn btn-outline btn-primary" | ||
| type="button" | ||
| onClick={handleFormSubmit} | ||
| > | ||
| Submit | ||
| </button> | ||
| </div> | ||
| </form> | ||
| <table className="table"> | ||
| <thead> | ||
| <tr> | ||
| <th>City</th> | ||
| <th>Temperature (F)</th> | ||
| <th>Pressure (hPa)</th> | ||
| <th>Humidity (%)</th> | ||
| </tr> | ||
| </thead> | ||
| <tbody>{renderWeather()}</tbody> | ||
| </table> | ||
| </div> | ||
| ); | ||
| }; | ||
|
|
||
| export default SearchCity; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| import '../index.css'; | ||
|
|
||
| const header = (props) => ( | ||
| <div className="custom"> | ||
| <div className="jumbotron text-center"> | ||
| <div className="container"> | ||
| <h1 className="jumbotron-heading">Search the weather for a city</h1> | ||
| </div> | ||
| </div> | ||
| <div className="container">{props.children}</div> | ||
| </div> | ||
| ); | ||
|
|
||
| export default header; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,17 +1,24 @@ | ||
| import 'bootstrap/dist/css/bootstrap.css'; | ||
|
|
||
| import React from 'react'; | ||
| import ReactDOM from 'react-dom'; | ||
| import { createStore, applyMiddleware } from 'redux'; | ||
| import { Provider } from 'react-redux'; | ||
|
|
||
| import promise from 'redux-promise'; | ||
| import './index.css'; | ||
| import App from './App'; | ||
| import reportWebVitals from './reportWebVitals'; | ||
|
|
||
| import Header from './components/header'; | ||
| import CitySearch from './components/city-search'; | ||
| import cityReducer from './reducers/reducer-cities'; | ||
|
|
||
| const store = applyMiddleware(promise)(createStore); | ||
|
|
||
| ReactDOM.render( | ||
| <React.StrictMode> | ||
| <App /> | ||
| </React.StrictMode>, | ||
| <Provider store={store(cityReducer)}> | ||
| <Header> | ||
| <CitySearch /> | ||
| </Header> | ||
| </Provider>, | ||
| document.getElementById('root') | ||
| ); | ||
|
|
||
| // If you want to start measuring performance in your app, pass a function | ||
| // to log results (for example: reportWebVitals(console.log)) | ||
| // or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals | ||
| reportWebVitals(); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| import { GET_CITY } from '../actions/actions-index'; | ||
|
|
||
| const initialState = []; | ||
|
|
||
| // eslint-disable-next-line default-param-last | ||
| const cityReducer = (state = initialState, action) => { | ||
| switch (action.type) { | ||
| case GET_CITY: | ||
| return [...state, action.payload.data]; | ||
| 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.
apiKey should not be hard coded like this - better to use a .env file