chore: create items
This commit is contained in:
@@ -43,7 +43,7 @@ function formatDateSafe(value) {
|
||||
}).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 token = user?.thalosToken || localStorage.getItem('thalosToken');
|
||||
const api = useMemo(() => new CategoriesApi(token), [token]);
|
||||
@@ -55,7 +55,7 @@ export default function AddOrEditCategoryForm({ onAdd, initialData, onCancel, ma
|
||||
const tagLabelById = useMemo(() => {
|
||||
const map = {};
|
||||
for (const t of allTags) {
|
||||
const key = t._id || t.id;
|
||||
const key = t._id;
|
||||
map[key] = t.tagName || t.name || key;
|
||||
}
|
||||
return map;
|
||||
@@ -107,9 +107,9 @@ const tagLabelById = useMemo(() => {
|
||||
// Build a case-insensitive name -> id map
|
||||
const nameToId = new Map(
|
||||
allTags.map(t => {
|
||||
const id = t._id || t.id;
|
||||
const _id = t._id;
|
||||
const label = (t.tagName || t.name || '').toLowerCase();
|
||||
return [label, id];
|
||||
return [label, _id];
|
||||
})
|
||||
);
|
||||
|
||||
@@ -126,11 +126,9 @@ const tagLabelById = useMemo(() => {
|
||||
// set inicial
|
||||
useEffect(() => {
|
||||
if (initialData) {
|
||||
const _Id = initialData._id || initialData._Id || '';
|
||||
const id = initialData.id || initialData.Id || _Id || '';
|
||||
setForm({
|
||||
_Id,
|
||||
id,
|
||||
_Id: initialData._Id,
|
||||
Id: initialData.Id,
|
||||
tenantId: initialData.tenantId || extractTenantId(token) || '',
|
||||
tagName: initialData.tagName || initialData.name || '',
|
||||
typeId: initialData.typeId || '',
|
||||
@@ -164,7 +162,7 @@ const tagLabelById = useMemo(() => {
|
||||
}
|
||||
}, [initialData]);
|
||||
|
||||
const isEdit = Boolean(form._Id || form.id);
|
||||
const isEdit = Boolean(form._Id);
|
||||
const isAdd = !isEdit;
|
||||
|
||||
const setVal = (name, value) => setForm(p => ({ ...p, [name]: value }));
|
||||
@@ -199,8 +197,10 @@ const tagLabelById = useMemo(() => {
|
||||
|
||||
if (form._Id) {
|
||||
// UPDATE
|
||||
// Prefer Mongo _id (24-hex) if present; fallback to GUID
|
||||
const idForUpdate = form._Id;
|
||||
const payload = {
|
||||
id: form.id || form._Id, // backend acepta GUID; si no hay, mandamos _id
|
||||
id: idForUpdate,
|
||||
...base,
|
||||
};
|
||||
await api.update(payload);
|
||||
@@ -221,9 +221,9 @@ const tagLabelById = useMemo(() => {
|
||||
|
||||
const handleDelete = async () => {
|
||||
try {
|
||||
// Try to use Mongo _Id (24-hex); if not present, 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;
|
||||
// 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');
|
||||
await api.changeStatus({ id: idToUse, status: 'Inactive' });
|
||||
if (onAdd) {
|
||||
@@ -273,7 +273,7 @@ const tagLabelById = useMemo(() => {
|
||||
required
|
||||
>
|
||||
{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;
|
||||
return (
|
||||
<MenuItem key={value} value={value}>
|
||||
@@ -307,7 +307,7 @@ const tagLabelById = useMemo(() => {
|
||||
sx={{ mb: 2 }}
|
||||
>
|
||||
{allTags.map((t) => {
|
||||
const value = t._id || t.id;
|
||||
const value = t._id;
|
||||
const label = t.tagName || t.name || value;
|
||||
return (
|
||||
<MenuItem key={value} value={value}>
|
||||
@@ -359,7 +359,7 @@ const tagLabelById = useMemo(() => {
|
||||
<MenuItem value="Inactive">Inactive</MenuItem>
|
||||
</TextField>
|
||||
|
||||
{form._Id || 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 +369,7 @@ const tagLabelById = useMemo(() => {
|
||||
) : null}
|
||||
|
||||
<Box display="flex" justifyContent="space-between" gap={1} mt={3}>
|
||||
{(form._Id || form.id) ? (
|
||||
{form._Id ? (
|
||||
<Button color="error" onClick={handleDelete}>Delete</Button>
|
||||
) : <span />}
|
||||
<Box sx={{ display: 'flex', gap: 1 }}>
|
||||
|
||||
@@ -43,7 +43,7 @@ export default function Categories() {
|
||||
// Build a map of tagId -> tagName to resolve parent names
|
||||
const idToName = {};
|
||||
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 || '';
|
||||
}
|
||||
|
||||
@@ -53,6 +53,7 @@ export default function Categories() {
|
||||
const materialNames = parents
|
||||
.map((pid) => idToName[pid])
|
||||
.filter(Boolean);
|
||||
|
||||
return {
|
||||
...r,
|
||||
materialNames, // array of strings
|
||||
@@ -75,8 +76,8 @@ export default function Categories() {
|
||||
const r = params?.row;
|
||||
if (!r) return;
|
||||
setEditingCategory({
|
||||
_Id: r._id || r._Id || '',
|
||||
id: r.id || r.Id || '',
|
||||
_id: String(r._id || ''),
|
||||
id: String(r.id || ''),
|
||||
tagName: r.tagName || r.name || '',
|
||||
typeId: r.typeId || '',
|
||||
parentTagId: Array.isArray(r.parentTagId) ? r.parentTagId : [],
|
||||
@@ -87,8 +88,8 @@ export default function Categories() {
|
||||
materialNames: Array.isArray(r.materialNames)
|
||||
? r.materialNames
|
||||
: (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,
|
||||
createdBy: r.createdBy ?? null,
|
||||
updatedAt: r.updatedAt ?? null,
|
||||
@@ -104,7 +105,7 @@ export default function Categories() {
|
||||
};
|
||||
|
||||
const pickHexId = (r) =>
|
||||
[r?._id, r?._Id, r?.id, r?.Id]
|
||||
[r?._id, r?.id]
|
||||
.filter(Boolean)
|
||||
.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: 'slug', headerName: 'Slug', flex: 1.0, minWidth: 160 },
|
||||
{ field: 'icon', headerName: 'Icon', flex: 0.7, minWidth: 250 },
|
||||
|
||||
/*
|
||||
{ field: 'material', headerName: 'Material', flex: 1.2, minWidth: 200 },
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user