llqd-product-card
Version:
Este es un paquete de pruebas de despliegue en NPM
56 lines (40 loc) • 1.43 kB
text/typescript
import { useState, useEffect, useRef } from 'react';
import { Product, onChangeArgs, InitialValues } from '../interfaces/interfaces';
interface useProductArgs {
product: Product,
onChange?: ( args: onChangeArgs ) => void,
value?: number,
initialValues?: InitialValues
}
export const useProduct = ( { onChange, product, value = 0, initialValues }: useProductArgs) => {
const [counter, setCounter] = useState<number>( initialValues?.count || value );
const isMounted = useRef(false);
const increaseBy = ( value: number ) => {
const newValue = Math.max( counter + value, 0 );
if ( initialValues?.maxCount ) {
if( newValue <= initialValues.maxCount ) {
setCounter( newValue );
} else {
return
}
}
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,
isMaxCountReached: !!initialValues?.count && initialValues?.maxCount === counter,
maxCount: initialValues?.maxCount,
increaseBy,
reset
}
}