fix: prevent auto-save from overwriting survey status during publish

The 10-second auto-save interval for draft surveys only checked
isSurveySaving before firing, but the publish flow sets
isSurveyPublishing instead. This allowed auto-save to race with
publish — sending status: "draft" concurrently with status:
"inProgress". If the auto-save write committed after the publish
write, the survey reverted to draft while the user was already
redirected to the summary page.

Add isSurveyPublishingRef so the auto-save interval also skips
when a publish is in progress.
This commit is contained in:
Dhruwang
2026-03-30 10:24:49 +05:30
parent 01f765e969
commit d875601990

View File

@@ -72,6 +72,7 @@ export const SurveyMenuBar = ({
const [lastAutoSaved, setLastAutoSaved] = useState<Date | null>(null);
const isSuccessfullySavedRef = useRef(false);
const isAutoSavingRef = useRef(false);
const isSurveyPublishingRef = useRef(false);
// Refs for interval-based auto-save (to access current values without re-creating interval)
const localSurveyRef = useRef(localSurvey);
@@ -101,6 +102,10 @@ export const SurveyMenuBar = ({
isSurveySavingRef.current = isSurveySaving;
}, [isSurveySaving]);
useEffect(() => {
isSurveyPublishingRef.current = isSurveyPublishing;
}, [isSurveyPublishing]);
// Reset the successfully saved flag when survey prop updates (page refresh complete)
useEffect(() => {
if (isSuccessfullySavedRef.current) {
@@ -269,8 +274,8 @@ export const SurveyMenuBar = ({
// Skip if tab is not visible (no computation, no API calls for background tabs)
if (document.hidden) return;
// Skip if already saving (manual or auto)
if (isAutoSavingRef.current || isSurveySavingRef.current) return;
// Skip if already saving, publishing, or auto-saving
if (isAutoSavingRef.current || isSurveySavingRef.current || isSurveyPublishingRef.current) return;
// Check for changes using refs (avoids re-creating interval on every change)
const { updatedAt: localUpdatedAt, ...localSurveyRest } = localSurveyRef.current;