ato-product-card
Version:
This is a test package for deployment on NPM
53 lines (43 loc) • 1.19 kB
text/typescript
import { useEffect, useRef, useState } from 'react'
import { Product, onChangeArgs, InitialValues } from '../interfaces/interfaces'
interface useProductArgs {
initialValues?: InitialValues
product: Product
value?: number
onChange?: (args: onChangeArgs) => void
}
export const useProduct = ({
initialValues,
product,
value = 0,
onChange,
}: useProductArgs) => {
const [counter, setCounter] = useState<number>(initialValues?.count || value)
const isMounted = useRef(false)
const increaseBy = (value: number) => {
let newValue = Math.max(counter + value, 0)
if (initialValues?.maxCount) {
newValue = Math.min(newValue, initialValues.maxCount)
}
setCounter(newValue)
onChange && onChange({ count: newValue, product })
}
const reset = () => {
setCounter(initialValues?.count || value)
}
useEffect(() => {
if (!isMounted.current) return
setCounter(value)
}, [value])
useEffect(() => {
isMounted.current = true
}, [])
return {
counter,
increaseBy,
reset,
isMaxCountReached:
!!initialValues?.count && initialValues.maxCount === counter,
maxCount: initialValues?.maxCount,
}
}