fix: #1748, bails out of autosave if doc is published while autosaving
This commit is contained in:
@@ -31,12 +31,18 @@ const Autosave: React.FC<Props> = ({ collection, global, id, publishedDocUpdated
|
||||
const [lastSaved, setLastSaved] = useState<number>();
|
||||
const debouncedFields = useDebounce(fields, interval);
|
||||
const fieldRef = useRef(fields);
|
||||
const modifiedRef = useRef(modified);
|
||||
|
||||
// Store fields in ref so the autosave func
|
||||
// can always retrieve the most to date copies
|
||||
// after the timeout has executed
|
||||
fieldRef.current = fields;
|
||||
|
||||
// Store modified in ref so the autosave func
|
||||
// can bail out if modified becomes false while
|
||||
// timing out during autosave
|
||||
modifiedRef.current = modified;
|
||||
|
||||
const createCollectionDoc = useCallback(async () => {
|
||||
const res = await fetch(`${serverURL}${api}/${collection.slug}?locale=${locale}&fallback-locale=null&depth=0&draft=true`, {
|
||||
method: 'POST',
|
||||
@@ -95,29 +101,31 @@ const Autosave: React.FC<Props> = ({ collection, global, id, publishedDocUpdated
|
||||
};
|
||||
|
||||
setTimeout(async () => {
|
||||
const res = await fetch(url, {
|
||||
method,
|
||||
credentials: 'include',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Accept-Language': i18n.language,
|
||||
},
|
||||
body: JSON.stringify(body),
|
||||
});
|
||||
if (modifiedRef.current) {
|
||||
const res = await fetch(url, {
|
||||
method,
|
||||
credentials: 'include',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Accept-Language': i18n.language,
|
||||
},
|
||||
body: JSON.stringify(body),
|
||||
});
|
||||
|
||||
if (res.status === 200) {
|
||||
setLastSaved(new Date().getTime());
|
||||
getVersions();
|
||||
}
|
||||
}
|
||||
|
||||
setSaving(false);
|
||||
|
||||
if (res.status === 200) {
|
||||
setLastSaved(new Date().getTime());
|
||||
getVersions();
|
||||
}
|
||||
}, 1000);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
autosave();
|
||||
}, [i18n, debouncedFields, modified, serverURL, api, collection, global, id, getVersions, locale]);
|
||||
}, [i18n, debouncedFields, modified, serverURL, api, collection, global, id, getVersions, locale, modifiedRef]);
|
||||
|
||||
useEffect(() => {
|
||||
if (versions?.docs?.[0]) {
|
||||
|
||||
Reference in New Issue
Block a user