stream-saga/src/components/Posts.jsx

47 lines
1 KiB
React
Raw Normal View History

2019-06-22 19:57:06 -04:00
import React, { useEffect } from "react"
2019-06-19 17:08:53 -04:00
import { connect } from "react-redux"
2019-06-21 10:53:16 -04:00
import { getHillaryData } from "../actions/index"
import { getTrumpData } from "../actions/index"
2019-06-19 17:08:53 -04:00
2019-06-22 19:57:06 -04:00
export function Post(props) {
let { articles, candidate, getHillaryData, getTrumpData } = props
// Fetch tweets when user clicks on candidate's button
useEffect(() => {
if (candidate === 'Hillary Clinton') {
getHillaryData()
2019-06-21 10:53:16 -04:00
}
2019-06-22 19:57:06 -04:00
else if (candidate === 'Donald Trump') {
getTrumpData()
}
}, [candidate, getHillaryData, getTrumpData])
2019-06-19 17:08:53 -04:00
2019-06-22 19:57:06 -04:00
let cand = candidate === undefined ? 'Not set' : candidate
2019-06-21 00:59:25 -04:00
2019-06-22 19:57:06 -04:00
return (
<div>
<p>@{cand}</p>
<ul className="list-group list-group-flush">
{articles.map(el => (
<li className="list-group-item" key={el.id_str}>
<p>{el.created_at}</p>
<p>{el.text}</p>
</li>
))}
</ul>
</div>
)
2019-06-19 17:08:53 -04:00
}
function mapStateToProps(state) {
2019-06-21 11:51:07 -04:00
return {
2019-06-21 00:59:25 -04:00
articles: state.remoteArticles.slice(0, 10),
candidate: state.candidate
2019-06-21 11:51:07 -04:00
}
2019-06-19 17:08:53 -04:00
}
export default connect(
2019-06-21 11:51:07 -04:00
mapStateToProps,
{ getHillaryData, getTrumpData }
2019-06-19 17:08:53 -04:00
)(Post)