Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Api 318 decrease product amount #332

Merged
merged 3 commits into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { type FC } from 'react'

import { IProduct } from '@/models/ProductModel'
import { IProduct } from '@/shared/model/types/ProductModel'
import Subheading from '@/shared/ui/Subheading/Subheading'

import styles from './ProductEntity.module.scss'
Expand Down
23 changes: 23 additions & 0 deletions src/features/CartEdit/model/services/putDecreaseProductAmount.ts
Original file line number Diff line number Diff line change
@@ -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 { IProductCartList } from '@/shared/model/types/ProductCartListModel'

export const putDecreaseProductAmount = createAsyncThunk<IProductCartList, number, ThunkConfig<ApiError>>(
'cart-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 }
)
return data
} catch (error) {
return rejectWithValue(apiErrorIdentify(error, ApiErrorTypes.DATA_EMPTY_ERROR))
}
}
)
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { createAsyncThunk } from '@reduxjs/toolkit'

import { ThunkConfig } from '@/app/providers/StoreProvider/config/StateSchema'
import { IProductCartList } from '@/models/ProductCartListModel'
import { apiErrorIdentify } from '@/shared/api/apiErrorIdentify'
import { ApiError, ApiErrorTypes, ApiRoutes } from '@/shared/api/types'
import { IProductCartList } from '@/shared/model/types/ProductCartListModel'

export const putIncreaseProductAmount = createAsyncThunk<IProductCartList, number, ThunkConfig<ApiError>>(
'cart-increase-product-amount',
Expand Down
20 changes: 18 additions & 2 deletions src/features/CartEdit/model/slice/productAmountSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { createSlice } from '@reduxjs/toolkit'

import { rejectedPayloadHandle } from '@/shared/api/rejectedPayloadHandle'

import { putDecreaseProductAmount } from '../services/putDecreaseProductAmount'
import { putIncreaseProductAmount } from '../services/putIncreaseProductAmount'
import { IProductAmountStateSchema } from '../types'

Expand All @@ -21,8 +22,10 @@ const initialState: IProductAmountStateSchema = {
code: 2147483647,
wb_urls: ''
},
full_price: 0
}
full_price: 0,
full_weight: 0
},
isDecreaseSuccessful: false
}

export const productAmountSlice = createSlice({
Expand All @@ -37,6 +40,7 @@ export const productAmountSlice = createSlice({
builder
.addCase(putIncreaseProductAmount.pending, state => {
state.isIncreaseSuccessful = false
state.isDecreaseSuccessful = false
})
.addCase(putIncreaseProductAmount.fulfilled, (state, { payload }) => {
state.isIncreaseSuccessful = true
Expand All @@ -46,6 +50,18 @@ export const productAmountSlice = createSlice({
state.isIncreaseSuccessful = false
state.error = rejectedPayloadHandle(payload)
})

.addCase(putDecreaseProductAmount.pending, state => {
state.isDecreaseSuccessful = false
})
.addCase(putDecreaseProductAmount.fulfilled, (state, { payload }) => {
state.isDecreaseSuccessful = true
state.productList = payload
})
.addCase(putDecreaseProductAmount.rejected, (state, { payload }) => {
state.isDecreaseSuccessful = false
state.error = rejectedPayloadHandle(payload)
})
}
})

Expand Down
3 changes: 2 additions & 1 deletion src/features/CartEdit/model/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { IProductCartList } from '@/models/ProductCartListModel'
import { IProductCartList } from '@/shared/model/types/ProductCartListModel'

export interface IProductAmountStateSchema {
isIncreaseSuccessful: boolean
isDecreaseSuccessful: boolean
productList: IProductCartList
error?: string | string[]
}
3 changes: 2 additions & 1 deletion src/features/CartEdit/ui/CartEdit/CartEdit.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ export const Default: Story = {
code: 108290,
wb_urls: 'jnfne'
},
full_price: 0
full_price: 0,
full_weight: 0
}
}
}
4 changes: 3 additions & 1 deletion src/features/CartEdit/ui/CartEdit/CartEdit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ import { useSelector } from 'react-redux'

