From b5bfa1f7acf8bba83a7fbf71253d2b2cfde4f6f6 Mon Sep 17 00:00:00 2001 From: Joshua Ryder Date: Mon, 8 Dec 2025 15:40:51 -0500 Subject: [PATCH] Favorites section default fix --- frontend/src/components/SectionsManager.tsx | 44 ++++++++++----------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/frontend/src/components/SectionsManager.tsx b/frontend/src/components/SectionsManager.tsx index 9b8fee36..a4bdf14d 100644 --- a/frontend/src/components/SectionsManager.tsx +++ b/frontend/src/components/SectionsManager.tsx @@ -104,20 +104,20 @@ const SectionsManager: React.FC = ({ } }; - const setDefaultSection = async (id: number) => { + const toggleDefaultSection = async (id: number, currentlyDefault: boolean) => { try { const response = await fetch(`/api/sections/${id}`, { method: 'PUT', headers: { 'Content-Type': 'application/json' }, credentials: 'include', - body: JSON.stringify({ is_default: true }) + body: JSON.stringify({ is_default: !currentlyDefault }) }); if (response.ok) { onSectionChange(); } } catch (err) { - setError('Failed to set default section'); + setError('Failed to update default section'); } }; @@ -139,6 +139,9 @@ const SectionsManager: React.FC = ({ const sectionIds = newSections.map(s => s.id); + // Store original sections for potential revert (avoid stale closure) + const originalSections = localSections; + try { const response = await fetch('/api/sections/reorder', { method: 'PUT', @@ -147,16 +150,16 @@ const SectionsManager: React.FC = ({ body: JSON.stringify({ sectionIds }) }); - if (response.ok) { - onSectionChange(); - } else { + if (!response.ok) { // Revert on failure - setLocalSections(localSections); + setLocalSections(originalSections); setError('Failed to reorder sections'); } + // On success, don't call onSectionChange - local state is already correct + // This avoids race condition where parent refetch overwrites optimistic update } catch (err) { // Revert on failure - setLocalSections(localSections); + setLocalSections(originalSections); setError('Failed to reorder sections'); } }; @@ -274,11 +277,6 @@ const SectionsManager: React.FC = ({ {section.name} - {!!section.is_default && ( - - default - - )}
- {!section.is_default && ( - - )} +