{"size": 860, "ext": "js", "max_stars_repo_path": "src/reducers/todos.js", "max_stars_repo_name": "planetbartoski/react-todo", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/reducers/todos.js", "max_issues_repo_name": "planetbartoski/react-todo", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/reducers/todos.js", "max_forks_repo_name": "planetbartoski/react-todo", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 18.6956521739, "max_line_length": 63, "alphanum_fraction": 0.5279069767}
import {ADD_TODO, TOGGLE_TODO} from '../constants/actionTypes';

/**
 * Initial state of the todo list = []
 */
export const initialState = [];

/**
 * Todo list reducer
 *
 * Executes actions on todos list
 *
 * @param {array} [state = initialState] - current state
 * @param {object} action - action to act upon
 * @returns {array} - new state
 */
export function todos(state = initialState, action) {
  switch (action.type) {
    
    case ADD_TODO:
      return [
        ...state,
        {
          id: action.id,
          text: action.text,
          completed: false
        }
      ];
    
    case TOGGLE_TODO:
      return state.map( t => {
        if (t.id !== action.id) {
          return t;
        }

        return Object.assign({}, t, {
          completed: !t.completed
        });
      });
    
    default:
      return state;
  
  }
}
