@pagamio/frontend-commons-lib
Version:
Pagamio library for Frontend reusable components like the form engine and table container
47 lines (46 loc) • 1.52 kB
JavaScript
import { useCallback } from 'react';
import { useMultiFormEngineDrawer } from '../../context/MultiFormEngineDrawerProvider';
/**
* Hook for form persistence functionality
* Provides methods to save, restore, and clear form data
*/
export const useFormPersistence = (formKey) => {
const { getFormData, setFormData, clearFormData, hasFormData } = useMultiFormEngineDrawer();
const saveFormData = useCallback((data, isDirty = true) => {
if (!formKey)
return;
setFormData(formKey, {
values: data,
lastUpdated: Date.now(),
isDirty,
});
}, [formKey, setFormData]);
const restoreFormData = useCallback(() => {
if (!formKey)
return null;
const persistedData = getFormData(formKey);
return persistedData?.values || null;
}, [formKey, getFormData]);
const clearPersistedData = useCallback(() => {
if (!formKey)
return;
clearFormData(formKey);
}, [formKey, clearFormData]);
const hasPersistedData = useCallback(() => {
if (!formKey)
return false;
return hasFormData(formKey);
}, [formKey, hasFormData]);
const getPersistedDataInfo = useCallback(() => {
if (!formKey)
return null;
return getFormData(formKey);
}, [formKey, getFormData]);
return {
saveFormData,
restoreFormData,
clearPersistedData,
hasPersistedData,
getPersistedDataInfo,
};
};