case EDIT_TODO:
const index = state.todos.findIndex(todo => todo.id == action.payload.id);
state.todos[index] = action.payload;
return state;
import {
GET_TODO,
GET_ALL_TODOS,
ADD_TODO,
EDIT_TODO,
DELETE_TODO,
} from "../constants.js";
const initialState = {
todos: [],
};
export const todoReducer = (state = initialState, action) => {
switch (action.type) {
case GET_TODO:
return state;
case GET_ALL_TODOS:
return { todos: action.payload };
case ADD_TODO:
state.push(action.payload);
return { state };
case EDIT_TODO:
state.todos.find(todo => todo.id == action.payload.id);
return state;
case DELETE_TODO:
state.todos.findIndex(item => item.id == action.payload);
return { todos: newTodos };
}
return state;
};
export const editTodo = (data) => {
return async (dispatch) => {
const responce = await fetch(EDIT_TODO_API, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(data),
});
const json = await responce.json();
dispatch({ type: EDIT_TODO, payload: json });
};
};