{"size": 1289, "ext": "js", "max_stars_repo_path": "src/store/cartSlice.js", "max_stars_repo_name": "mrrohanrp/amazon-app", "max_stars_repo_stars_event_min_datetime": "2022-03-01T04:49:58.000Z", "max_stars_repo_stars_event_max_datetime": "2022-03-01T04:49:58.000Z", "max_issues_repo_path": "src/store/cartSlice.js", "max_issues_repo_name": "mrrohanrp/amazon-app", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/store/cartSlice.js", "max_forks_repo_name": "mrrohanrp/amazon-app", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 1, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 25.2745098039, "max_line_length": 104, "alphanum_fraction": 0.5942591156}
import { createSlice } from '@reduxjs/toolkit';

const initialState = {
  cartCount: 0,
  cart: {
    /* B09JQZ3Y7V: { id: 'B09JQZ3Y7V', title: '', price: 193900, image: '', quantity: 1 } */
  }
};

export const cartSlice = createSlice({
  name: 'cart',
  initialState,
  reducers: {
    ADDTOCART: (state) => {
      state.cartCount += 1;
    },

    FILLCART: (state, action) => {
      if (state.cart[action.payload.id]) {
        state.cart[action.payload.id].quantity += 1;
      } else {
        state.cart[action.payload.id] = { ...action.payload, quantity: 1 };
      }
    },

    DELETEFROMCART: (state, action) => {
      state.cartCount -= state.cart[action.payload].quantity;
      delete state.cart[action.payload];
    },

    ADDQUANTITY: (state, action) => {
      state.cartCount += 1;
      state.cart[action.payload].quantity += 1;
    },

    SUBTRACTQUANTITY: (state, action) => {
      if (state.cart[action.payload].quantity === 1) {
        delete state.cart[action.payload];
        state.cartCount -= 1;
      } else {
        state.cartCount -= 1;
        state.cart[action.payload].quantity -= 1;
      }
    }
  }
});

export const { ADDTOCART, FILLCART, ADDQUANTITY, SUBTRACTQUANTITY, DELETEFROMCART } = cartSlice.actions;

export default cartSlice.reducer;
