123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- function cartReducer (state = {}, { type, good = {}, count = 1 }) {
- const types = {
- CART_ADD () {
- const { _id } = good;
- return {
- ...state,
- [_id]: { good, count: count + (state[_id]?.count || 0) }
- };
- },
- CART_MINUS () {
- const { _id } = good;
- return {
- ...state,
- [_id]: { good, count: (-count + (state[_id]?.count || 0)) < 1 ? 0 : -count + (state[_id]?.count || 0) }
- };
- },
- CART_REMOVE () {
- const { _id } = good;
- const newState = { ...state };
- const arrKeys = Object.keys(newState);
- for (const key of arrKeys) {
- if (_id === key) {
- delete newState[_id];
- }
- }
- return {
- ...newState
- };
- },
- CART_CHANGE () {
- const { _id } = good;
- return {
- ...state,
- [_id]: { good, count }
- };
- },
- CART_CLEAR () {
- return {
- };
- },
- AUTH_LOGOUT () {
- localStorage.clear();
- return {};
- }
- };
- if (type in types) { return types[type](); };
- return state;
- }
- export default cartReducer;
|