import ArrowIcon from '@/assets/images/cart/arrow-right.svg'
import { ProductEntity } from '@/entities/ProductEntity/ui/ProductEntity/ProductEntity'
import { IProductCartList } from '@/models/ProductCartListModel'
import { useAppDispatch } from '@/shared/libs/hooks/store'
import { 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 { getProductListSelector } from '../../model/selectors'
import { putDecreaseProductAmount } from '../../model/services/putDecreaseProductAmount'
import { putIncreaseProductAmount } from '../../model/services/putIncreaseProductAmount'
import { productAmountActions } from '../../model/slice/productAmountSlice'

Expand Down Expand Up @@ -48,6 +49,7 @@ export const CartEdit: React.FC<TCartEditProps> = ({ cartId, productList }: TCar
}

function decreaseAmountHandler() {
dispatch(putDecreaseProductAmount(productListState.product.id))
// tbd https://github.com/Studio-Yandex-Practicum/maxboom_frontend/issues/318
JuliaAvramenko marked this conversation as resolved.
Show resolved Hide resolved
}

Expand Down
5 changes: 3 additions & 2 deletions src/pages/CartPage/CartPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import { Link } from 'react-router-dom'
import WrapperForMainContent from '@/components/WrapperForMainContent/WrapperForMainContent'
import { CartCouponApply } from '@/features/CartCouponApply/ui/CartCouponApply/CartCouponApply'
import { CartEdit } from '@/features/CartEdit/ui/CartEdit/CartEdit'
import { ICart } from '@/models/CartModel'
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'
Expand All @@ -32,7 +32,8 @@ const CartPage = () => {
<WrapperForMainContent>
<div className={styles.titles}>
<Heading>
{/* Оформление заказа ({cart.weight.toFixed(2)} {cart.unit}) Вес не приходит с бэка, tbd */}
Оформление заказа ({cart.cart_full_weight.toFixed(2)} кг)
{/* Кг приходит с бека или нет, tbd */}
</Heading>
<Subheading>
<Link to={'/'} className={styles.link}>
Expand Down
2 changes: 1 addition & 1 deletion src/pages/CartPage/model/services.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { createAsyncThunk } from '@reduxjs/toolkit'

import { ThunkConfig } from '@/app/providers/StoreProvider/config/StateSchema'
import { ICart } from '@/models/CartModel'
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<ICart, void, ThunkConfig<ApiError>>(
'cart',
Expand Down
3 changes: 2 additions & 1 deletion src/pages/CartPage/model/slice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ const initialState: ICartSchema = {
id: -1,
products: [],
user: -1,
cart_full_price: 0
cart_full_price: 0,
cart_full_weight: 0
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/pages/CartPage/model/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ICart } from '@/models/CartModel'
import { ICart } from '@/shared/model/types/CartModel'

export interface ICartSchema {
isLoading: boolean
Expand Down
3 changes: 2 additions & 1 deletion src/shared/api/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ export enum ApiRoutes {
STORIES = 'stories',
PRODUCT = 'catalogue',
CART_LIST = 'cart',
INCREASE_PRODUCT_AMOUNT = 'cart/add/'
INCREASE_PRODUCT_AMOUNT = 'cart/add/',
DECREASE_PRODUCT_AMOUNT = 'cart/subtract/'
}

export enum ApiErrorTypes {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ export interface ICart {
products: IProductCartList[]
user: number
cart_full_price: number
cart_full_weight: number
JuliaAvramenko marked this conversation as resolved.
Show resolved Hide resolved
}
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ export interface IProductCartList {
amount: number
product: IProduct
full_price: number
full_weight: number
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ export interface IProduct {
wholesale?: number
label_hit?: boolean
label_popular?: boolean
weight?: string
JuliaAvramenko marked this conversation as resolved.
Show resolved Hide resolved
}
Loading