diff --git a/docs/examples/todos/public/index.html b/docs/examples/todos/public/index.html index a48055a..81b9dd9 100644 --- a/docs/examples/todos/public/index.html +++ b/docs/examples/todos/public/index.html @@ -10,7 +10,11 @@ - +
diff --git a/docs/examples/todos/src/actions/index.js b/docs/examples/todos/src/actions/index.js index d10bb7a..43fb24f 100644 --- a/docs/examples/todos/src/actions/index.js +++ b/docs/examples/todos/src/actions/index.js @@ -1,7 +1,13 @@ -let nextTodoId = 0 +const nextTodoId = new Function(` + let nextId = 0 + return function() { + return nextId++ + } +`)() + export const addTodo = text => ({ type: 'ADD_TODO', - id: nextTodoId++, + id: nextTodoId(), text }) diff --git a/docs/examples/todos/src/containers/AddTodo.js b/docs/examples/todos/src/containers/AddTodo.js index ca73276..6612031 100644 --- a/docs/examples/todos/src/containers/AddTodo.js +++ b/docs/examples/todos/src/containers/AddTodo.js @@ -3,20 +3,20 @@ import { addTodo } from '../actions/index.js' const h = React.createElement const AddTodo = ({ dispatch }) => { - let input + const inputref = {} return ( h('div', null, - h('form', { onSubmit: e => { + h('form', { + onSubmit: e => { e.preventDefault() - if (!input.value.trim()) { - return + if (inputref.input.value.trim()) { + dispatch(addTodo(inputref.input.value)) + Object.assign(inputref.input, {value: ''}) } - dispatch(addTodo(input.value)) - input.value = '' } }, - h('input', {ref: node => {input = node}}), + h('input', {ref: input => Object.assign(inputref, {input})}), h('button', {type: 'submit'}, 'Add Todo') ) ) diff --git a/docs/examples/todos/src/containers/VisibleTodoList.js b/docs/examples/todos/src/containers/VisibleTodoList.js index cfa69d3..6e3a9d9 100644 --- a/docs/examples/todos/src/containers/VisibleTodoList.js +++ b/docs/examples/todos/src/containers/VisibleTodoList.js @@ -3,11 +3,15 @@ import TodoList from '../components/TodoList.js' import { VisibilityFilters } from '../actions/index.js' const getVisibleTodos = (todos, filter) => { - return { - [VisibilityFilters.SHOW_ALL]: todos, - [VisibilityFilters.SHOW_COMPLETED]: todos.filter(t => t.completed), - [VisibilityFilters.SHOW_ACTIVE]: todos.filter(t => !t.completed), - }[filter] + if(filter == VisibilityFilters.SHOW_ALL) { + return todos + } else if(filter == VisibilityFilters.SHOW_COMPLETED) { + return todos.filter(t => t.completed) + } else if(filter == VisibilityFilters.SHOW_ACTIVE) { + return todos.filter(t => !t.completed) + } else { + throw new Error('Unknown filter: ' + filter) + } } const mapStateToProps = state => ({ diff --git a/docs/examples/todos/src/reducers/todos.js b/docs/examples/todos/src/reducers/todos.js index fc6ed32..8c2df46 100644 --- a/docs/examples/todos/src/reducers/todos.js +++ b/docs/examples/todos/src/reducers/todos.js @@ -1,22 +1,21 @@ const todos = (state = [], action) => { - switch (action.type) { - case 'ADD_TODO': - return [ - ...state, - { - id: action.id, - text: action.text, - completed: false - } - ] - case 'TOGGLE_TODO': - return state.map(todo => - (todo.id === action.id) - ? {...todo, completed: !todo.completed} - : todo - ) - default: - return state + if(action.type == 'ADD_TODO') { + return [ + ...state, + { + id: action.id, + text: action.text, + completed: false + } + ] + } else if(action.type == 'TOGGLE_TODO') { + return state.map(todo => + (todo.id === action.id) + ? {...todo, completed: !todo.completed} + : todo + ) + } else { + return state } } diff --git a/docs/examples/todos/src/reducers/visibilityFilter.js b/docs/examples/todos/src/reducers/visibilityFilter.js index 613301c..285a79a 100644 --- a/docs/examples/todos/src/reducers/visibilityFilter.js +++ b/docs/examples/todos/src/reducers/visibilityFilter.js @@ -1,11 +1,10 @@ import { VisibilityFilters } from '../actions/index.js' const visibilityFilter = (state = VisibilityFilters.SHOW_ALL, action) => { - switch (action.type) { - case 'SET_VISIBILITY_FILTER': - return action.filter - default: - return state + if(action.type == 'SET_VISIBILITY_FILTER') { + return action.filter + } else { + return state } }