Commit 20a28abe authored by AlexandrValgamov's avatar AlexandrValgamov
Browse files

Widget | feat: add store

parent 20eea547
Loading
Loading
Loading
Loading

src/store/index.ts

0 → 100644
+4 −0
Original line number Diff line number Diff line
export * from './store';
export * from './slices';
export * from './types';
export * from './selectors';
+1 −0
Original line number Diff line number Diff line
export * from './selectors';
+7 −0
Original line number Diff line number Diff line
import { RootState } from '../types';

export const loggedInSelector = (state: RootState) => state.user.loggedIn;
export const userSelector = (state: RootState) => state.user.user;
export const accessTokenSelector = (state: RootState) => state.user.accessToken;
export const basketSelector = (state: RootState) => state.basket;
export const currentProductSelector = (state: RootState) => state.product;
+80 −0
Original line number Diff line number Diff line
import { PayloadAction, createSlice } from '@reduxjs/toolkit';

import { isObjectsEqual } from '@/utils';
import { IProductState, BasketState } from '@/interfaces';

const initialState: BasketState = {};

const basketSlice = createSlice({
  name: 'cart',
  initialState,
  reducers: {
    addItem(
      state,
      action: PayloadAction<{ shop: string; item: IProductState }>,
    ) {
      const { shop, item } = action.payload;

      if (!state[shop]) {
        state[shop] = {
          items: [],
          totalPrice: 0,
        };
      }

      const existingItem = state[shop].items.find((currentItem) =>
        isObjectsEqual(currentItem, item),
      );

      if (existingItem) {
        existingItem.count += 1;
      } else {
        state[shop].items.push({
          ...item,
          count: 1,
        });
      }

      state[shop].totalPrice = state[shop].items.reduce(
        (total, currentItem) => total + currentItem.cost * currentItem.count,
        0,
      );
    },

    removeItem(
      state,
      action: PayloadAction<{ shop: string; item: IProductState }>,
    ) {
      const { shop, item } = action.payload;

      const existingItem = state[shop].items.find((currentItem) =>
        isObjectsEqual(currentItem, item),
      );

      if (existingItem && existingItem.count > 1) {
        existingItem.count -= 1;
      } else {
        state[shop].items = state[shop].items.filter(
          (currentItem) => currentItem !== existingItem,
        );
      }

      state[shop].totalPrice = state[shop].items.reduce(
        (total, currentItem) => total + currentItem.cost * currentItem.count,
        0,
      );
    },

    clearCart(state, action: PayloadAction<{ shop: string }>) {
      const { shop } = action.payload;
      state[shop] = {
        items: [],
        totalPrice: 0,
      };
    },
  },
});

export const { addItem, removeItem, clearCart } = basketSlice.actions;

export const basketReducer = basketSlice.reducer;
+3 −0
Original line number Diff line number Diff line
export * from './basket-slice';
export * from './product-slice';
export * from './user-slice';
Loading