import { BUG_ADDED, BUG_REMOVED, BUG_RESOLVED } from './actionTypes';
export default function reducer(state = [], action) {
switch (action.type) {
case BUG_ADDED:
return [
...state,
{
id: ++lastId,
description: action.payload.description,
resolved: false,
},
];
case BUG_REMOVED:
return state.filter((bug) => bug.id !== action.payload.id);
case BUG_RESOLVED:
return state.map((bug) =>
bug.id !== action.payload.id ? bug : { ...bug, resolved: true }
);
default:
return state;
}
}
The reducer function takes in two variables. The first is state, which is an array of objects. The second is action, which is an object that represents the type of action being taken. The reducer function will first check to see if the action is a bug_added action. If it is, the reducer function will return the state and an object that contains the id of the last bug added, the description of the bug, and whether the bug has been resolved. If the action is a bug_removed action, the reducer function will return the state and an object that contains the id of the last bug removed, the description of the bug, and whether the bug has been resolved. Finally, the reducer function will return the state if the action is not a bug_added, bug_removed, or default action.
0 Comments
Log in to add a comment