Hillary and Trump sagas

This commit is contained in:
Rodrigo Pedroso 2019-06-21 10:53:16 -04:00
commit aac33139ba
5 changed files with 75 additions and 11 deletions

View file

@ -12,3 +12,11 @@ export function addArticle(payload) {
export function getData() { export function getData() {
return { type: "DATA_REQUESTED" } return { type: "DATA_REQUESTED" }
} }
export function getTrumpData() {
return { type: "DATA_TRUMP_REQUESTED" }
}
export function getHillaryData() {
return { type: "DATA_HILLARY_REQUESTED" }
}

View file

@ -17,7 +17,7 @@ const App = () => (
</div> </div>
<div className="col-md-4 offset-md-1"> <div className="col-md-4 offset-md-1">
<h2>API posts</h2> <h2>Latest tweets</h2>
<Post /> <Post />
</div> </div>

View file

@ -17,7 +17,7 @@ class ConnectedForm extends Component {
this.state = { this.state = {
title: '', title: '',
candidate: 'Hillary' candidate: ''
} }
this.handleChange = this.handleChange.bind(this) this.handleChange = this.handleChange.bind(this)
@ -26,6 +26,11 @@ class ConnectedForm extends Component {
this.clickHilary = this.clickHilary.bind(this) this.clickHilary = this.clickHilary.bind(this)
} }
componentDidMount() {
this.props.setCandidate('Hillary Clinton')
this.setState({ candidate: 'Hillary Clinton'})
}
handleChange(event) { handleChange(event) {
this.setState({ [event.target.id]: event.target.value }) this.setState({ [event.target.id]: event.target.value })
} }

View file

@ -1,10 +1,18 @@
import React, { Component } from "react" import React, { Component } from "react"
import { connect } from "react-redux" import { connect } from "react-redux"
import { getData } from "../actions/index" import { getData } from "../actions/index"
import { getHillaryData } from "../actions/index"
import { getTrumpData } from "../actions/index"
export class Post extends Component { export class Post extends Component {
componentDidMount() { componentDidMount() {
this.props.getData() // this.props.getData()
if (this.props.candidate === 'Hilary Clinton') {
this.props.getHillaryData()
}
else {
this.props.getTrumpData()
}
} }
render() { render() {
@ -15,8 +23,9 @@ export class Post extends Component {
<p>Candidate: {cand}</p> <p>Candidate: {cand}</p>
<ul className="list-group list-group-flush"> <ul className="list-group list-group-flush">
{this.props.articles.map(el => ( {this.props.articles.map(el => (
<li className="list-group-item" key={el.id}> <li className="list-group-item" key={el.id_str}>
<p>{el.title}</p> <p>{el.created_at}</p>
<p>{el.text}</p>
</li> </li>
))} ))}
</ul> </ul>
@ -34,5 +43,5 @@ function mapStateToProps(state) {
export default connect( export default connect(
mapStateToProps, mapStateToProps,
{ getData } { getData, getHillaryData, getTrumpData }
)(Post) )(Post)

View file

@ -1,6 +1,14 @@
import { takeEvery, call, put } from 'redux-saga/effects' import { takeEvery, call, put, all } from 'redux-saga/effects'
export default function* watcherSaga() { export default function* watcherSaga() {
yield all([
worker(),
workerHillary(),
workerTrump()
])
}
function* worker() {
yield takeEvery("DATA_REQUESTED", workerSaga) yield takeEvery("DATA_REQUESTED", workerSaga)
} }
@ -13,12 +21,46 @@ function* workerSaga() {
} }
} }
function getData() { function* workerHillary() {
// return fetch("http://localhost:3030/api/twitter?hashtag=tesla").then(res1 => yield takeEvery("DATA_TRUMP_REQUESTED", workerTrumpSaga)
// res1.json() }
// )
function* workerHillarySaga() {
try {
const payload = yield call(getHillaryData)
yield put({ type: "DATA_LOADED", payload })
} catch (e) {
yield put({ type: "API_ERRORED", payload: e })
}
}
function* workerTrump() {
yield takeEvery("DATA_HILLARY_REQUESTED", workerHillarySaga)
}
function* workerTrumpSaga() {
try {
const payload = yield call(getTrumpData)
yield put({ type: "DATA_LOADED", payload })
} catch (e) {
yield put({ type: "API_ERRORED", payload: e })
}
}
function getData() {
return fetch("https://jsonplaceholder.typicode.com/posts").then(response => return fetch("https://jsonplaceholder.typicode.com/posts").then(response =>
response.json() response.json()
) )
} }
function getHillaryData() {
return fetch("http://localhost:3030/api/twitter?hashtag=Hillary%20Clinton").then(res1 =>
res1.json()
)
}
function getTrumpData() {
return fetch("http://localhost:3030/api/twitter?hashtag=Donald%20Trump").then(res1 =>
res1.json()
)
}