From d9bfaba977f09ce33dc330a943786cdec0ba663d Mon Sep 17 00:00:00 2001 From: Rodolfo Ruiz Date: Thu, 4 Sep 2025 21:35:26 -0600 Subject: [PATCH] chore: show material in the edit form, needs to test save button --- .../categories/AddOrEditCategoryForm.jsx | 43 ++++++++----------- src/private/categories/Categories.jsx | 1 + 2 files changed, 20 insertions(+), 24 deletions(-) diff --git a/src/private/categories/AddOrEditCategoryForm.jsx b/src/private/categories/AddOrEditCategoryForm.jsx index 28199e1..40c2445 100644 --- a/src/private/categories/AddOrEditCategoryForm.jsx +++ b/src/private/categories/AddOrEditCategoryForm.jsx @@ -2,6 +2,7 @@ import { useEffect, useMemo, useState } from 'react'; import { Box, Button, Paper, TextField, Typography, MenuItem, Chip } from '@mui/material'; import { useAuth } from '../../context/AuthContext'; import CategoriesApi from '../../api/CategoriesApi'; +import TagTypeApi from '../../api/TagTypeApi'; import { jwtDecode } from 'jwt-decode'; function slugify(s) { @@ -46,6 +47,7 @@ export default function AddOrEditCategoryForm({ onAdd, initialData, onCancel, ma const { user } = useAuth(); const token = user?.thalosToken || localStorage.getItem('thalosToken'); const api = useMemo(() => new CategoriesApi(token), [token]); + const tagTypeApi = useMemo(() => new TagTypeApi(token), [token]); const [types, setTypes] = useState([]); const [allTags, setAllTags] = useState([]); @@ -76,27 +78,16 @@ const tagLabelById = useMemo(() => { updatedBy: null, }); - // cargar tipos y tags para selects + // cargar tipos (Tag Types) y tags para selects useEffect(() => { (async () => { try { - // Always try to load all tags (for materials, lookups, etc.) - const tags = typeof api.getAll === 'function' ? await api.getAll() : []; - - // Try multiple method names for types; if none exist, derive from tags - let typesResp = []; - if (typeof api.getAllTypes === 'function') { - typesResp = await api.getAllTypes(); - } else if (typeof api.getTypes === 'function') { - typesResp = await api.getTypes(); - } else if (Array.isArray(tags)) { - // Derive a minimal "types" list from existing tag.typeId values - const uniqueTypeIds = [...new Set(tags.map(t => t?.typeId).filter(Boolean))]; - typesResp = uniqueTypeIds.map(id => ({ id, typeName: id, level: null })); - console.warn('CategoriesApi has no getAllTypes/getTypes; derived types from tag.typeId.'); - } - + // Load all tag types from TagTypeApi + const typesResp = await tagTypeApi.getAll(); setTypes(Array.isArray(typesResp) ? typesResp : []); + + // Load all existing tags (used to resolve parentTagId -> labels for Material) + const tags = typeof api.getAll === 'function' ? await api.getAll() : []; setAllTags(Array.isArray(tags) ? tags : []); } catch (e) { console.error('Failed to load tag types or tags', e); @@ -104,7 +95,7 @@ const tagLabelById = useMemo(() => { setAllTags([]); } })(); - }, [api]); + }, [tagTypeApi, api]); // When editing: if we received material names from the grid, map them to IDs once allTags are loaded. useEffect(() => { @@ -273,7 +264,7 @@ const tagLabelById = useMemo(() => { { sx={{ mb: 2 }} required > - {types.map(t => ( - - {t.typeName} ({t.level ?? '-'}) - - ))} + {types.map((t) => { + const value = t._id || t.id; // prefer Mongo _id for 1:1 mapping + const label = t.typeName || value; + return ( + + {label} + + ); + })}