Skip to content

Commit

Permalink
Merge pull request #343 from Studio-Yandex-Practicum/api-316-renew-pr…
Browse files Browse the repository at this point in the history
…oduct-amount

#316-api-renew-product-amount
  • Loading branch information
JuliaAvramenko authored May 13, 2024
2 parents 6078c85 + d776f87 commit cdacfef
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/entities/ProductEntity/ui/ProductEntity/ProductEntity.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ export const ProductEntity: FC<IProduct> = product => {
return (
<div className={`${styles.description}`}>
<div className={`${styles.frame}`}>
{product.images.length > 0 && (
{(product.images.length > 0 && (
<img src={product.images[0].image} alt={'product'} className={styles.image} />
)}
)) || <></>}
</div>
<div className={`${styles.description_wrapper}`}>
<Subheading>{product.id}</Subheading>
Expand Down
30 changes: 30 additions & 0 deletions src/features/CartEdit/model/services/putRenewProductAmount.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
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<ApiError>
>('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 === 0 && 1) || request.amount
},
{ withCredentials: true }
)
return data
} catch (error) {
return rejectWithValue(apiErrorIdentify(error, ApiErrorTypes.DATA_EMPTY_ERROR))
}
})
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 @@ -4,6 +4,7 @@ import { rejectedPayloadHandle } from '@/shared/api/rejectedPayloadHandle'

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

const initialState: IProductAmountStateSchema = {
Expand All @@ -25,7 +26,8 @@ const initialState: IProductAmountStateSchema = {
full_price: 0,
full_weight: 0
},
isDecreaseSuccessful: false
isDecreaseSuccessful: false,
isRenewProductAmountSuccessful: false
}

export const productAmountSlice = createSlice({
Expand All @@ -40,7 +42,6 @@ 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 @@ -62,6 +63,21 @@ export const productAmountSlice = createSlice({
state.isDecreaseSuccessful = false
state.error = rejectedPayloadHandle(payload)
})

.addCase(putRenewProductAmount.pending, state => {
state.isRenewProductAmountSuccessful = false
})
.addCase(putRenewProductAmount.fulfilled, (state, { payload }) => {
state.isRenewProductAmountSuccessful = true
state.productList = {
...state.productList,
amount: payload.amount
}
})
.addCase(putRenewProductAmount.rejected, (state, { payload }) => {
state.isRenewProductAmountSuccessful = false
state.error = rejectedPayloadHandle(payload)
})
}
})

Expand Down
7 changes: 7 additions & 0 deletions src/features/CartEdit/model/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ import { IProductCartList } from '@/shared/model/types/ProductCartListModel'
export interface IProductAmountStateSchema {
isIncreaseSuccessful: boolean
isDecreaseSuccessful: boolean
isRenewProductAmountSuccessful: boolean
productList: IProductCartList
error?: string | string[]
}

export interface IRenewProductAmountRequest {
product: number
cart: number
amount: number
}
31 changes: 27 additions & 4 deletions src/features/CartEdit/ui/CartEdit/CartEdit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ 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 { putRenewProductAmount } from '../../model/services/putRenewProductAmount'
import { productAmountActions } from '../../model/slice/productAmountSlice'

import styles from './CartEdit.module.scss'
Expand All @@ -32,10 +33,13 @@ export type TCartEditProps = {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
export const CartEdit: React.FC<TCartEditProps> = ({ cartId, productList }: TCartEditProps) => {
const [needToOpenContextMenuButtonDots, setNeedToOpen] = useState(false)
const EMPTY = ''
const dispatch = useAppDispatch()

const productListState: IProductCartList = useSelector(getProductListSelector)

const [value, setValue] = useState<string>(EMPTY)

function deleteProductHandler() {
setNeedToOpen(false)
// removeProduct(product.id) переделать на вызов action https://github.com/Studio-Yandex-Practicum/maxboom_frontend/issues/319
Expand All @@ -50,17 +54,36 @@ 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
}

function setAmountHandler() {
//tbd https://github.com/Studio-Yandex-Practicum/maxboom_frontend/issues/316
function setAmountHandler(e: React.ChangeEvent<HTMLInputElement>) {
const newValue = Number(e.target.value)

if (Number.isInteger(newValue) && newValue > 0) {
dispatch(
putRenewProductAmount({
product: productList.product.id,
cart: cartId,
amount: newValue
})
)
} else {
setValue(EMPTY)
}
}

useEffect(() => {
dispatch(productAmountActions.setProductList(productList))
}, [productList])

useEffect(() => {
if (productListState.amount === 0) {
setValue(EMPTY)
} else {
setValue(String(productListState.amount))
}
}, [productListState.amount])

return (
<>
<div className={styles.container}>
Expand Down Expand Up @@ -88,7 +111,7 @@ export const CartEdit: React.FC<TCartEditProps> = ({ cartId, productList }: TCar
<ArrowIcon className={styles.arrowIcon} />
</Button>
<input
value={productListState.amount}
value={value}
min="1"
max="99"
type="text"
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 @@ -14,7 +14,8 @@ export enum ApiRoutes {
PRODUCT = 'catalogue',
CART_LIST = 'cart',
INCREASE_PRODUCT_AMOUNT = 'cart/add/',
DECREASE_PRODUCT_AMOUNT = 'cart/subtract/'
DECREASE_PRODUCT_AMOUNT = 'cart/subtract/',
RENEW_PRODUCT_AMOUNT = 'cart/'
}

export enum ApiErrorTypes {
Expand Down

0 comments on commit cdacfef

Please sign in to comment.