Favorites section default fix
This commit is contained in:
@@ -104,20 +104,20 @@ const SectionsManager: React.FC<SectionsManagerProps> = ({
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const setDefaultSection = async (id: number) => {
|
const toggleDefaultSection = async (id: number, currentlyDefault: boolean) => {
|
||||||
try {
|
try {
|
||||||
const response = await fetch(`/api/sections/${id}`, {
|
const response = await fetch(`/api/sections/${id}`, {
|
||||||
method: 'PUT',
|
method: 'PUT',
|
||||||
headers: { 'Content-Type': 'application/json' },
|
headers: { 'Content-Type': 'application/json' },
|
||||||
credentials: 'include',
|
credentials: 'include',
|
||||||
body: JSON.stringify({ is_default: true })
|
body: JSON.stringify({ is_default: !currentlyDefault })
|
||||||
});
|
});
|
||||||
|
|
||||||
if (response.ok) {
|
if (response.ok) {
|
||||||
onSectionChange();
|
onSectionChange();
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
setError('Failed to set default section');
|
setError('Failed to update default section');
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -139,6 +139,9 @@ const SectionsManager: React.FC<SectionsManagerProps> = ({
|
|||||||
|
|
||||||
const sectionIds = newSections.map(s => s.id);
|
const sectionIds = newSections.map(s => s.id);
|
||||||
|
|
||||||
|
// Store original sections for potential revert (avoid stale closure)
|
||||||
|
const originalSections = localSections;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const response = await fetch('/api/sections/reorder', {
|
const response = await fetch('/api/sections/reorder', {
|
||||||
method: 'PUT',
|
method: 'PUT',
|
||||||
@@ -147,16 +150,16 @@ const SectionsManager: React.FC<SectionsManagerProps> = ({
|
|||||||
body: JSON.stringify({ sectionIds })
|
body: JSON.stringify({ sectionIds })
|
||||||
});
|
});
|
||||||
|
|
||||||
if (response.ok) {
|
if (!response.ok) {
|
||||||
onSectionChange();
|
|
||||||
} else {
|
|
||||||
// Revert on failure
|
// Revert on failure
|
||||||
setLocalSections(localSections);
|
setLocalSections(originalSections);
|
||||||
setError('Failed to reorder sections');
|
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) {
|
} catch (err) {
|
||||||
// Revert on failure
|
// Revert on failure
|
||||||
setLocalSections(localSections);
|
setLocalSections(originalSections);
|
||||||
setError('Failed to reorder sections');
|
setError('Failed to reorder sections');
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -274,11 +277,6 @@ const SectionsManager: React.FC<SectionsManagerProps> = ({
|
|||||||
<span className="flex-1 text-sm text-gray-900 dark:text-gray-100">
|
<span className="flex-1 text-sm text-gray-900 dark:text-gray-100">
|
||||||
{section.name}
|
{section.name}
|
||||||
</span>
|
</span>
|
||||||
{!!section.is_default && (
|
|
||||||
<span className="text-xs bg-yellow-100 dark:bg-yellow-900 text-yellow-700 dark:text-yellow-300 px-2 py-0.5 rounded">
|
|
||||||
default
|
|
||||||
</span>
|
|
||||||
)}
|
|
||||||
<div className="flex items-center gap-1">
|
<div className="flex items-center gap-1">
|
||||||
<button
|
<button
|
||||||
onClick={() => moveSection(section.id, 'up')}
|
onClick={() => moveSection(section.id, 'up')}
|
||||||
@@ -296,15 +294,17 @@ const SectionsManager: React.FC<SectionsManagerProps> = ({
|
|||||||
>
|
>
|
||||||
<ChevronDown className="h-4 w-4" />
|
<ChevronDown className="h-4 w-4" />
|
||||||
</button>
|
</button>
|
||||||
{!section.is_default && (
|
<button
|
||||||
<button
|
onClick={() => toggleDefaultSection(section.id, !!section.is_default)}
|
||||||
onClick={() => setDefaultSection(section.id)}
|
className="p-1 hover:bg-gray-200 dark:hover:bg-gray-600 rounded"
|
||||||
className="p-1 text-gray-400 hover:text-yellow-500"
|
title={section.is_default ? "Remove as default" : "Set as default"}
|
||||||
title="Set as default"
|
>
|
||||||
>
|
<Star className={`h-4 w-4 ${
|
||||||
<Star className="h-4 w-4" />
|
section.is_default
|
||||||
</button>
|
? 'text-yellow-500 fill-yellow-500'
|
||||||
)}
|
: 'text-gray-400 hover:text-yellow-500'
|
||||||
|
}`} />
|
||||||
|
</button>
|
||||||
<button
|
<button
|
||||||
onClick={() => startEditing(section)}
|
onClick={() => startEditing(section)}
|
||||||
className="p-1 text-gray-400 hover:text-blue-500"
|
className="p-1 text-gray-400 hover:text-blue-500"
|
||||||
|
|||||||
Reference in New Issue
Block a user