diff --git a/src/private/categories/AddOrEditCategoryForm.jsx b/src/private/categories/AddOrEditCategoryForm.jsx index 7014292..a354ce8 100644 --- a/src/private/categories/AddOrEditCategoryForm.jsx +++ b/src/private/categories/AddOrEditCategoryForm.jsx @@ -43,7 +43,7 @@ function formatDateSafe(value) { }).format(d); } -export default function AddOrEditCategoryForm({ onAdd, initialData, onCancel, materials: materialsProp = [], initialMaterialNames = [], originalMongoId, originalGuid }) { +export default function AddOrEditCategoryForm({ onAdd, initialData, onCancel, materials: materialsProp = [], initialMaterialNames = [] }) { const { user } = useAuth(); const token = user?.thalosToken || localStorage.getItem('thalosToken'); const api = useMemo(() => new CategoriesApi(token), [token]); @@ -62,7 +62,7 @@ const tagLabelById = useMemo(() => { }, [allTags]); const [form, setForm] = useState({ - _Id: '', + _id: '', id: '', tenantId: '', tagName: '', @@ -127,8 +127,8 @@ const tagLabelById = useMemo(() => { useEffect(() => { if (initialData) { setForm({ - _Id: initialData._Id, - Id: initialData.Id, + _id: initialData._id, + id: initialData.id, tenantId: initialData.tenantId || extractTenantId(token) || '', tagName: initialData.tagName || initialData.name || '', typeId: initialData.typeId || '', @@ -144,7 +144,7 @@ const tagLabelById = useMemo(() => { }); } else { setForm({ - _Id: '', + _id: '', id: '', tenantId: extractTenantId(token) || '', tagName: '', @@ -162,7 +162,7 @@ const tagLabelById = useMemo(() => { } }, [initialData]); - const isEdit = Boolean(form._Id); + const isEdit = Boolean(form._id); const isAdd = !isEdit; const setVal = (name, value) => setForm(p => ({ ...p, [name]: value })); @@ -170,7 +170,7 @@ const tagLabelById = useMemo(() => { const handleChange = (e) => { const { name, value } = e.target; setVal(name, value); - if (name === 'tagName' && !form._Id) { + if (name === 'tagName' && !form._id) { setVal('slug', slugify(value)); } }; @@ -185,6 +185,7 @@ const tagLabelById = useMemo(() => { if (!tenantId) throw new Error('TenantId not found in token'); const base = { + id: form.id?.trim() || undefined, tagName: form.tagName.trim(), typeId: form.typeId, parentTagId: form.parentTagId, @@ -195,17 +196,16 @@ const tagLabelById = useMemo(() => { tenantId, // requerido por backend (400 si falta) }; - if (form._Id) { - // UPDATE - // Prefer Mongo _id (24-hex) if present; fallback to GUID - const idForUpdate = form._Id; + if (form._id) { + const idForUpdate = Boolean(form._id) ? String(form._id) : null; + if (!idForUpdate) throw new Error('Missing _id for update'); const payload = { - id: idForUpdate, + _id: idForUpdate, ...base, }; + console.log('[CategoryForm] SUBMIT (edit) with _id:', idForUpdate, 'payload:', payload); await api.update(payload); } else { - // CREATE await api.create(base); } @@ -221,10 +221,9 @@ const tagLabelById = useMemo(() => { const handleDelete = async () => { try { - // Prefer Mongo _id if it looks like a 24-hex; otherwise fall back to GUID id - const hex = (typeof form._Id === 'string' && /^[0-9a-f]{24}$/i.test(form._Id)) ? form._Id : null; - const idToUse = hex || form._Id; - if (!idToUse) throw new Error('Missing id to delete'); + const idToUse = form._id; + if (!idToUse) throw new Error('Missing _id to delete'); + console.debug('[CategoryForm] DELETE with _id:', idToUse); await api.changeStatus({ id: idToUse, status: 'Inactive' }); if (onAdd) { await onAdd(); @@ -238,7 +237,7 @@ const tagLabelById = useMemo(() => { return ( - {form._Id ? 'Edit Category' : 'Add Category'} + {form._id ? 'Edit Category' : 'Add Category'} {isAdd && ( @@ -359,7 +358,7 @@ const tagLabelById = useMemo(() => { Inactive - {form._Id ? ( + {form._id ? ( @@ -369,7 +368,7 @@ const tagLabelById = useMemo(() => { ) : null} - {form._Id ? ( + {form._id ? ( ) : }