Candidate swapping button
This commit is contained in:
parent
6759a05743
commit
a2b8e7480e
6 changed files with 77 additions and 34 deletions
|
@ -1,4 +1,9 @@
|
||||||
import { ADD_ARTICLE } from "../constants/action-types"
|
import { ADD_ARTICLE } from "../constants/action-types"
|
||||||
|
import { SET_CANDIDATE } from "../constants/action-types"
|
||||||
|
|
||||||
|
export function setCandidate(payload) {
|
||||||
|
return { type: SET_CANDIDATE, payload }
|
||||||
|
}
|
||||||
|
|
||||||
export function addArticle(payload) {
|
export function addArticle(payload) {
|
||||||
return { type: ADD_ARTICLE, payload }
|
return { type: ADD_ARTICLE, payload }
|
||||||
|
|
|
@ -2,10 +2,12 @@ import React, { Component } from "react"
|
||||||
import { connect } from "react-redux"
|
import { connect } from "react-redux"
|
||||||
import uuidv1 from "uuid"
|
import uuidv1 from "uuid"
|
||||||
import { addArticle } from "../actions/index"
|
import { addArticle } from "../actions/index"
|
||||||
|
import { setCandidate } from "../actions/index"
|
||||||
|
|
||||||
function mapDispatchToProps(dispatch) {
|
function mapDispatchToProps(dispatch) {
|
||||||
return {
|
return {
|
||||||
addArticle: article => dispatch(addArticle(article))
|
addArticle: article => dispatch(addArticle(article)),
|
||||||
|
setCandidate: candidate => dispatch(setCandidate(candidate))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -14,11 +16,14 @@ class ConnectedForm extends Component {
|
||||||
super()
|
super()
|
||||||
|
|
||||||
this.state = {
|
this.state = {
|
||||||
title: ""
|
title: '',
|
||||||
|
candidate: 'Hillary'
|
||||||
}
|
}
|
||||||
|
|
||||||
this.handleChange = this.handleChange.bind(this)
|
this.handleChange = this.handleChange.bind(this)
|
||||||
this.handleSubmit = this.handleSubmit.bind(this)
|
this.handleSubmit = this.handleSubmit.bind(this)
|
||||||
|
this.clickTrump = this.clickTrump.bind(this)
|
||||||
|
this.clickHilary = this.clickHilary.bind(this)
|
||||||
}
|
}
|
||||||
|
|
||||||
handleChange(event) {
|
handleChange(event) {
|
||||||
|
@ -34,9 +39,22 @@ class ConnectedForm extends Component {
|
||||||
this.setState({ title: "" })
|
this.setState({ title: "" })
|
||||||
}
|
}
|
||||||
|
|
||||||
|
clickTrump(event) {
|
||||||
|
event.preventDefault()
|
||||||
|
this.props.setCandidate('Donald Trump')
|
||||||
|
this.setState({ candidate: 'Donald Trump' })
|
||||||
|
}
|
||||||
|
|
||||||
|
clickHilary(event) {
|
||||||
|
event.preventDefault()
|
||||||
|
this.props.setCandidate('Hillary Clinton')
|
||||||
|
this.setState({ candidate: 'Hillary Clinton' })
|
||||||
|
}
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
const { title } = this.state
|
const { title } = this.state
|
||||||
return (
|
return (
|
||||||
|
<div>
|
||||||
<form onSubmit={this.handleSubmit}>
|
<form onSubmit={this.handleSubmit}>
|
||||||
<div className="form-group">
|
<div className="form-group">
|
||||||
<label htmlFor="title">Title</label>
|
<label htmlFor="title">Title</label>
|
||||||
|
@ -52,6 +70,12 @@ class ConnectedForm extends Component {
|
||||||
SAVE
|
SAVE
|
||||||
</button>
|
</button>
|
||||||
</form>
|
</form>
|
||||||
|
|
||||||
|
<button onClick={this.clickTrump}>Donald Trump</button>
|
||||||
|
<button onClick={this.clickHilary}>Hillary Clinton</button>
|
||||||
|
|
||||||
|
<p>{this.state.candidate}</p>
|
||||||
|
</div>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,22 +8,27 @@ export class Post extends Component {
|
||||||
}
|
}
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
|
let cand = this.props.candidate === undefined ? 'Not set' : this.props.candidate
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
<div>
|
||||||
|
<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_str}>
|
<li className="list-group-item" key={el.id}>
|
||||||
<p>{el.created_at}</p>
|
<p>{el.title}</p>
|
||||||
<p>{el.text}</p>
|
|
||||||
</li>
|
</li>
|
||||||
))}
|
))}
|
||||||
</ul>
|
</ul>
|
||||||
|
</div>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function mapStateToProps(state) {
|
function mapStateToProps(state) {
|
||||||
return {
|
return {
|
||||||
articles: state.remoteArticles.slice(0, 10)
|
articles: state.remoteArticles.slice(0, 10),
|
||||||
|
candidate: state.candidate
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1 +1,2 @@
|
||||||
export const ADD_ARTICLE = "ADD_ARTICLE"
|
export const ADD_ARTICLE = "ADD_ARTICLE"
|
||||||
|
export const SET_CANDIDATE = "SET_CANDIDATE"
|
||||||
|
|
|
@ -1,8 +1,10 @@
|
||||||
import { ADD_ARTICLE } from '../constants/action-types'
|
import { ADD_ARTICLE } from '../constants/action-types'
|
||||||
|
import { SET_CANDIDATE } from '../constants/action-types'
|
||||||
|
|
||||||
const initialState = {
|
const initialState = {
|
||||||
articles: [],
|
articles: [],
|
||||||
remoteArticles: []
|
remoteArticles: [],
|
||||||
|
candidate: ''
|
||||||
}
|
}
|
||||||
|
|
||||||
function rootReducer(state = initialState, action) {
|
function rootReducer(state = initialState, action) {
|
||||||
|
@ -12,6 +14,12 @@ function rootReducer(state = initialState, action) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
else if (action.type === SET_CANDIDATE) {
|
||||||
|
return Object.assign({}, state, {
|
||||||
|
candidate: action.payload
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
else if (action.type === "DATA_LOADED") {
|
else if (action.type === "DATA_LOADED") {
|
||||||
return Object.assign({}, state, {
|
return Object.assign({}, state, {
|
||||||
remoteArticles: state.remoteArticles.concat(action.payload.message.statuses)
|
remoteArticles: state.remoteArticles.concat(action.payload.message.statuses)
|
||||||
|
|
|
@ -14,11 +14,11 @@ function* workerSaga() {
|
||||||
}
|
}
|
||||||
|
|
||||||
function getData() {
|
function getData() {
|
||||||
return fetch("http://localhost:3030/api/twitter?hashtag=tesla").then(res1 =>
|
// return fetch("http://localhost:3030/api/twitter?hashtag=tesla").then(res1 =>
|
||||||
res1.json()
|
// res1.json()
|
||||||
)
|
|
||||||
|
|
||||||
// return fetch("https://jsonplaceholder.typicode.com/posts").then(response =>
|
|
||||||
// response.json()
|
|
||||||
// )
|
// )
|
||||||
|
|
||||||
|
return fetch("https://jsonplaceholder.typicode.com/posts").then(response =>
|
||||||
|
response.json()
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue