Skip to content

Commit

Permalink
Add list
Browse files Browse the repository at this point in the history
  • Loading branch information
Jaakko Ojalehto committed Oct 2, 2015
1 parent bbb7dce commit 6a22288
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 5 deletions.
14 changes: 14 additions & 0 deletions examples/counter/actions/list.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export const ADD_COUNTER = 'ADD_COUNTER';
export const REMOVE_COUNTER = 'REMOVE_COUNTER';

export function add() {
return {
type: ADD_COUNTER
};
}

export function remove() {
return {
type: REMOVE_COUNTER
};
}
41 changes: 41 additions & 0 deletions examples/counter/components/List.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import React, { Component, PropTypes } from 'react';
import Counter from './Counter';

class List extends Component {

increment(index) {

This comment has been minimized.

Copy link
@Zeikko

Zeikko Oct 2, 2015

Owner

These functions cause the list to not be generic. How to do this in a generic way?

let action = this.props.counterActions.increment()
action.index = index
this.props.dispatch(action)
}

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>
);
}
}

List.propTypes = {
add: PropTypes.func.isRequired,
remove: PropTypes.func.isRequired,
counterList: PropTypes.array.isRequired,
dispatch: PropTypes.func.isRequired
};

export default List;
12 changes: 8 additions & 4 deletions examples/counter/containers/App.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
import { bindActionCreators } from 'redux';
import { connect } from 'react-redux';
import Counter from '../components/Counter';
import List from '../components/List';
import * as CounterActions from '../actions/counter';
import * as ListActions from '../actions/list';

function mapStateToProps(state) {
return {
counter: state.counter
counterList: state.counterList
};
}

function mapDispatchToProps(dispatch) {
return bindActionCreators(CounterActions, dispatch);
let actions = bindActionCreators(ListActions, dispatch);
actions.counterActions = bindActionCreators(CounterActions, dispatch);
actions.dispatch = dispatch
return actions;
}

export default connect(mapStateToProps, mapDispatchToProps)(Counter);
export default connect(mapStateToProps, mapDispatchToProps)(List);
6 changes: 5 additions & 1 deletion examples/counter/reducers/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { combineReducers } from 'redux';
import counter from './counter';
import list from './list'

const rootReducer = combineReducers({
counter
counterList: list(counter, {
add: 'ADD_COUNTER',
remove: 'REMOVE_COUNTER'
})
});

export default rootReducer;
22 changes: 22 additions & 0 deletions examples/counter/reducers/list.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
export default function list(reducer, actionTypes) {
return function (state = [], action) {
switch (action.type) {
case actionTypes.add:
return [...state, reducer(undefined, action)];
case actionTypes.remove:
return [...state.slice(0, action.index), ...state.slice(action.index + 1)];
default:
const { index, ...rest } = action;
if (typeof index !== 'undefined') {
return state.map((item, i) => {
if(index == i) {
return reducer(item, rest)
} else {
return item
}
});
}
return state;
}
}
}

0 comments on commit 6a22288

Please sign in to comment.