fix: info page editor — locale switch no longer resets user edits

Split the form population effect into two:
1. Form fields (slug, icon, active, titles, contents) — populated
   once when pageData loads, never re-run on locale switch
2. Editor content — set once when editor instance is ready

Previously activeLocale was in the deps array, causing every tab
switch to overwrite all in-memory edits with original server data.
This commit is contained in:
Fringg
2026-04-24 08:22:16 +03:00
parent 7d6d0ba344
commit 3ef54adb3e

View File

@@ -411,10 +411,11 @@ export default function AdminInfoPageEditor() {
refetchOnWindowFocus: false, refetchOnWindowFocus: false,
}); });
// Populate form when page data loads // Populate form when page data loads (once only — not on locale switch)
const editorPopulated = useRef(false); const editorPopulated = useRef(false);
const formPopulated = useRef(false);
useEffect(() => { useEffect(() => {
if (!pageData) return; if (!pageData || formPopulated.current) return;
setSlug(pageData.slug); setSlug(pageData.slug);
setSlugManuallyEdited(true); setSlugManuallyEdited(true);
setIcon(pageData.icon ?? ''); setIcon(pageData.icon ?? '');
@@ -422,11 +423,16 @@ export default function AdminInfoPageEditor() {
setSortOrder(pageData.sort_order); setSortOrder(pageData.sort_order);
setTitles(pageData.title); setTitles(pageData.title);
setContents(pageData.content); setContents(pageData.content);
if (editor && pageData.content[activeLocale] && !editorPopulated.current) { formPopulated.current = true;
editor.commands.setContent(pageData.content[activeLocale] ?? ''); }, [pageData]);
editorPopulated.current = true;
} // Set editor content once when editor is ready
}, [pageData, editor, activeLocale]); useEffect(() => {
if (!pageData || !editor || editorPopulated.current) return;
const initialContent = pageData.content[activeLocale] ?? pageData.content['ru'] ?? '';
editor.commands.setContent(initialContent);
editorPopulated.current = true;
}, [pageData, editor]); // activeLocale intentionally omitted
// Auto-generate slug from Russian title // Auto-generate slug from Russian title
useEffect(() => { useEffect(() => {