Skip to content

Commit

Permalink
Merge pull request #380 from Studio-Yandex-Practicum/bug-378-on-Cart-…
Browse files Browse the repository at this point in the history
…Page

#378-bug-fix
  • Loading branch information
JuliaAvramenko authored May 16, 2024
2 parents 68afeaa + 2f4af37 commit 7aeeb9f
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 24 deletions.
1 change: 1 addition & 0 deletions src/features/CartEdit/model/selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export const isSuccessfulRequestSelector = (state: StateSchema) => {
return (
state.productAmount.isIncreaseSuccessful ||
state.productAmount.isDecreaseSuccessful ||
state.productAmount.isRenewProductAmountSuccessful ||
state.productAmount.isRemoveSuccessful
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const putRenewProductAmount = createAsyncThunk<
{
product: request.product,
cart: request.cart,
amount: (request.amount === 0 && 1) || request.amount
amount: request.amount
},
{ withCredentials: true }
)
Expand Down
30 changes: 13 additions & 17 deletions src/features/CartEdit/model/slice/productAmountSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,15 @@ const initialState: IProductAmountStateSchema = {
},
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({
Expand Down Expand Up @@ -69,31 +70,26 @@ export const productAmountSlice = createSlice({
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 => {
.addCase(putRemoveProduct.rejected, (state, { payload }) => {
state.isRemoveSuccessful = false

.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
9 changes: 3 additions & 6 deletions src/features/CartEdit/ui/CartEdit/CartEdit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ import { putDecreaseProductAmount } from '../../model/services/putDecreaseProduc
import { putIncreaseProductAmount } from '../../model/services/putIncreaseProductAmount'
import { putRemoveProduct } from '../../model/services/putRemoveProduct'
import { putRenewProductAmount } from '../../model/services/putRenewProductAmount'
import { productAmountActions } from '../../model/slice/productAmountSlice'


import styles from './CartEdit.module.scss'

Expand All @@ -36,7 +34,6 @@ export type TCartEditProps = {
*/

export const CartEdit: React.FC<TCartEditProps> = ({
// eslint-disable-next-line @typescript-eslint/no-unused-vars
cartId,
productWithInfo,
updateCart
Expand All @@ -49,6 +46,7 @@ export const CartEdit: React.FC<TCartEditProps> = ({

const isSuccessful: boolean = useSelector(isSuccessfulRequestSelector)

// eslint-disable-next-line @typescript-eslint/no-unused-vars
const [value, setValue] = useState<string>(EMPTY)

function deleteProductHandler() {
Expand Down Expand Up @@ -77,11 +75,10 @@ export const CartEdit: React.FC<TCartEditProps> = ({

function setAmountHandler(e: React.ChangeEvent<HTMLInputElement>) {
const newValue = Number(e.target.value)

if (Number.isInteger(newValue) && newValue > 0) {
if (Number.isInteger(newValue)) {
dispatch(
putRenewProductAmount({
product: productList.product.id,
product: productWithInfo.product.id,
cart: cartId,
amount: newValue
})
Expand Down

0 comments on commit 7aeeb9f

Please sign in to comment.