docs_headless/src/content/docs/AutoSaveBase.md
A component that enables autosaving of the form. It's ideal for long data entry tasks, and reduces the risk of data loss.
<video controls autoplay playsinline muted loop> <source src="https://react-admin-ee.marmelab.com/assets/AutoSave.mp4" type="video/mp4"/> Your browser does not support the video tag. </video>This feature requires a valid Enterprise Edition subscription.
Put <AutoSaveBase> inside a form built with ra-core <Form>:
import { AutoSaveBase } from '@react-admin/ra-core-ee';
import { EditBase, Form } from 'ra-core';
import { TextInput } from 'my-react-admin-ui-library';
const PostEdit = () => (
<EditBase mutationMode="optimistic">
<Form resetOptions={{ keepDirtyValues: true }}>
<TextInput source="title" />
<TextInput source="teaser" />
<button type="submit">Save</button>
<AutoSaveBase
render={({ error, isSaving, lastSaveAt }) => {
if (error) {
return <span>Error: {error}</span>;
}
if (isSaving) {
return <span>Saving...</span>;
}
if (lastSaveAt) {
return (
<span>
Last saved at{' '}
{new Intl.DateTimeFormat(undefined, {
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
}).format(new Date(lastSaveAt))}
</span>
);
}
}}
/>
</Form>
</EditBase>
);
The app will save the current form values after 3 seconds of inactivity.
<AutoSaveBase> imposes a few limitations:
<Form resetOptions> prop to { keepDirtyValues: true }. If you forget that prop, any change entered by the end user after the autosave but before its acknowledgement by the server will be lost.<EditBase> page, you must set mutationMode to pessimistic or optimistic (<AutoSaveBase> doesn't work with the default mutationMode="undoable").<Form warnWhenUnsavedChanges> with this component. <AutoSaveBase> implements its own similar mechanism, and it's enabled by default. You can disable it with the disableWarnWhenUnsavedChanges prop.| Prop | Required | Type | Default | Description |
|---|---|---|---|---|
children | - | Element | The content to display by leveraging AutoSaveContext | |
render | - | Function | A function to render the content. | |
debounce | - | number | 3000 (3s) | The interval in milliseconds between two autosaves. |
onSuccess | - | function | A callback to call when the save request succeeds. | |
onError | - | function | A callback to call when the save request fails. | |
transform | - | function | A function to transform the data before saving. | |
disableWarnWhenUnsavedChanges | - | boolean | false | A boolean indicating whether users should be warned when they close the browser tab or navigate away from the application if they have unsaved changes. |
childrenYou can pass a children to <AutoSaveBase> and leverage its AutoSaveContext with the useAutoSaveContext hook:
import { AutoSaveBase, useAutoSaveContext } from '@react-admin/ra-core-ee';
import { EditBase, Form } from 'ra-core';
import { TextInput } from 'my-react-admin-ui-library';
const AutoSaveContent = () => {
const { error, isSaving, lastSaveAt } = useAutoSaveContext();
if (error) {
return <span>Error: {error}</span>;
}
if (isSaving) {
return <span>Saving...</span>;
}
if (lastSaveAt) {
return (
<span>
Last saved at{' '}
{new Intl.DateTimeFormat(undefined, {
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
}).format(new Date(lastSaveAt))}
</span>
);
}
return null;
}
const PostEdit = () => (
<EditBase mutationMode="optimistic">
<Form resetOptions={{ keepDirtyValues: true }}>
<TextInput source="title" />
<TextInput source="teaser" />
<button type="submit">Save</button>
<AutoSaveBase>
<AutoSaveContent />
</AutoSaveBase>
</Form>
</EditBase>
);
debounceThe interval in milliseconds between two autosaves. Defaults to 3000 (3s).
<AutoSaveBase debounce={5000} />
onSuccessA callback to call when the save request succeeds.
const [lastSave, setLastSave] = useState();
<AutoSaveBase
onSuccess={() => setLastSave(new Date())}
/>
onErrorA callback to call when the save request fails.
const [error, setError] = useState();
<AutoSaveBase
onError={error => setError(error)}
/>
transformA function to transform the data before saving.
<AutoSaveBase
transform={data => ({
...data,
fullName: `${data.firstName} ${data.lastName}`
})}
/>
disableWarnWhenUnsavedChangesA boolean indicating whether users should be warned when they close the browser tab or navigate away from the application if they have unsaved changes.
<AutoSaveBase disableWarnWhenUnsavedChanges />
renderYou can pass a render prop instead of children to render a UI for the auto save feature:
import { AutoSaveBase } from '@react-admin/ra-core-ee';
const AutoSave = () => (
<AutoSaveBase
render={({ error, isSaving, lastSaveAt }) => {
if (error) {
return <span>Error: {error}</span>;
}
if (isSaving) {
return <span>Saving...</span>;
}
if (lastSaveAt) {
return (
<span>
Last saved at{' '}
{new Intl.DateTimeFormat(undefined, {
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
}).format(new Date(lastSaveAt))}
</span>
);
}
}}
/>
);