/
lizardking
/
react_final
Обзор
Документация
Войти
/
lizardking
/
react_final
Код
Запросы
1
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
main
src/features/cart/hooks/useCount.ts
33 строки
1 KB
LizardKing
Рефакторинг на FSD
24 ноя 2025, 16:05
24 ноя 2025, 16:05
eedf5c5
Код
Авторство
О чём код?
import type { CartProduct } from 'entities/cart' import type { ChangeEvent } from 'react' import { useAppDispatch, useAppSelector } from 'shared/store/utils' import { cartActions, cartSelectors } from '../slice/cart' const MIN_COUNT = 1 const MAX_COUNT = 99 export const useCount = (productId: string) => { const dispatch = useAppDispatch() const products = useAppSelector(cartSelectors.getCartProducts) const product = products.find((p) => p.id === productId) as CartProduct const { id, count, stock } = product const handleIncrement = () => { const newCount = count + 1 const validCount = newCount > MAX_COUNT ? MAX_COUNT : newCount dispatch(cartActions.setCartProductCount({ id, count: validCount })) } const handleDecrement = () => { const newCount = count - 1 const validCount = newCount < MIN_COUNT ? MIN_COUNT : newCount dispatch(cartActions.setCartProductCount({ id, count: validCount })) } const handleSetCount = (e: ChangeEvent<HTMLInputElement>) => { const newCount = +e.target.value const validCount = newCount > MAX_COUNT ? MAX_COUNT : newCount < MIN_COUNT ? MIN_COUNT : newCount dispatch(cartActions.setCartProductCount({ id, count: validCount })) } return { count, stock, handleSetCount, handleIncrement, handleDecrement } }