diff --git a/rsconcept/frontend/src/dialogs/DlgConstituentaTemplate/DlgConstituentaTemplate.tsx b/rsconcept/frontend/src/dialogs/DlgConstituentaTemplate/DlgConstituentaTemplate.tsx index d5d460f9..57eb6ea3 100644 --- a/rsconcept/frontend/src/dialogs/DlgConstituentaTemplate/DlgConstituentaTemplate.tsx +++ b/rsconcept/frontend/src/dialogs/DlgConstituentaTemplate/DlgConstituentaTemplate.tsx @@ -9,6 +9,7 @@ import Modal, { ModalProps } from '@/components/ui/Modal'; import Overlay from '@/components/ui/Overlay'; import TabLabel from '@/components/ui/TabLabel'; import AnimateFade from '@/components/wrap/AnimateFade'; +import { useLibrary } from '@/context/LibraryContext'; import usePartialUpdate from '@/hooks/usePartialUpdate'; import { HelpTopic } from '@/models/miscellaneous'; import { CstType, ICstCreateData, IRSForm } from '@/models/rsform'; @@ -33,8 +34,10 @@ export enum TabID { } function DlgConstituentaTemplate({ hideWindow, schema, onCreate, insertAfter }: DlgConstituentaTemplateProps) { + const { retrieveTemplate } = useLibrary(); const [activeTab, setActiveTab] = useState(TabID.TEMPLATE); + const [templateSchema, setTemplateSchema] = useState(undefined); const [template, updateTemplate] = usePartialUpdate({}); const [substitutes, updateSubstitutes] = usePartialUpdate({ definition: '', @@ -43,7 +46,7 @@ function DlgConstituentaTemplate({ hideWindow, schema, onCreate, insertAfter }: const [constituenta, updateConstituenta] = usePartialUpdate({ cst_type: CstType.TERM, insert_after: insertAfter ?? null, - alias: '', + alias: generateAlias(CstType.TERM, schema), convention: '', definition_formal: '', definition_raw: '', @@ -55,8 +58,12 @@ function DlgConstituentaTemplate({ hideWindow, schema, onCreate, insertAfter }: const handleSubmit = () => onCreate(constituenta); useLayoutEffect(() => { - updateConstituenta({ alias: generateAlias(constituenta.cst_type, schema) }); - }, [constituenta.cst_type, updateConstituenta, schema]); + if (!template.templateID) { + setTemplateSchema(undefined); + } else { + retrieveTemplate(template.templateID, setTemplateSchema); + } + }, [template.templateID, retrieveTemplate]); useLayoutEffect(() => { if (!template.prototype) { @@ -72,6 +79,7 @@ function DlgConstituentaTemplate({ hideWindow, schema, onCreate, insertAfter }: } else { updateConstituenta({ cst_type: template.prototype.cst_type, + alias: generateAlias(template.prototype.cst_type, schema), definition_raw: template.prototype.definition_raw, definition_formal: template.prototype.definition_formal, term_raw: template.prototype.term_raw @@ -85,7 +93,7 @@ function DlgConstituentaTemplate({ hideWindow, schema, onCreate, insertAfter }: })) }); } - }, [template.prototype, updateConstituenta, updateSubstitutes]); + }, [template.prototype, updateConstituenta, updateSubstitutes, schema]); useLayoutEffect(() => { if (substitutes.arguments.length === 0 || !template.prototype) { @@ -95,12 +103,13 @@ function DlgConstituentaTemplate({ hideWindow, schema, onCreate, insertAfter }: const type = inferTemplatedType(template.prototype.cst_type, substitutes.arguments); updateConstituenta({ cst_type: type, + alias: generateAlias(type, schema), definition_formal: definition }); updateSubstitutes({ definition: definition }); - }, [substitutes.arguments, template.prototype, updateConstituenta, updateSubstitutes]); + }, [substitutes.arguments, template.prototype, updateConstituenta, updateSubstitutes, schema]); useLayoutEffect(() => { setValidated(!!template.prototype && validateNewAlias(constituenta.alias, constituenta.cst_type, schema)); @@ -109,10 +118,10 @@ function DlgConstituentaTemplate({ hideWindow, schema, onCreate, insertAfter }: const templatePanel = useMemo( () => ( - + ), - [template, updateTemplate] + [template, templateSchema, updateTemplate] ); const argumentsPanel = useMemo( diff --git a/rsconcept/frontend/src/dialogs/DlgConstituentaTemplate/TabArguments.tsx b/rsconcept/frontend/src/dialogs/DlgConstituentaTemplate/TabArguments.tsx index 15ed7d34..582af1d2 100644 --- a/rsconcept/frontend/src/dialogs/DlgConstituentaTemplate/TabArguments.tsx +++ b/rsconcept/frontend/src/dialogs/DlgConstituentaTemplate/TabArguments.tsx @@ -216,6 +216,7 @@ function TabArguments({ state, schema, partialUpdate }: TabArgumentsProps) { >; + templateSchema?: IRSForm; } -function TabTemplate({ state, partialUpdate }: TabTemplateProps) { - const { templates, retrieveTemplate } = useLibrary(); - const [templateSchema, setTemplateSchema] = useState(undefined); +function TabTemplate({ state, partialUpdate, templateSchema }: TabTemplateProps) { + const { templates } = useLibrary(); const [filteredData, setFilteredData] = useState([]); @@ -65,14 +66,6 @@ function TabTemplate({ state, partialUpdate }: TabTemplateProps) { } }, [templates, state.templateID, partialUpdate]); - useEffect(() => { - if (!state.templateID) { - setTemplateSchema(undefined); - } else { - retrieveTemplate(state.templateID, setTemplateSchema); - } - }, [state.templateID, retrieveTemplate]); - useEffect(() => { if (!templateSchema) { return; diff --git a/rsconcept/frontend/src/dialogs/DlgCreateCst/DlgCreateCst.tsx b/rsconcept/frontend/src/dialogs/DlgCreateCst/DlgCreateCst.tsx index b5d2016c..4113a389 100644 --- a/rsconcept/frontend/src/dialogs/DlgCreateCst/DlgCreateCst.tsx +++ b/rsconcept/frontend/src/dialogs/DlgCreateCst/DlgCreateCst.tsx @@ -1,11 +1,11 @@ 'use client'; -import { useEffect, useLayoutEffect, useState } from 'react'; +import { useState } from 'react'; import Modal, { ModalProps } from '@/components/ui/Modal'; import usePartialUpdate from '@/hooks/usePartialUpdate'; import { CstType, ICstCreateData, IRSForm } from '@/models/rsform'; -import { generateAlias, validateNewAlias } from '@/models/rsformAPI'; +import { generateAlias } from '@/models/rsformAPI'; import FormCreateCst from './FormCreateCst'; @@ -21,7 +21,7 @@ function DlgCreateCst({ hideWindow, initial, schema, onCreate }: DlgCreateCstPro initial || { cst_type: CstType.BASE, insert_after: null, - alias: '', + alias: generateAlias(CstType.BASE, schema), convention: '', definition_formal: '', definition_raw: '', @@ -32,14 +32,6 @@ function DlgCreateCst({ hideWindow, initial, schema, onCreate }: DlgCreateCstPro const handleSubmit = () => onCreate(cstData); - useLayoutEffect(() => { - updateCstData({ alias: generateAlias(cstData.cst_type, schema) }); - }, [cstData.cst_type, updateCstData, schema]); - - useEffect(() => { - setValidated(validateNewAlias(cstData.alias, cstData.cst_type, schema)); - }, [cstData.alias, cstData.cst_type, schema]); - return ( !!state.convention || forceComment || isBasic, [state, forceComment, isBasic]); useLayoutEffect(() => { - partialUpdate({ alias: generateAlias(state.cst_type, schema) }); setForceComment(false); }, [state.cst_type, partialUpdate, schema]); @@ -43,30 +42,35 @@ function FormCreateCst({ schema, state, partialUpdate, setValidated }: FormCreat } }, [state.alias, state.cst_type, schema, setValidated]); + const handleTypeChange = useCallback( + (target: CstType) => partialUpdate({ cst_type: target, alias: generateAlias(target, schema) }), + [partialUpdate, schema, generateAlias] + ); + return ( -
+
partialUpdate({ cst_type: data?.value ?? CstType.BASE })} - /> - handleTypeChange(data?.value ?? CstType.BASE)} /> partialUpdate({ alias: event.target.value })} /> +