diff --git a/src/app/App.tsx b/src/app/App.tsx index c23ca576..5757f08a 100644 --- a/src/app/App.tsx +++ b/src/app/App.tsx @@ -1,7 +1,7 @@ import { useEffect } from 'react' import { RouterProvider } from 'react-router-dom' -import { getCart } from '@/entities/CartEntity/model/slice/cartEntitySlice' +import { getCart } from '@/entities/CartEntity/model/services/getCart' import { getCurrentUser } from '@/features/login/model/services/getCurrentUser/getCurrentUser' import { loginActions } from '@/features/login/model/slice/loginSlice' import { $api } from '@/shared/api/api' diff --git a/src/app/providers/StoreProvider/config/StateSchema.ts b/src/app/providers/StoreProvider/config/StateSchema.ts index c32743ec..5f0b5a0a 100644 --- a/src/app/providers/StoreProvider/config/StateSchema.ts +++ b/src/app/providers/StoreProvider/config/StateSchema.ts @@ -16,8 +16,6 @@ import { ICategoryFiltersSchema } from '@/components/Dropdown/types/types' import type { IFeedbackFormSchema } from '@/widgets/FeedbackForm/model/scheme/feedbackFormSliceSchema' import { ICartEntitySchema } from '@/entities/CartEntity/model/types/types' import { IAboutUsSchema } from '@/pages/AboutUsPage/model/types/types' -import { ICartSchema } from '@/pages/CartPage/model/types' -import { IProductAmountStateSchema } from '@/features/CartEdit/model/types' import { IFeedbackSchema } from '@/features/Reviews/model/types/types' import { TNumberOfPageSchema } from '@/components/Pagination/types/types' @@ -40,10 +38,8 @@ export interface StateSchema { categorySlug: CategorySlug categoryBranches: ICategorySchema getCategories: IMainCategorySchema - cart: ICartSchema cartEntity: ICartEntitySchema categoryFilters: ICategoryFiltersSchema - productAmount: IProductAmountStateSchema feedbacks: IFeedbackSchema pagination: TNumberOfPageSchema } diff --git a/src/app/providers/StoreProvider/config/store.ts b/src/app/providers/StoreProvider/config/store.ts index 537d13c5..88d5aaaf 100644 --- a/src/app/providers/StoreProvider/config/store.ts +++ b/src/app/providers/StoreProvider/config/store.ts @@ -20,9 +20,7 @@ import { getCategoriesReducer } from '@/widgets/CategoryList/slice/pageCategorie import { categoryFiltersSliceReducer } from '@/components/Dropdown/slice/filtersSlice' import { feedbackFormReducer } from '@/widgets/FeedbackForm/model/slice/feedbackFormSlice' import { aboutUsReducer } from '@/pages/AboutUsPage/model/slice/aboutUsSlice' -import { cartReducer } from '@/pages/CartPage/model/slice' import { cartEntityReducer } from '@/entities/CartEntity/model/slice/cartEntitySlice' -import { productAmountReducer } from '@/features/CartEdit/model/slice/productAmountSlice' import { feedbacksReducer } from '@/features/Reviews/model/slice/feedbacksSlice' import { paginationSliceReducer } from '@/components/Pagination/slice/paginationSlice' @@ -49,9 +47,7 @@ const rootReducer: ReducersMapObject = { categoryBranches: categoryBranchesReducer, getCategories: getCategoriesReducer, cartEntity: cartEntityReducer, - cart: cartReducer, categoryFilters: categoryFiltersSliceReducer, - productAmount: productAmountReducer, pagination: paginationSliceReducer } diff --git a/src/entities/CartEntity/model/hooks/cartHooks.ts b/src/entities/CartEntity/model/hooks/cartHooks.ts index c50c7a4a..144ec8e6 100644 --- a/src/entities/CartEntity/model/hooks/cartHooks.ts +++ b/src/entities/CartEntity/model/hooks/cartHooks.ts @@ -5,7 +5,7 @@ import { Routes } from '@/shared/config/routerConfig/routes' import { useAppDispatch } from '@/shared/libs/hooks/store' import { isInCartBySlug } from '../functions/cartHelper' -import { addToCart } from '../slice/cartEntitySlice' +import { addToCart } from '../services/addToCart' import { useCartSelector } from './sliceHooks' diff --git a/src/pages/CartPage/model/selector.ts b/src/entities/CartEntity/model/selectors/selectors.ts similarity index 79% rename from src/pages/CartPage/model/selector.ts rename to src/entities/CartEntity/model/selectors/selectors.ts index 4c120071..3cceb75e 100644 --- a/src/pages/CartPage/model/selector.ts +++ b/src/entities/CartEntity/model/selectors/selectors.ts @@ -1,5 +1,5 @@ import { StateSchema } from '@/app/providers/StoreProvider' export const getCartSelector = (state: StateSchema) => { - return state.cart.cart + return state.cartEntity.cart } diff --git a/src/entities/CartEntity/model/services/addToCart.ts b/src/entities/CartEntity/model/services/addToCart.ts new file mode 100644 index 00000000..e80dd7e3 --- /dev/null +++ b/src/entities/CartEntity/model/services/addToCart.ts @@ -0,0 +1,23 @@ +import { createAsyncThunk } from '@reduxjs/toolkit' + +import { ThunkConfig } from '@/app/providers/StoreProvider/config/StateSchema' +import { apiErrorIdentify } from '@/shared/api/apiErrorIdentify' +import { ApiError, ApiErrorTypes, ApiRoutes } from '@/shared/api/types' + +import type { IAddedProduct } from '../types/types' + +import { getCart } from './getCart' + +export const addToCart = createAsyncThunk>( + 'cartEntitie/addToCart', + async (addedProduct, thunkAPI) => { + const { rejectWithValue, extra } = thunkAPI + try { + await extra.api.post(`api/${ApiRoutes.CART_LIST}/`, addedProduct) + + thunkAPI.dispatch(getCart()) + } catch (error) { + return rejectWithValue(apiErrorIdentify(error, ApiErrorTypes.DATA_EMPTY_ERROR)) + } + } +) diff --git a/src/pages/CartPage/model/services.ts b/src/entities/CartEntity/model/services/getCart.ts similarity index 73% rename from src/pages/CartPage/model/services.ts rename to src/entities/CartEntity/model/services/getCart.ts index 2cdeb36b..559055c3 100644 --- a/src/pages/CartPage/model/services.ts +++ b/src/entities/CartEntity/model/services/getCart.ts @@ -3,16 +3,15 @@ import { createAsyncThunk } from '@reduxjs/toolkit' import { ThunkConfig } from '@/app/providers/StoreProvider/config/StateSchema' import { apiErrorIdentify } from '@/shared/api/apiErrorIdentify' import { ApiError, ApiErrorTypes, ApiRoutes } from '@/shared/api/types' -import { ICart } from '@/shared/model/types/CartModel' -export const getCartList = createAsyncThunk>( - 'cart', +import type { ICartEntity } from '../types/types' + +export const getCart = createAsyncThunk>( + 'cartEntitie/getCart', async (_, thunkAPI) => { const { rejectWithValue, extra } = thunkAPI try { - const { data } = await extra.api.get(`api/${ApiRoutes.CART_LIST}`, { - withCredentials: true - }) + const { data } = await extra.api.get(`api/${ApiRoutes.CART_LIST}/`) return data } catch (error) { return rejectWithValue(apiErrorIdentify(error, ApiErrorTypes.DATA_EMPTY_ERROR)) diff --git a/src/features/CartEdit/model/services/putDecreaseProductAmount.ts b/src/entities/CartEntity/model/services/putDecreaseProductAmount.ts similarity index 66% rename from src/features/CartEdit/model/services/putDecreaseProductAmount.ts rename to src/entities/CartEntity/model/services/putDecreaseProductAmount.ts index 513d95a5..f6accecd 100644 --- a/src/features/CartEdit/model/services/putDecreaseProductAmount.ts +++ b/src/entities/CartEntity/model/services/putDecreaseProductAmount.ts @@ -3,18 +3,17 @@ import { createAsyncThunk } from '@reduxjs/toolkit' import { ThunkConfig } from '@/app/providers/StoreProvider/config/StateSchema' import { apiErrorIdentify } from '@/shared/api/apiErrorIdentify' import { ApiError, ApiErrorTypes, ApiRoutes } from '@/shared/api/types' -import { IProductCartList } from '@/shared/model/types/ProductCartListModel' +import type { IProductCartList } from '@/shared/model/types/ProductCartListModel' + +import { getCart } from './getCart' export const putDecreaseProductAmount = createAsyncThunk>( - 'cart-decrease-product-amount', + 'cartEntitie/decrease-product-amount', async (productId, thunkAPI) => { const { rejectWithValue, extra } = thunkAPI try { - const { data } = await extra.api.put( - `api/${ApiRoutes.DECREASE_PRODUCT_AMOUNT}`, - { product: productId }, - { withCredentials: true } - ) + const { data } = await extra.api.put(`api/${ApiRoutes.DECREASE_PRODUCT_AMOUNT}`, { product: productId }) + thunkAPI.dispatch(getCart()) return data } catch (error) { return rejectWithValue(apiErrorIdentify(error, ApiErrorTypes.DATA_EMPTY_ERROR)) diff --git a/src/features/CartEdit/model/services/putIncreaseProductAmount.ts b/src/entities/CartEntity/model/services/putIncreaseProductAmount.ts similarity index 66% rename from src/features/CartEdit/model/services/putIncreaseProductAmount.ts rename to src/entities/CartEntity/model/services/putIncreaseProductAmount.ts index 31f22575..59465e1d 100644 --- a/src/features/CartEdit/model/services/putIncreaseProductAmount.ts +++ b/src/entities/CartEntity/model/services/putIncreaseProductAmount.ts @@ -3,20 +3,17 @@ import { createAsyncThunk } from '@reduxjs/toolkit' import { ThunkConfig } from '@/app/providers/StoreProvider/config/StateSchema' import { apiErrorIdentify } from '@/shared/api/apiErrorIdentify' import { ApiError, ApiErrorTypes, ApiRoutes } from '@/shared/api/types' -import { IProductCartList } from '@/shared/model/types/ProductCartListModel' +import type { IProductCartList } from '@/shared/model/types/ProductCartListModel' + +import { getCart } from './getCart' export const putIncreaseProductAmount = createAsyncThunk>( - 'cart-increase-product-amount', + 'cartEntitie/increase-product-amount', async (productId, thunkAPI) => { const { rejectWithValue, extra } = thunkAPI try { - const { data } = await extra.api.put( - `api/${ApiRoutes.INCREASE_PRODUCT_AMOUNT}`, - { product: productId }, - { - withCredentials: true - } - ) + const { data } = await extra.api.put(`api/${ApiRoutes.INCREASE_PRODUCT_AMOUNT}`, { product: productId }) + thunkAPI.dispatch(getCart()) return data } catch (error) { return rejectWithValue(apiErrorIdentify(error, ApiErrorTypes.DATA_EMPTY_ERROR)) diff --git a/src/features/CartEdit/model/services/putRemoveProduct.ts b/src/entities/CartEntity/model/services/putRemoveProduct.ts similarity index 72% rename from src/features/CartEdit/model/services/putRemoveProduct.ts rename to src/entities/CartEntity/model/services/putRemoveProduct.ts index 3957db0f..95b54b7e 100644 --- a/src/features/CartEdit/model/services/putRemoveProduct.ts +++ b/src/entities/CartEntity/model/services/putRemoveProduct.ts @@ -4,20 +4,17 @@ import { ThunkConfig } from '@/app/providers/StoreProvider/config/StateSchema' import { apiErrorIdentify } from '@/shared/api/apiErrorIdentify' import { ApiError, ApiErrorTypes, ApiRoutes } from '@/shared/api/types' +import { getCart } from './getCart' + export const putRemoveProduct = createAsyncThunk>( - 'cart-remove-product', + 'cartEntitie/remove-product', async (productId, thunkAPI) => { const { rejectWithValue, extra } = thunkAPI try { - const { data } = await extra.api.put( - `api/${ApiRoutes.REMOVE_PRODUCT}`, - { - product: productId - }, - { - withCredentials: true - } - ) + const { data } = await extra.api.put(`api/${ApiRoutes.REMOVE_PRODUCT}`, { + product: productId + }) + thunkAPI.dispatch(getCart()) return data } catch (error) { return rejectWithValue(apiErrorIdentify(error, ApiErrorTypes.DATA_EMPTY_ERROR)) diff --git a/src/entities/CartEntity/model/services/putRenewProductAmount.ts b/src/entities/CartEntity/model/services/putRenewProductAmount.ts new file mode 100644 index 00000000..9ecf29be --- /dev/null +++ b/src/entities/CartEntity/model/services/putRenewProductAmount.ts @@ -0,0 +1,28 @@ +import { createAsyncThunk } from '@reduxjs/toolkit' + +import { ThunkConfig } from '@/app/providers/StoreProvider/config/StateSchema' +import { apiErrorIdentify } from '@/shared/api/apiErrorIdentify' +import { ApiError, ApiErrorTypes, ApiRoutes } from '@/shared/api/types' +import type { IProductCartList } from '@/shared/model/types/ProductCartListModel' + +import type { IAddedProduct } from '../types/types' + +import { getCart } from './getCart' + +export const putRenewProductAmount = createAsyncThunk>( + 'cartEntitie/renew-product-amount', + async (request, thunkAPI) => { + const { rejectWithValue, extra } = thunkAPI + try { + const { data } = await extra.api.put(`api/${ApiRoutes.RENEW_PRODUCT_AMOUNT}${request.cart}/`, { + product: request.product, + cart: request.cart, + amount: request.amount + }) + thunkAPI.dispatch(getCart()) + return data + } catch (error) { + return rejectWithValue(apiErrorIdentify(error, ApiErrorTypes.DATA_EMPTY_ERROR)) + } + } +) diff --git a/src/entities/CartEntity/model/slice/cartEntitySlice.ts b/src/entities/CartEntity/model/slice/cartEntitySlice.ts index 9dd574d4..ae091291 100644 --- a/src/entities/CartEntity/model/slice/cartEntitySlice.ts +++ b/src/entities/CartEntity/model/slice/cartEntitySlice.ts @@ -1,38 +1,14 @@ -import { createAsyncThunk, createSlice } from '@reduxjs/toolkit' +import { createSlice } from '@reduxjs/toolkit' -import type { ThunkConfig } from '@/app/providers/StoreProvider/config/StateSchema' -import { apiErrorIdentify } from '@/shared/api/apiErrorIdentify' import { rejectedPayloadHandle } from '@/shared/api/rejectedPayloadHandle' -import { ApiError, ApiErrorTypes, ApiRoutes } from '@/shared/api/types' -import type { IAddedProduct, ICartEntity, ICartEntitySchema } from '../types/types' - -export const getCart = createAsyncThunk>( - 'cart/getCart', - async (_, thunkAPI) => { - const { rejectWithValue, extra } = thunkAPI - try { - const { data } = await extra.api.get(`api/${ApiRoutes.CART_LIST}/`) - return data - } catch (error) { - return rejectWithValue(apiErrorIdentify(error, ApiErrorTypes.DATA_EMPTY_ERROR)) - } - } -) - -export const addToCart = createAsyncThunk>( - 'cart/addToCart', - async (addedProduct, thunkAPI) => { - const { rejectWithValue, extra } = thunkAPI - try { - await extra.api.post(`api/${ApiRoutes.CART_LIST}/`, addedProduct) - - thunkAPI.dispatch(getCart()) - } catch (error) { - return rejectWithValue(apiErrorIdentify(error, ApiErrorTypes.DATA_EMPTY_ERROR)) - } - } -) +import { addToCart } from '../services/addToCart' +import { getCart } from '../services/getCart' +import { putDecreaseProductAmount } from '../services/putDecreaseProductAmount' +import { putIncreaseProductAmount } from '../services/putIncreaseProductAmount' +import { putRemoveProduct } from '../services/putRemoveProduct' +import { putRenewProductAmount } from '../services/putRenewProductAmount' +import type { ICartEntitySchema } from '../types/types' const initialState: ICartEntitySchema = { isLoading: false, @@ -41,18 +17,20 @@ const initialState: ICartEntitySchema = { id: 0, products: [], user: 0, - cart_full_price: 0 + cart_full_price: 0, + cart_full_weight: 0 } } export const cartEntitySlice = createSlice({ - name: 'cart', + name: 'cartEntitie', initialState, reducers: {}, extraReducers: builder => { builder .addCase(getCart.pending, state => { state.isLoading = true + state.error = null }) .addCase(getCart.fulfilled, (state, { payload }) => { state.isLoading = false @@ -65,6 +43,7 @@ export const cartEntitySlice = createSlice({ builder .addCase(addToCart.pending, state => { state.isLoading = true + state.error = null }) .addCase(addToCart.fulfilled, state => { state.isLoading = false @@ -72,6 +51,54 @@ export const cartEntitySlice = createSlice({ .addCase(addToCart.rejected, (state, { payload }) => { state.isLoading = false state.error = rejectedPayloadHandle(payload) + }), + builder + .addCase(putIncreaseProductAmount.pending, state => { + state.isLoading = true + state.error = null + }) + .addCase(putIncreaseProductAmount.fulfilled, state => { + state.isLoading = false + }) + .addCase(putIncreaseProductAmount.rejected, (state, { payload }) => { + state.isLoading = false + state.error = rejectedPayloadHandle(payload) + }), + builder + .addCase(putDecreaseProductAmount.pending, state => { + state.isLoading = true + state.error = null + }) + .addCase(putDecreaseProductAmount.fulfilled, state => { + state.isLoading = false + }) + .addCase(putDecreaseProductAmount.rejected, (state, { payload }) => { + state.isLoading = false + state.error = rejectedPayloadHandle(payload) + }), + builder + .addCase(putRenewProductAmount.pending, state => { + state.isLoading = true + state.error = null + }) + .addCase(putRenewProductAmount.fulfilled, state => { + state.isLoading = false + }) + .addCase(putRenewProductAmount.rejected, (state, { payload }) => { + state.isLoading = false + state.error = rejectedPayloadHandle(payload) + }), + builder + .addCase(putRemoveProduct.pending, state => { + state.isLoading = true + state.error = null + }) + .addCase(putRemoveProduct.fulfilled, state => { + state.isLoading = false + }) + .addCase(putRemoveProduct.rejected, (state, { payload }) => { + state.isLoading = false + state.error = rejectedPayloadHandle(payload) }) } }) diff --git a/src/entities/CartEntity/model/types/types.ts b/src/entities/CartEntity/model/types/types.ts index ffc4987d..082fb707 100644 --- a/src/entities/CartEntity/model/types/types.ts +++ b/src/entities/CartEntity/model/types/types.ts @@ -11,6 +11,7 @@ export interface ICartEntity { products: ICartProduct[] user: number cart_full_price: number + cart_full_weight: number } export interface ICartProduct { @@ -19,6 +20,7 @@ export interface ICartProduct { price: number amount: number full_price: number + full_weight: number product: IProduct } diff --git a/src/features/CartEdit/model/constants/constants.ts b/src/features/CartEdit/model/constants/constants.ts new file mode 100644 index 00000000..46f7ddaf --- /dev/null +++ b/src/features/CartEdit/model/constants/constants.ts @@ -0,0 +1,3 @@ +export const MIN_AMOUNT = 1 +export const MAX_AMOUNT = 99 +export const EMPTY = '' diff --git a/src/features/CartEdit/model/selectors.ts b/src/features/CartEdit/model/selectors.ts deleted file mode 100644 index 491c9c3e..00000000 --- a/src/features/CartEdit/model/selectors.ts +++ /dev/null @@ -1,14 +0,0 @@ -import { StateSchema } from '@/app/providers/StoreProvider' - -export const isSuccessfulRequestSelector = (state: StateSchema) => { - return ( - state.productAmount.isIncreaseSuccessful || - state.productAmount.isDecreaseSuccessful || - state.productAmount.isRenewProductAmountSuccessful || - state.productAmount.isRemoveSuccessful - ) -} - -export const getProductListSelector = (state: StateSchema) => { - return state.productAmount.productList -} diff --git a/src/features/CartEdit/model/services/putRenewProductAmount.ts b/src/features/CartEdit/model/services/putRenewProductAmount.ts deleted file mode 100644 index 49bcb33b..00000000 --- a/src/features/CartEdit/model/services/putRenewProductAmount.ts +++ /dev/null @@ -1,30 +0,0 @@ -import { createAsyncThunk } from '@reduxjs/toolkit' - -import { ThunkConfig } from '@/app/providers/StoreProvider/config/StateSchema' -import { apiErrorIdentify } from '@/shared/api/apiErrorIdentify' -import { ApiError, ApiErrorTypes, ApiRoutes } from '@/shared/api/types' -import { IProductCartList } from '@/shared/model/types/ProductCartListModel' - -import { IRenewProductAmountRequest } from '../types' - -export const putRenewProductAmount = createAsyncThunk< - IProductCartList, - IRenewProductAmountRequest, - ThunkConfig ->('cart-renew-product-amount', async (request, thunkAPI) => { - const { rejectWithValue, extra } = thunkAPI - try { - const { data } = await extra.api.put( - `api/${ApiRoutes.RENEW_PRODUCT_AMOUNT}${request.cart}/`, - { - product: request.product, - cart: request.cart, - amount: request.amount - }, - { withCredentials: true } - ) - return data - } catch (error) { - return rejectWithValue(apiErrorIdentify(error, ApiErrorTypes.DATA_EMPTY_ERROR)) - } -}) diff --git a/src/features/CartEdit/model/slice/productAmountSlice.ts b/src/features/CartEdit/model/slice/productAmountSlice.ts deleted file mode 100644 index 64059291..00000000 --- a/src/features/CartEdit/model/slice/productAmountSlice.ts +++ /dev/null @@ -1,97 +0,0 @@ -import { createSlice } from '@reduxjs/toolkit' - -import { rejectedPayloadHandle } from '@/shared/api/rejectedPayloadHandle' - -import { putDecreaseProductAmount } from '../services/putDecreaseProductAmount' -import { putIncreaseProductAmount } from '../services/putIncreaseProductAmount' -import { putRemoveProduct } from '../services/putRemoveProduct' -import { putRenewProductAmount } from '../services/putRenewProductAmount' -import { IProductAmountStateSchema } from '../types' - -const initialState: IProductAmountStateSchema = { - productList: { - amount: 0, - product: { - id: 0, - category: 'string', - brand: 'string', - images: [], - price: '', - name: '', - slug: '', - description: '', - code: 2147483647, - wb_urls: '' - }, - full_price: 0, - full_weight: 0 - }, - isIncreaseSuccessful: false, - isDecreaseSuccessful: false, - isRenewProductAmountSuccessful: false, - isRemoveSuccessful: false -} - -function resetStatuses(state: IProductAmountStateSchema) { - state.isIncreaseSuccessful = false - state.isDecreaseSuccessful = false - state.isRenewProductAmountSuccessful = false - state.isRemoveSuccessful = false -} - -export const productAmountSlice = createSlice({ - name: 'cart/product/amount', - initialState, - reducers: { - setProductList: (state, { payload }) => { - state.productList = payload - } - }, - extraReducers: builder => { - builder - .addCase(putIncreaseProductAmount.pending, state => { - resetStatuses(state) - }) - .addCase(putIncreaseProductAmount.fulfilled, state => { - state.isIncreaseSuccessful = true - }) - .addCase(putIncreaseProductAmount.rejected, (state, { payload }) => { - state.isIncreaseSuccessful = false - state.error = rejectedPayloadHandle(payload) - }) - - .addCase(putDecreaseProductAmount.pending, state => { - resetStatuses(state) - }) - .addCase(putDecreaseProductAmount.fulfilled, state => { - state.isDecreaseSuccessful = true - }) - .addCase(putDecreaseProductAmount.rejected, (state, { payload }) => { - state.isDecreaseSuccessful = false - state.error = rejectedPayloadHandle(payload) - }) - .addCase(putRenewProductAmount.pending, state => { - resetStatuses(state) - }) - .addCase(putRenewProductAmount.fulfilled, state => { - state.isRenewProductAmountSuccessful = true - }) - .addCase(putRenewProductAmount.rejected, (state, { payload }) => { - state.isRenewProductAmountSuccessful = false - state.error = rejectedPayloadHandle(payload) - }) - - .addCase(putRemoveProduct.pending, state => { - resetStatuses(state) - }) - .addCase(putRemoveProduct.fulfilled, state => { - state.isRemoveSuccessful = true - }) - .addCase(putRemoveProduct.rejected, (state, { payload }) => { - state.isRemoveSuccessful = false - state.error = rejectedPayloadHandle(payload) - }) - } -}) - -export const { actions: productAmountActions, reducer: productAmountReducer } = productAmountSlice diff --git a/src/features/CartEdit/model/types.ts b/src/features/CartEdit/model/types.ts deleted file mode 100644 index af89a951..00000000 --- a/src/features/CartEdit/model/types.ts +++ /dev/null @@ -1,16 +0,0 @@ -import { IProductCartList } from '@/shared/model/types/ProductCartListModel' - -export interface IProductAmountStateSchema { - isIncreaseSuccessful: boolean - isDecreaseSuccessful: boolean - isRemoveSuccessful: boolean - isRenewProductAmountSuccessful: boolean - productList: IProductCartList - error?: string | string[] -} - -export interface IRenewProductAmountRequest { - product: number - cart: number - amount: number -} diff --git a/src/features/CartEdit/ui/CartEdit/CartEdit.stories.tsx b/src/features/CartEdit/ui/CartEdit/CartEdit.stories.tsx index e53d62d7..890b8615 100644 --- a/src/features/CartEdit/ui/CartEdit/CartEdit.stories.tsx +++ b/src/features/CartEdit/ui/CartEdit/CartEdit.stories.tsx @@ -48,7 +48,6 @@ export const Default: Story = { }, full_price: 0, full_weight: 0 - }, - updateCart: () => {} + } } } diff --git a/src/features/CartEdit/ui/CartEdit/CartEdit.tsx b/src/features/CartEdit/ui/CartEdit/CartEdit.tsx index 2e07dc8c..14dc4f1c 100644 --- a/src/features/CartEdit/ui/CartEdit/CartEdit.tsx +++ b/src/features/CartEdit/ui/CartEdit/CartEdit.tsx @@ -1,50 +1,41 @@ -import { useEffect, useState } from 'react' -import { useSelector } from 'react-redux' +import { useState } from 'react' import ArrowIcon from '@/assets/images/cart/arrow-right.svg' +import { putDecreaseProductAmount } from '@/entities/CartEntity/model/services/putDecreaseProductAmount' +import { putIncreaseProductAmount } from '@/entities/CartEntity/model/services/putIncreaseProductAmount' +import { putRemoveProduct } from '@/entities/CartEntity/model/services/putRemoveProduct' +import { putRenewProductAmount } from '@/entities/CartEntity/model/services/putRenewProductAmount' +import { useWithFavorite } from '@/entities/Favorite/model/hooks/useWithFavorie' import { ProductEntity } from '@/entities/ProductEntity/ui/ProductEntity/ProductEntity' import { calculateProductPrice } from '@/shared/libs/helpers/calculateProductPrice' import { useAppDispatch } from '@/shared/libs/hooks/store' -import { IProductCartList } from '@/shared/model/types/ProductCartListModel' +import type { IProductCartList } from '@/shared/model/types/ProductCartListModel' import { Button } from '@/shared/ui/Button/Button' import ButtonDots from '@/shared/ui/ButtonDots/ButtonDots' import Paragraph from '@/shared/ui/Paragraph/Paragraph' import Subheading from '@/shared/ui/Subheading/Subheading' -import { isSuccessfulRequestSelector } from '../../model/selectors' -import { putDecreaseProductAmount } from '../../model/services/putDecreaseProductAmount' -import { putIncreaseProductAmount } from '../../model/services/putIncreaseProductAmount' -import { putRemoveProduct } from '../../model/services/putRemoveProduct' -import { putRenewProductAmount } from '../../model/services/putRenewProductAmount' +import { EMPTY, MAX_AMOUNT, MIN_AMOUNT } from '../../model/constants/constants' import styles from './CartEdit.module.scss' export type TCartEditProps = { cartId: number productWithInfo: IProductCartList - updateCart: () => void } /** * Компонент используется для отображения добавленных в корзину продуктов, изменения кол-ва продуктов в корзине, * для удаления продуктов из корзины, для добавления продуктов в закладки * @param {number} cartId - id корзины - * @param {IProductCartList} productList - это корзина с количеством товара, общей стоимостью и весом - * @param {function} updateCart - это функция для обновления корзины + * @param {IProductCartList} productWithInfo - это корзина с количеством товара, общей стоимостью и весом */ -export const CartEdit: React.FC = ({ - cartId, - productWithInfo, - updateCart -}: TCartEditProps) => { - const MIN_AMOUNT = 1 - const MAX_AMOUNT = 99 +export const CartEdit: React.FC = ({ cartId, productWithInfo }: TCartEditProps) => { const [needToOpenContextMenuButtonDots, setNeedToOpen] = useState(false) - const EMPTY = '' - const dispatch = useAppDispatch() - const isSuccessful: boolean = useSelector(isSuccessfulRequestSelector) + const dispatch = useAppDispatch() + const { isLiked, handleLike } = useWithFavorite(productWithInfo.product) // eslint-disable-next-line @typescript-eslint/no-unused-vars const [value, setValue] = useState(EMPTY) @@ -53,11 +44,9 @@ export const CartEdit: React.FC = ({ setNeedToOpen(false) dispatch(putRemoveProduct(productWithInfo.product.id)) } - useEffect(() => { - updateCart() - }, [isSuccessful]) function addToFavoritesHandler() { + handleLike() setNeedToOpen(false) } @@ -96,7 +85,8 @@ export const CartEdit: React.FC = ({
{' '} - {productWithInfo.amount * Number(productWithInfo.product.price)} {productWithInfo.product.brand} + {(productWithInfo.amount * Number(productWithInfo.product.price)).toFixed(2)}{' '} + {productWithInfo.product.brand} {calculateProductPrice(productWithInfo.amount, String(productWithInfo.product.price))}{' '} {productWithInfo.product.brand} {/* currency, not brand, c Number непонятно пока*/} @@ -134,7 +124,7 @@ export const CartEdit: React.FC = ({
  • diff --git a/src/features/login/model/services/loginByUsername/loginByUsername.ts b/src/features/login/model/services/loginByUsername/loginByUsername.ts index 09851ed8..4c33236b 100644 --- a/src/features/login/model/services/loginByUsername/loginByUsername.ts +++ b/src/features/login/model/services/loginByUsername/loginByUsername.ts @@ -1,6 +1,7 @@ import { createAsyncThunk } from '@reduxjs/toolkit' import { ThunkConfig } from '@/app/providers/StoreProvider/config/StateSchema' +import { getCart } from '@/entities/CartEntity/model/services/getCart' import { apiErrorIdentify } from '@/shared/api/apiErrorIdentify' import { ApiError, ApiErrorTypes, ApiRoutes } from '@/shared/api/types' import { LOCAL_STORAGE_TOKEN_KEY } from '@/shared/constants/localStorage' @@ -22,6 +23,7 @@ export const loginByUsername = createAsyncThunk>( extra.api.delToken && extra.api.delToken() $localStorageHandler.removeItem(LOCAL_STORAGE_TOKEN_KEY) + thunkAPI.dispatch(getCart()) dispatch(loginActions.userReset()) } catch (error) { if (error) { diff --git a/src/pages/CartPage/CartPage.tsx b/src/pages/CartPage/CartPage.tsx index 680e5b63..58c2c82c 100644 --- a/src/pages/CartPage/CartPage.tsx +++ b/src/pages/CartPage/CartPage.tsx @@ -1,36 +1,23 @@ -import { useEffect } from 'react' import { useSelector } from 'react-redux' import { Link } from 'react-router-dom' import WrapperForMainContent from '@/components/WrapperForMainContent/WrapperForMainContent' +import { getCartSelector } from '@/entities/CartEntity/model/selectors/selectors' +import { ICartEntity } from '@/entities/CartEntity/model/types/types' import { CartCouponApply } from '@/features/CartCouponApply/ui/CartCouponApply/CartCouponApply' import { CartEdit } from '@/features/CartEdit/ui/CartEdit/CartEdit' -import { useAppDispatch } from '@/shared/libs/hooks/store' -import { ICart } from '@/shared/model/types/CartModel' import Heading, { HeadingType } from '@/shared/ui/Heading/Heading' import Subheading from '@/shared/ui/Subheading/Subheading' import { MakeOrder } from '@/widgets/MakeOrder/ui/MakeOrder/MakeOrder' import styles from './CartPage.module.scss' -import { getCartSelector } from './model/selector' -import { getCartList } from './model/services' /** * Компонент страница корзины. На странице отображаются товары в корзине, можно изменять кол-во товаров в корзине, * сразу происходит изменение стоимости. Также можно добавить сертификат или купон на скидку, есть опция оформения быстрого и обычного заказа */ - const CartPage = () => { - const dispatch = useAppDispatch() - const cart: ICart = useSelector(getCartSelector) - - useEffect(() => { - dispatch(getCartList()) - }, []) - - function updateCart() { - dispatch(getCartList()) - } + const cart: ICartEntity = useSelector(getCartSelector) return ( @@ -52,14 +39,7 @@ const CartPage = () => {
    {cart.products.map(item => { - return ( - - ) + return })}
    diff --git a/src/pages/CartPage/model/slice.ts b/src/pages/CartPage/model/slice.ts deleted file mode 100644 index b87e37ca..00000000 --- a/src/pages/CartPage/model/slice.ts +++ /dev/null @@ -1,39 +0,0 @@ -import { createSlice } from '@reduxjs/toolkit' - -import { rejectedPayloadHandle } from '@/shared/api/rejectedPayloadHandle' - -import { getCartList } from './services' -import { ICartSchema } from './types' - -const initialState: ICartSchema = { - isLoading: false, - cart: { - id: -1, - products: [], - user: -1, - cart_full_price: 0, - cart_full_weight: 0 - } -} - -export const cartSlice = createSlice({ - name: 'cart/list', - initialState, - reducers: {}, - extraReducers: builder => { - builder - .addCase(getCartList.pending, state => { - state.isLoading = true - }) - .addCase(getCartList.fulfilled, (state, { payload }) => { - state.isLoading = false - state.cart = payload - }) - .addCase(getCartList.rejected, (state, { payload }) => { - state.isLoading = false - state.error = rejectedPayloadHandle(payload) - }) - } -}) - -export const { actions: cartActions, reducer: cartReducer } = cartSlice diff --git a/src/pages/CartPage/model/types.ts b/src/pages/CartPage/model/types.ts deleted file mode 100644 index 4e4c3ca9..00000000 --- a/src/pages/CartPage/model/types.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { ICart } from '@/shared/model/types/CartModel' - -export interface ICartSchema { - isLoading: boolean - cart: ICart - error?: string | string[] -} diff --git a/src/widgets/Header/Header.tsx b/src/widgets/Header/Header.tsx index 5537b4ea..44ab7147 100644 --- a/src/widgets/Header/Header.tsx +++ b/src/widgets/Header/Header.tsx @@ -235,7 +235,7 @@ const Header: FC = () => { total + item.amount, 0)} total={cart && cart.cart?.cart_full_price} />