gml-product-cart
Version:
this is a test package to do deploy NPM
48 lines (38 loc) • 1.21 kB
text/typescript
import { useEffect, useRef, useState } from 'react';
import { InitialValues, Product, onChangeArgs } from '../interfaces';
interface useProductArgs {
product: Product;
onChange?: (args: onChangeArgs) => void;
value?: number;
initialValues?: InitialValues;
}
export const useProduct = (props: useProductArgs) => {
const { onChange, product, value = 0, initialValues } = props;
const [counter, setCounter] = useState<number>(initialValues?.count || value);
const isMounted = useRef(false);
useEffect(() => {
isMounted.current = true;
}, []);
useEffect(() => {
if (!isMounted.current) return;
setCounter(value);
}, [value]);
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);
};
return {
counter,
isMaxCountReached:
!!initialValues?.maxCount && counter === initialValues.maxCount,
maxCount: initialValues?.maxCount,
increaseBy,
reset,
};
};