{"size": 1958, "ext": "js", "max_stars_repo_path": "src/reducers/index.js", "max_stars_repo_name": "noelcperez/reactnativewithfluxtodoapp", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/reducers/index.js", "max_issues_repo_name": "noelcperez/reactnativewithfluxtodoapp", "max_issues_repo_issues_event_min_datetime": "2021-03-18T19:25:57.000Z", "max_issues_repo_issues_event_max_datetime": "2021-08-03T10:34:31.000Z", "max_forks_repo_path": "src/reducers/index.js", "max_forks_repo_name": "noelcperez/reactnativewithfluxtodoapp", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": 10, "max_forks_count": null, "avg_line_length": 22.7674418605, "max_line_length": 67, "alphanum_fraction": 0.4948927477}
import constants from '../constants'
import initialState from '../initialState.json'

export default appReducer = (state = initialState.app, action) => {
  switch (action.type) {
    case constants.REPLACE_TODOS:
      return {
        ...state,
        todos: action.payload
      }
    case constants.ADD_TODO:
      return {
        ...state,
        todos: [
          ...state.todos, {
            id: action.id,
            text: action.text,
            done: false
          }
        ]
      }
    case constants.TOOGLE_TODO:
      return {
        ...state,
        todos: state.todos.map(todo => {
          if (todo.id !== action.payload) {
            return todo
          }

          return {
            ...todo,
            done: !todo.done
          }
        })
      }
    case constants.REMOVE_TODO:
      return {
        ...state,
        todos: state.todos.filter(todo => {
          return todo.id !== action.payload
        })
      }
    case constants.SELECT_TODO:
      const selectedTodos = state.todos.filter(todo => {
        return todo.id === action.payload
      })
      if (selectedTodos.length > 0) {
        const selectedTodo = selectedTodos[0]
        return {
          ...state,
          selectedTodo: selectedTodo
        }
      }
      return state
    case constants.EDIT_TODO:
      return {
        ...state,
        todos: state.todos.map(todo => {
          if (todo.id !== action.todoId) {
            return todo
          }

          return {
            ...todo,
            text: action.todoText
          }
        })
      }
    case constants.FILTER_TODO:
      const filteredTodos = state.todos.filter(todo => {
        return todo.text.includes(action.payload)
      })
      return {
        ...state,
        filteredTodos: filteredTodos
      }
    case constants.TOOGLE_LOADING:
      return {
        ...state,
        loading: !state.loading
      }
    default:
      return state
  }
}
