Speedometer: Add a React + Redux TodoMVC implementation
[WebKit-https.git] / PerformanceTests / Speedometer / resources / todomvc / architecture-examples / react-redux / src / reducers / todos.js
1 import { ADD_TODO, DELETE_TODO, EDIT_TODO, COMPLETE_TODO, COMPLETE_ALL, CLEAR_COMPLETED } from '../constants/ActionTypes'
2
3 const initialState = []
4
5 export default function todos(state = initialState, action) {
6   switch (action.type) {
7     case ADD_TODO:
8       return [
9         {
10           id: state.reduce((maxId, todo) => Math.max(todo.id, maxId), -1) + 1,
11           completed: false,
12           text: action.text
13         },
14         ...state
15       ]
16
17     case DELETE_TODO:
18       return state.filter(todo =>
19         todo.id !== action.id
20       )
21
22     case EDIT_TODO:
23       return state.map(todo =>
24         todo.id === action.id ?
25           { ...todo, text: action.text } :
26           todo
27       )
28
29     case COMPLETE_TODO:
30       return state.map(todo =>
31         todo.id === action.id ?
32           { ...todo, completed: !todo.completed } :
33           todo
34       )
35
36     case COMPLETE_ALL:
37       const areAllMarked = state.every(todo => todo.completed)
38       return state.map(todo => ({
39         ...todo,
40         completed: !areAllMarked
41       }))
42
43     case CLEAR_COMPLETED:
44       return state.filter(todo => todo.completed === false)
45
46     default:
47       return state
48   }
49 }