borgwarehouse/hooks/useFormStatus.ts
2025-04-20 23:07:23 +02:00

32 lines
731 B
TypeScript

import { useState } from 'react';
import { Optional } from '~/types';
export function useFormStatus() {
const [isLoading, setIsLoading] = useState(false);
const [isSaved, setIsSaved] = useState(false);
const [error, setError] = useState<Optional<string>>(undefined);
const handleSuccess = () => {
setIsLoading(false);
setIsSaved(true);
setTimeout(() => setIsSaved(false), 3000);
};
const handleError = (message: string) => {
setIsLoading(false);
setError(message);
setTimeout(() => setError(undefined), 4000);
};
const clearError = () => setError(undefined);
return {
isLoading,
isSaved,
error,
setIsLoading,
handleSuccess,
handleError,
clearError,
};
}