chore: create items

This commit is contained in:
Rodolfo Ruiz
2025-09-05 18:50:11 -06:00
parent f5acde78de
commit 2fa6b95012
2 changed files with 25 additions and 23 deletions

View File

@@ -43,7 +43,7 @@ function formatDateSafe(value) {
}).format(d); }).format(d);
} }
export default function AddOrEditCategoryForm({ onAdd, initialData, onCancel, materials: materialsProp = [], initialMaterialNames = [] }) { export default function AddOrEditCategoryForm({ onAdd, initialData, onCancel, materials: materialsProp = [], initialMaterialNames = [], originalMongoId, originalGuid }) {
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]);
@@ -55,7 +55,7 @@ export default function AddOrEditCategoryForm({ onAdd, initialData, onCancel, ma
const tagLabelById = useMemo(() => { const tagLabelById = useMemo(() => {
const map = {}; const map = {};
for (const t of allTags) { for (const t of allTags) {
const key = t._id || t.id; const key = t._id;
map[key] = t.tagName || t.name || key; map[key] = t.tagName || t.name || key;
} }
return map; return map;
@@ -107,9 +107,9 @@ const tagLabelById = useMemo(() => {
// Build a case-insensitive name -> id map // Build a case-insensitive name -> id map
const nameToId = new Map( const nameToId = new Map(
allTags.map(t => { allTags.map(t => {
const id = t._id || t.id; const _id = t._id;
const label = (t.tagName || t.name || '').toLowerCase(); const label = (t.tagName || t.name || '').toLowerCase();
return [label, id]; return [label, _id];
}) })
); );
@@ -126,11 +126,9 @@ const tagLabelById = useMemo(() => {
// set inicial // set inicial
useEffect(() => { useEffect(() => {
if (initialData) { if (initialData) {
const _Id = initialData._id || initialData._Id || '';
const id = initialData.id || initialData.Id || _Id || '';
setForm({ setForm({
_Id, _Id: initialData._Id,
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 || '',
@@ -164,7 +162,7 @@ const tagLabelById = useMemo(() => {
} }
}, [initialData]); }, [initialData]);
const isEdit = Boolean(form._Id || 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 }));
@@ -199,8 +197,10 @@ const tagLabelById = useMemo(() => {
if (form._Id) { if (form._Id) {
// UPDATE // UPDATE
// Prefer Mongo _id (24-hex) if present; fallback to GUID
const idForUpdate = form._Id;
const payload = { const payload = {
id: form.id || form._Id, // backend acepta GUID; si no hay, mandamos _id id: idForUpdate,
...base, ...base,
}; };
await api.update(payload); await api.update(payload);
@@ -221,9 +221,9 @@ const tagLabelById = useMemo(() => {
const handleDelete = async () => { const handleDelete = async () => {
try { try {
// Try to use Mongo _Id (24-hex); if not present, fall back to GUID `id`. // 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 hex = (typeof form._Id === 'string' && /^[0-9a-f]{24}$/i.test(form._Id)) ? form._Id : null;
const idToUse = hex || form.id; const idToUse = hex || form._Id;
if (!idToUse) throw new Error('Missing id to delete'); 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) {
@@ -273,7 +273,7 @@ const tagLabelById = useMemo(() => {
required required
> >
{types.map((t) => { {types.map((t) => {
const value = t._id || t.id; // prefer Mongo _id for 1:1 mapping const value = t._id;
const label = t.typeName || value; const label = t.typeName || value;
return ( return (
<MenuItem key={value} value={value}> <MenuItem key={value} value={value}>
@@ -307,7 +307,7 @@ const tagLabelById = useMemo(() => {
sx={{ mb: 2 }} sx={{ mb: 2 }}
> >
{allTags.map((t) => { {allTags.map((t) => {
const value = t._id || t.id; const value = t._id;
const label = t.tagName || t.name || value; const label = t.tagName || t.name || value;
return ( return (
<MenuItem key={value} value={value}> <MenuItem key={value} value={value}>
@@ -359,7 +359,7 @@ const tagLabelById = useMemo(() => {
<MenuItem value="Inactive">Inactive</MenuItem> <MenuItem value="Inactive">Inactive</MenuItem>
</TextField> </TextField>
{form._Id || 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 +369,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) ? ( {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 }}>

View File

@@ -43,7 +43,7 @@ export default function Categories() {
// Build a map of tagId -> tagName to resolve parent names // Build a map of tagId -> tagName to resolve parent names
const idToName = {}; const idToName = {};
for (const item of list) { for (const item of list) {
const key = item?._id || item?._Id || item?.id || item?.Id; const key = item?._id || item?.id;
if (key) idToName[key] = item?.tagName || item?.name || ''; if (key) idToName[key] = item?.tagName || item?.name || '';
} }
@@ -53,6 +53,7 @@ export default function Categories() {
const materialNames = parents const materialNames = parents
.map((pid) => idToName[pid]) .map((pid) => idToName[pid])
.filter(Boolean); .filter(Boolean);
return { return {
...r, ...r,
materialNames, // array of strings materialNames, // array of strings
@@ -75,8 +76,8 @@ export default function Categories() {
const r = params?.row; const r = params?.row;
if (!r) return; if (!r) return;
setEditingCategory({ setEditingCategory({
_Id: r._id || r._Id || '', _id: String(r._id || ''),
id: r.id || r.Id || '', id: String(r.id || ''),
tagName: r.tagName || r.name || '', tagName: r.tagName || r.name || '',
typeId: r.typeId || '', typeId: r.typeId || '',
parentTagId: Array.isArray(r.parentTagId) ? r.parentTagId : [], parentTagId: Array.isArray(r.parentTagId) ? r.parentTagId : [],
@@ -87,8 +88,8 @@ export default function Categories() {
materialNames: Array.isArray(r.materialNames) materialNames: Array.isArray(r.materialNames)
? r.materialNames ? r.materialNames
: (typeof r.material === 'string' : (typeof r.material === 'string'
? r.material.split(',').map(s => s.trim()).filter(Boolean) ? r.material.split(',').map(s => s.trim()).filter(Boolean)
: []), : []),
createdAt: r.createdAt ?? null, createdAt: r.createdAt ?? null,
createdBy: r.createdBy ?? null, createdBy: r.createdBy ?? null,
updatedAt: r.updatedAt ?? null, updatedAt: r.updatedAt ?? null,
@@ -104,7 +105,7 @@ export default function Categories() {
}; };
const pickHexId = (r) => const pickHexId = (r) =>
[r?._id, r?._Id, r?.id, r?.Id] [r?._id, r?.id]
.filter(Boolean) .filter(Boolean)
.find((x) => typeof x === 'string' && /^[0-9a-f]{24}$/i.test(x)) || null; .find((x) => typeof x === 'string' && /^[0-9a-f]{24}$/i.test(x)) || null;
@@ -182,6 +183,7 @@ export default function Categories() {
{ field: 'tagName', headerName: 'Name', flex: 1.2, minWidth: 180 }, { field: 'tagName', headerName: 'Name', flex: 1.2, minWidth: 180 },
{ field: 'slug', headerName: 'Slug', flex: 1.0, minWidth: 160 }, { field: 'slug', headerName: 'Slug', flex: 1.0, minWidth: 160 },
{ field: 'icon', headerName: 'Icon', flex: 0.7, minWidth: 250 }, { field: 'icon', headerName: 'Icon', flex: 0.7, minWidth: 250 },
/* /*
{ field: 'material', headerName: 'Material', flex: 1.2, minWidth: 200 }, { field: 'material', headerName: 'Material', flex: 1.2, minWidth: 200 },
*/ */