chore: update, delete and create

This commit is contained in:
Rodolfo Ruiz
2025-09-05 19:17:17 -06:00
parent 2fa6b95012
commit 49dead566c

View File

@@ -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 (
<Paper sx={{ p: 2 }}>
<Typography variant="subtitle1" sx={{ mb: 2 }}>
{form._Id ? 'Edit Category' : 'Add Category'}
{form._id ? 'Edit Category' : 'Add Category'}
</Typography>
{isAdd && (
@@ -359,7 +358,7 @@ const tagLabelById = useMemo(() => {
<MenuItem value="Inactive">Inactive</MenuItem>
</TextField>
{form._Id ? (
{form._id ? (
<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 By" value={form.createdBy ?? '—'} InputProps={{ readOnly: true }} fullWidth />
@@ -369,7 +368,7 @@ const tagLabelById = useMemo(() => {
) : null}
<Box display="flex" justifyContent="space-between" gap={1} mt={3}>
{form._Id ? (
{form._id ? (
<Button color="error" onClick={handleDelete}>Delete</Button>
) : <span />}
<Box sx={{ display: 'flex', gap: 1 }}>