Skip to content

Commit 6a22288

Browse files
author
Jaakko Ojalehto
committed
Add list
1 parent bbb7dce commit 6a22288

File tree

5 files changed

+90
-5
lines changed

5 files changed

+90
-5
lines changed

examples/counter/actions/list.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
export const ADD_COUNTER = 'ADD_COUNTER';
2+
export const REMOVE_COUNTER = 'REMOVE_COUNTER';
3+
4+
export function add() {
5+
return {
6+
type: ADD_COUNTER
7+
};
8+
}
9+
10+
export function remove() {
11+
return {
12+
type: REMOVE_COUNTER
13+
};
14+
}

examples/counter/components/List.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import React, { Component, PropTypes } from 'react';
2+
import Counter from './Counter';
3+
4+
class List extends Component {
5+
6+
increment(index) {
7+
let action = this.props.counterActions.increment()
8+
action.index = index
9+
this.props.dispatch(action)
10+
}
11+
12+
decrement(index) {
13+
let action = this.props.counterActions.decrement()
14+
action.index = index
15+
this.props.dispatch(action)
16+
}
17+
18+
render() {
19+
const { add, remove, counterList, counterActions } = this.props;
20+
return (
21+
<p>
22+
{' '}
23+
<button onClick={add}>Add counter</button>
24+
{' '}
25+
<button onClick={remove}>Remove counter</button>
26+
{counterList.map((counter, counterId) => {
27+
return <Counter key={counterId} id={counterId} counter={counter} increment={this.increment.bind(this, counterId)} decrement={this.decrement.bind(this, counterId)}></Counter>
28+
})}
29+
</p>
30+
);
31+
}
32+
}
33+
34+
List.propTypes = {
35+
add: PropTypes.func.isRequired,
36+
remove: PropTypes.func.isRequired,
37+
counterList: PropTypes.array.isRequired,
38+
dispatch: PropTypes.func.isRequired
39+
};
40+
41+
export default List;

examples/counter/containers/App.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
import { bindActionCreators } from 'redux';
22
import { connect } from 'react-redux';
3-
import Counter from '../components/Counter';
3+
import List from '../components/List';
44
import * as CounterActions from '../actions/counter';
5+
import * as ListActions from '../actions/list';
56

67
function mapStateToProps(state) {
78
return {
8-
counter: state.counter
9+
counterList: state.counterList
910
};
1011
}
1112

1213
function mapDispatchToProps(dispatch) {
13-
return bindActionCreators(CounterActions, dispatch);
14+
let actions = bindActionCreators(ListActions, dispatch);
15+
actions.counterActions = bindActionCreators(CounterActions, dispatch);
16+
actions.dispatch = dispatch
17+
return actions;
1418
}
1519

16-
export default connect(mapStateToProps, mapDispatchToProps)(Counter);
20+
export default connect(mapStateToProps, mapDispatchToProps)(List);

examples/counter/reducers/index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
import { combineReducers } from 'redux';
22
import counter from './counter';
3+
import list from './list'
34

45
const rootReducer = combineReducers({
5-
counter
6+
counterList: list(counter, {
7+
add: 'ADD_COUNTER',
8+
remove: 'REMOVE_COUNTER'
9+
})
610
});
711

812
export default rootReducer;

examples/counter/reducers/list.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
export default function list(reducer, actionTypes) {
2+
return function (state = [], action) {
3+
switch (action.type) {
4+
case actionTypes.add:
5+
return [...state, reducer(undefined, action)];
6+
case actionTypes.remove:
7+
return [...state.slice(0, action.index), ...state.slice(action.index + 1)];
8+
default:
9+
const { index, ...rest } = action;
10+
if (typeof index !== 'undefined') {
11+
return state.map((item, i) => {
12+
if(index == i) {
13+
return reducer(item, rest)
14+
} else {
15+
return item
16+
}
17+
});
18+
}
19+
return state;
20+
}
21+
}
22+
}

0 commit comments

Comments
 (0)