chore: update, delete and create
This commit is contained in:
		| @@ -43,7 +43,7 @@ function formatDateSafe(value) { | |||||||
|   }).format(d); |   }).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 { user } = useAuth(); | ||||||
|   const token = user?.thalosToken || localStorage.getItem('thalosToken'); |   const token = user?.thalosToken || localStorage.getItem('thalosToken'); | ||||||
|   const api = useMemo(() => new CategoriesApi(token), [token]); |   const api = useMemo(() => new CategoriesApi(token), [token]); | ||||||
| @@ -62,7 +62,7 @@ const tagLabelById = useMemo(() => { | |||||||
| }, [allTags]); | }, [allTags]); | ||||||
|  |  | ||||||
|   const [form, setForm] = useState({ |   const [form, setForm] = useState({ | ||||||
|     _Id: '', |     _id: '', | ||||||
|     id: '', |     id: '', | ||||||
|     tenantId: '', |     tenantId: '', | ||||||
|     tagName: '', |     tagName: '', | ||||||
| @@ -127,8 +127,8 @@ const tagLabelById = useMemo(() => { | |||||||
|   useEffect(() => { |   useEffect(() => { | ||||||
|     if (initialData) { |     if (initialData) { | ||||||
|       setForm({ |       setForm({ | ||||||
|         _Id: initialData._Id, |         _id: initialData._id, | ||||||
|         Id: initialData.Id, |         id: initialData.id, | ||||||
|         tenantId: initialData.tenantId || extractTenantId(token) || '', |         tenantId: initialData.tenantId || extractTenantId(token) || '', | ||||||
|         tagName: initialData.tagName || initialData.name || '', |         tagName: initialData.tagName || initialData.name || '', | ||||||
|         typeId: initialData.typeId || '', |         typeId: initialData.typeId || '', | ||||||
| @@ -144,7 +144,7 @@ const tagLabelById = useMemo(() => { | |||||||
|       }); |       }); | ||||||
|     } else { |     } else { | ||||||
|       setForm({ |       setForm({ | ||||||
|         _Id: '', |         _id: '', | ||||||
|         id: '', |         id: '', | ||||||
|         tenantId: extractTenantId(token) || '', |         tenantId: extractTenantId(token) || '', | ||||||
|         tagName: '', |         tagName: '', | ||||||
| @@ -162,7 +162,7 @@ const tagLabelById = useMemo(() => { | |||||||
|     } |     } | ||||||
|   }, [initialData]); |   }, [initialData]); | ||||||
|  |  | ||||||
|   const isEdit = Boolean(form._Id); |   const isEdit = Boolean(form._id); | ||||||
|   const isAdd = !isEdit; |   const isAdd = !isEdit; | ||||||
|  |  | ||||||
|   const setVal = (name, value) => setForm(p => ({ ...p, [name]: value })); |   const setVal = (name, value) => setForm(p => ({ ...p, [name]: value })); | ||||||
| @@ -170,7 +170,7 @@ const tagLabelById = useMemo(() => { | |||||||
|   const handleChange = (e) => { |   const handleChange = (e) => { | ||||||
|     const { name, value } = e.target; |     const { name, value } = e.target; | ||||||
|     setVal(name, value); |     setVal(name, value); | ||||||
|     if (name === 'tagName' && !form._Id) { |     if (name === 'tagName' && !form._id) { | ||||||
|       setVal('slug', slugify(value)); |       setVal('slug', slugify(value)); | ||||||
|     } |     } | ||||||
|   }; |   }; | ||||||
| @@ -185,6 +185,7 @@ const tagLabelById = useMemo(() => { | |||||||
|       if (!tenantId) throw new Error('TenantId not found in token'); |       if (!tenantId) throw new Error('TenantId not found in token'); | ||||||
|  |  | ||||||
|       const base = { |       const base = { | ||||||
|  |         id: form.id?.trim() || undefined, | ||||||
|         tagName: form.tagName.trim(), |         tagName: form.tagName.trim(), | ||||||
|         typeId: form.typeId, |         typeId: form.typeId, | ||||||
|         parentTagId: form.parentTagId, |         parentTagId: form.parentTagId, | ||||||
| @@ -195,17 +196,16 @@ const tagLabelById = useMemo(() => { | |||||||
|         tenantId, // requerido por backend (400 si falta) |         tenantId, // requerido por backend (400 si falta) | ||||||
|       }; |       }; | ||||||
|  |  | ||||||
|       if (form._Id) { |       if (form._id) { | ||||||
|         // UPDATE |         const idForUpdate = Boolean(form._id) ? String(form._id) : null; | ||||||
|         // Prefer Mongo _id (24-hex) if present; fallback to GUID |         if (!idForUpdate) throw new Error('Missing _id for update'); | ||||||
|         const idForUpdate = form._Id; |  | ||||||
|         const payload = { |         const payload = { | ||||||
|           id: idForUpdate, |           _id: idForUpdate, | ||||||
|           ...base, |           ...base, | ||||||
|         }; |         }; | ||||||
|  |         console.log('[CategoryForm] SUBMIT (edit) with _id:', idForUpdate, 'payload:', payload); | ||||||
|         await api.update(payload); |         await api.update(payload); | ||||||
|       } else { |       } else { | ||||||
|         // CREATE |  | ||||||
|         await api.create(base); |         await api.create(base); | ||||||
|       } |       } | ||||||
|  |  | ||||||
| @@ -221,10 +221,9 @@ const tagLabelById = useMemo(() => { | |||||||
|  |  | ||||||
|   const handleDelete = async () => { |   const handleDelete = async () => { | ||||||
|     try { |     try { | ||||||
|       // Prefer Mongo _id if it looks like a 24-hex; otherwise fall back to GUID id |       const idToUse = form._id; | ||||||
|       const hex = (typeof form._Id === 'string' && /^[0-9a-f]{24}$/i.test(form._Id)) ? form._Id : null; |       if (!idToUse) throw new Error('Missing _id to delete'); | ||||||
|       const idToUse = hex || form._Id; |       console.debug('[CategoryForm] DELETE with _id:', idToUse); | ||||||
|       if (!idToUse) throw new Error('Missing id to delete'); |  | ||||||
|       await api.changeStatus({ id: idToUse, status: 'Inactive' }); |       await api.changeStatus({ id: idToUse, status: 'Inactive' }); | ||||||
|       if (onAdd) { |       if (onAdd) { | ||||||
|         await onAdd(); |         await onAdd(); | ||||||
| @@ -238,7 +237,7 @@ const tagLabelById = useMemo(() => { | |||||||
|   return ( |   return ( | ||||||
|     <Paper sx={{ p: 2 }}> |     <Paper sx={{ p: 2 }}> | ||||||
|       <Typography variant="subtitle1" sx={{ mb: 2 }}> |       <Typography variant="subtitle1" sx={{ mb: 2 }}> | ||||||
|         {form._Id ? 'Edit Category' : 'Add Category'} |         {form._id ? 'Edit Category' : 'Add Category'} | ||||||
|       </Typography> |       </Typography> | ||||||
|  |  | ||||||
|       {isAdd && ( |       {isAdd && ( | ||||||
| @@ -359,7 +358,7 @@ const tagLabelById = useMemo(() => { | |||||||
|         <MenuItem value="Inactive">Inactive</MenuItem> |         <MenuItem value="Inactive">Inactive</MenuItem> | ||||||
|       </TextField> |       </TextField> | ||||||
|  |  | ||||||
|       {form._Id ? ( |       {form._id ? ( | ||||||
|         <Box sx={{ display: 'grid', gridTemplateColumns: { xs: '1fr', md: '1fr 1fr' }, gap: 2, mt: 2 }}> |         <Box sx={{ display: 'grid', gridTemplateColumns: { xs: '1fr', md: '1fr 1fr' }, gap: 2, mt: 2 }}> | ||||||
|           <TextField label="Created At" value={formatDateSafe(form.createdAt)} InputProps={{ readOnly: true }} fullWidth /> |           <TextField label="Created At" value={formatDateSafe(form.createdAt)} InputProps={{ readOnly: true }} fullWidth /> | ||||||
|           <TextField label="Created By" value={form.createdBy ?? '—'} InputProps={{ readOnly: true }} fullWidth /> |           <TextField label="Created By" value={form.createdBy ?? '—'} InputProps={{ readOnly: true }} fullWidth /> | ||||||
| @@ -369,7 +368,7 @@ const tagLabelById = useMemo(() => { | |||||||
|       ) : null} |       ) : null} | ||||||
|  |  | ||||||
|       <Box display="flex" justifyContent="space-between" gap={1} mt={3}> |       <Box display="flex" justifyContent="space-between" gap={1} mt={3}> | ||||||
|         {form._Id ? ( |         {form._id ? ( | ||||||
|           <Button color="error" onClick={handleDelete}>Delete</Button> |           <Button color="error" onClick={handleDelete}>Delete</Button> | ||||||
|         ) : <span />} |         ) : <span />} | ||||||
|         <Box sx={{ display: 'flex', gap: 1 }}> |         <Box sx={{ display: 'flex', gap: 1 }}> | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Rodolfo Ruiz
					Rodolfo Ruiz