// src/redux/reducers.js
import { ADD_TODO, TOGGLE_TODO } from './actions';
// Initial state
const initialState = {
todos: []
};
// The reducer function
const todoReducer = (state = initialState, action) => {
switch (action.type) {
case ADD_TODO:
return {
...state,
todos: [...state.todos, { text: action.payload.text, completed: false }]
};
case TOGGLE_TODO:
return {
...state,
todos: state.todos.map((todo, index) =>
index === action.payload.index ? { ...todo, completed: !todo.completed } : todo
)
};
default:
return state;
}
};
export default todoReducer;