Download Amazon-clone/src/reducer/ListReducer.js from Devendra174/react-code-dataset: direct link, hf CLI and curl.
- Browser
- Download file 810 Bytes
-
https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/f5071caa249115f3b590492f1c8eb471e02aae4b/Amazon-clone/src/reducer/ListReducer.js
- Command line
-
hf download hf://datasets/Devendra174/react-code-dataset@f5071caa249115f3b590492f1c8eb471e02aae4b/Amazon-clone/src/reducer/ListReducer.js
-
curl -L -o ListReducer.js https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/f5071caa249115f3b590492f1c8eb471e02aae4b/Amazon-clone/src/reducer/ListReducer.js
810 Bytes
| const ListData = { | |
| ListItems: JSON.parse(localStorage.getItem("listItems")) || [], | |
| }; | |
| const ItemsAdded = (state = ListData, action) => { | |
| switch (action.type) { | |
| case "ADD": | |
| const productData = action.data; | |
| const updatedList = [...state.ListItems, productData]; | |
| localStorage.setItem("listItems", JSON.stringify(updatedList)); // Update local storage | |
| return { | |
| ...state, | |
| ListItems: updatedList, | |
| }; | |
| case "REMOVE": | |
| const itemId = action.id; | |
| const newList = state.ListItems.filter((element) => element.id !== itemId); | |
| localStorage.setItem("listItems", JSON.stringify(newList)); // Update local storage | |
| return { | |
| ...state, | |
| ListItems: newList, | |
| }; | |
| default: | |
| return state; | |
| } | |
| }; | |
| export default ItemsAdded; | |