fix: reset editor state on section change to ensure proper template editing

This commit is contained in:
seaznCode 2026-01-28 20:14:14 +01:00
parent 40a01ef665
commit fb623b26a2

View File

@ -21,6 +21,7 @@ export default function ContractManagementPage() {
const router = useRouter();
const [section, setSection] = useState('templates');
const [editingTemplateId, setEditingTemplateId] = useState<string | null>(null);
const [editorKey, setEditorKey] = useState(0);
useEffect(() => { setMounted(true); }, []);
@ -56,7 +57,17 @@ export default function ContractManagementPage() {
{NAV.map((item) => (
<button
key={item.key}
onClick={() => setSection(item.key)}
onClick={() => {
if (section === 'editor' && item.key !== 'editor') {
setEditingTemplateId(null);
setEditorKey((k) => k + 1);
}
if (item.key === 'editor') {
setEditingTemplateId(null);
setEditorKey((k) => k + 1);
}
setSection(item.key);
}}
className={`flex flex-shrink-0 items-center gap-2 px-4 py-2 rounded-lg font-medium transition whitespace-nowrap text-sm md:text-base
${section === item.key
? 'bg-blue-900 text-blue-50 shadow'
@ -99,6 +110,7 @@ export default function ContractManagementPage() {
refreshKey={refreshKey}
onEdit={(id) => {
setEditingTemplateId(id);
setEditorKey((k) => k + 1);
setSection('editor');
}}
/>
@ -111,15 +123,18 @@ export default function ContractManagementPage() {
Create Template
</h2>
<ContractEditor
key={`${editorKey}-${editingTemplateId ?? 'new'}`}
editingTemplateId={editingTemplateId}
onCancelEdit={() => {
setEditingTemplateId(null);
setEditorKey((k) => k + 1);
setSection('templates');
}}
onSaved={(info) => {
bumpRefresh();
if (info?.action === 'revised') {
setEditingTemplateId(null);
setEditorKey((k) => k + 1);
setSection('templates');
}
}}