xxxxxxxxxx
case foodConstants.FOOD_SUCCESS_POST: {
let updatedItems = { state.items };
const itemIndex = updatedItems.findIndex((food1) => food1.id === action.id);
if(itemIndex > -1){
updatedItems[itemIndex] = {
updatedItems[itemIndex],
action,
}
}
return { state, items: updatedItems };
}
xxxxxxxxxx
// (state, action) => newState
function todosReducer(todos = [], action = {}) {
switch (action.type) {
case 'ADD_TODO':
return [todos, action.payload];
case 'DELETE_TODO':
return todos.filter((todo) => todo.id !== action.payload.id);
case 'DELETE_ALL_TODO':
return [];
case 'TOGGLE_TODO':
return todos.map((todo) => {
if (todo.id === action.payload.id) {
return { todo, complete: !todo.complete };
}
return todo;
});
default:
return todos;
}
}
xxxxxxxxxx
case foodConstants.FOOD_SUCCESS_POST: {
const updatedItems = state.items.map((food1) => {
if (food1.id === action.id) {
return { action };
}
return food1;
});
return { state, items: updatedItems };
}
xxxxxxxxxx
if (action.type == types.FILTER_TITLE_CHANGED) {
let newState = { state };
newState.filterTitleStr = action.filterTitleStr;
return newState;
} else if (action.type == types.FILTER_AUTHOR_CHANGED) {
let newState = { state };
newState.filterAuthorStr = action.filterAuthorStr;
return newState;
} else if (action.type == types.FILTER_CATEGORY_CHANGED) {
let newState = { state };
newState.filterCategoryStr = action.filterCategoryStr;
return newState;
}