import { useLayoutEffect, useState } from 'react'; import Modal, { ModalProps } from '../../components/Common/Modal'; import SelectSingle from '../../components/Common/SelectSingle'; import TextInput from '../../components/Common/TextInput'; import { useRSForm } from '../../context/RSFormContext'; import { CstType, ICstRenameData } from '../../models/rsform'; import { createAliasFor, getCstTypeLabel, getCstTypePrefix } from '../../utils/staticUI'; import { SelectorCstType } from '../../utils/selectors'; interface DlgRenameCstProps extends Pick { initial?: ICstRenameData onRename: (data: ICstRenameData) => void } function DlgRenameCst({ hideWindow, initial, onRename }: DlgRenameCstProps) { const { schema } = useRSForm(); const [validated, setValidated] = useState(false); const [cstType, setCstType] = useState(CstType.BASE); const [cstID, setCstID] = useState(0) const [alias, setAlias] = useState(''); function getData(): ICstRenameData { return { cst_type: cstType, alias: alias, id: cstID } } const handleSubmit = () => onRename(getData()); useLayoutEffect( () => { if (schema && initial && cstType !== initial.cst_type) { setAlias(createAliasFor(cstType, schema)); } }, [initial, cstType, schema]); useLayoutEffect( () => { if (initial) { setCstType(initial.cst_type); setAlias(initial.alias); setCstID(initial.id); } }, [initial]); useLayoutEffect( () => { if (!initial || !schema) { setValidated(false); } else if(alias === initial.alias || alias.length < 2 || alias[0] !== getCstTypePrefix(cstType)) { setValidated(false); } else { setValidated(!schema.items.find(cst => cst.alias === alias)) } }, [cstType, alias, initial, schema]); return (
setCstType(data?.value ?? CstType.BASE)} />
setAlias(event.target.value)} />
); } export default DlgRenameCst;