-
-
Notifications
You must be signed in to change notification settings - Fork 15.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Note: some things are still broken, in particular actions requiring middleware. I'm not sure how to fix them right now—we'll need to discuss this.
- Loading branch information
Showing
6 changed files
with
95 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,24 @@ | ||
export const ADD_COUNTER = 'ADD_COUNTER'; | ||
export const REMOVE_COUNTER = 'REMOVE_COUNTER'; | ||
export const ADD_TO_LIST = 'ADD_TO_LIST'; | ||
export const REMOVE_FROM_LIST = 'REMOVE_FROM_LIST'; | ||
export const PERFORM_IN_LIST = 'PERFORM_IN_LIST'; | ||
|
||
export function add() { | ||
export function addToList() { | ||
return { | ||
type: ADD_COUNTER | ||
type: ADD_TO_LIST | ||
}; | ||
} | ||
|
||
export function remove() { | ||
export function removeFromList(index) { | ||
return { | ||
type: REMOVE_COUNTER | ||
type: REMOVE_FROM_LIST, | ||
index | ||
}; | ||
} | ||
|
||
export function performInList(index, action) { | ||
return { | ||
type: PERFORM_IN_LIST, | ||
index, | ||
action | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,39 @@ | ||
import React, { Component, PropTypes } from 'react'; | ||
import Counter from './Counter'; | ||
import { addToList, removeFromList, performInList } from '../actions/list'; | ||
|
||
class List extends Component { | ||
export default function list(mapItemStateToProps) { | ||
return function (Item) { | ||
return class List extends Component { | ||
static propTypes = { | ||
dispatch: PropTypes.func.isRequired, | ||
items: PropTypes.array.isRequired | ||
}; | ||
|
||
increment(index) { | ||
let action = this.props.counterActions.increment() | ||
action.index = index | ||
this.props.dispatch(action) | ||
} | ||
render() { | ||
const { dispatch, items } = this.props; | ||
return ( | ||
<div> | ||
<button onClick={() => | ||
dispatch(addToList()) | ||
}>Add counter</button> | ||
|
||
decrement(index) { | ||
let action = this.props.counterActions.decrement() | ||
action.index = index | ||
this.props.dispatch(action) | ||
} | ||
|
||
render() { | ||
const { add, remove, counterList, counterActions } = this.props; | ||
return ( | ||
<p> | ||
{' '} | ||
<button onClick={add}>Add counter</button> | ||
{' '} | ||
<button onClick={remove}>Remove counter</button> | ||
{counterList.map((counter, counterId) => { | ||
return <Counter key={counterId} id={counterId} counter={counter} increment={this.increment.bind(this, counterId)} decrement={this.decrement.bind(this, counterId)}></Counter> | ||
})} | ||
</p> | ||
); | ||
} | ||
<br /> | ||
{items.length > 0 && | ||
<button onClick={() => | ||
dispatch(removeFromList(items.length - 1)) | ||
}>Remove counter</button> | ||
} | ||
<br /> | ||
{this.props.items.map((item, index) => | ||
<Item {...mapItemStateToProps(item)} | ||
key={index} | ||
dispatch={action => | ||
dispatch(performInList(index, action)) | ||
} /> | ||
)} | ||
</div> | ||
) | ||
} | ||
} | ||
}; | ||
} | ||
|
||
List.propTypes = { | ||
add: PropTypes.func.isRequired, | ||
remove: PropTypes.func.isRequired, | ||
counterList: PropTypes.array.isRequired, | ||
dispatch: PropTypes.func.isRequired | ||
}; | ||
|
||
export default List; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,17 @@ | ||
import { bindActionCreators } from 'redux'; | ||
import { connect } from 'react-redux'; | ||
import List from '../components/List'; | ||
import * as CounterActions from '../actions/counter'; | ||
import * as ListActions from '../actions/list'; | ||
|
||
function mapStateToProps(state) { | ||
import counter from '../components/Counter'; | ||
import list from '../components/list'; | ||
|
||
const CounterList = list(function mapItemStateToProps(itemState) { | ||
return { | ||
counterList: state.counterList | ||
counter: itemState | ||
}; | ||
} | ||
|
||
function mapDispatchToProps(dispatch) { | ||
let actions = bindActionCreators(ListActions, dispatch); | ||
actions.counterActions = bindActionCreators(CounterActions, dispatch); | ||
actions.dispatch = dispatch | ||
return actions; | ||
} | ||
})(counter); | ||
|
||
export default connect(mapStateToProps, mapDispatchToProps)(List); | ||
export default connect(function mapStateToProps(state) { | ||
return { | ||
items: state.counterList | ||
}; | ||
})(CounterList); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters