feat: add validation for required fields and reset editor fields after saving
This commit is contained in:
parent
fa37a844e8
commit
3c531daa91
@ -24,6 +24,13 @@ export default function ContractEditor({ onSaved }: Props) {
|
||||
|
||||
const { uploadTemplate, updateTemplateState } = useContractManagement();
|
||||
|
||||
const resetEditorFields = () => {
|
||||
setName('');
|
||||
setHtmlCode('');
|
||||
setDescription('');
|
||||
setIsPreview(false);
|
||||
};
|
||||
|
||||
// Build a full HTML doc if user pasted only a snippet
|
||||
const wrapIfNeeded = (src: string) => {
|
||||
const hasDoc = /<!DOCTYPE|<html[\s>]/i.test(src);
|
||||
@ -100,7 +107,6 @@ export default function ContractEditor({ onSaved }: Props) {
|
||||
const canSave = Boolean(
|
||||
name.trim() &&
|
||||
htmlCode.trim() &&
|
||||
description.trim() &&
|
||||
type &&
|
||||
(type === 'contract' ? contractType : true) &&
|
||||
userType &&
|
||||
@ -111,7 +117,7 @@ export default function ContractEditor({ onSaved }: Props) {
|
||||
const html = htmlCode.trim();
|
||||
// NEW: validate all fields
|
||||
if (!canSave) {
|
||||
setStatusMsg('Please fill all required fields (name, HTML, type, user type, language, description).');
|
||||
setStatusMsg('Please fill all required fields (name, HTML, type, user type, language).');
|
||||
return;
|
||||
}
|
||||
|
||||
@ -137,8 +143,8 @@ export default function ContractEditor({ onSaved }: Props) {
|
||||
|
||||
setStatusMsg(publish ? 'Template created and activated.' : 'Template created.');
|
||||
if (onSaved) onSaved();
|
||||
// Optionally clear fields
|
||||
// setName(''); setHtmlCode(''); setDescription(''); setType('contract'); setLang('en');
|
||||
// Reset so another template can be created immediately
|
||||
resetEditorFields();
|
||||
} catch (e: any) {
|
||||
setStatusMsg(e?.message || 'Save failed.');
|
||||
} finally {
|
||||
@ -225,7 +231,6 @@ export default function ContractEditor({ onSaved }: Props) {
|
||||
placeholder="Description (optional)"
|
||||
value={description}
|
||||
onChange={(e) => setDescription(e.target.value)}
|
||||
required
|
||||
className="w-full rounded-lg border border-gray-300 bg-white px-4 py-2 text-sm text-gray-900 outline-none focus:ring-2 focus:ring-indigo-500/50 shadow"
|
||||
/>
|
||||
</div>
|
||||
|
||||
Loading…
Reference in New Issue
Block a user