fix(categories): add safe null guards on allowedParentOptions and enable parent selection in NewCategory.tsx
This commit is contained in:
@@ -113,8 +113,11 @@ export default function NewCategory() {
|
||||
}, [isEdit, id, categories, parentIdParam]);
|
||||
|
||||
const allowedParentOptions = useMemo(() => {
|
||||
if (!isEdit) return categories;
|
||||
return categories.filter((cat) => cat.id !== id && !cat.path?.startsWith(categories.find(c => c.id === id)?.path + "/"));
|
||||
const validCategories = Array.isArray(categories) ? categories.filter((cat) => cat && cat.id) : [];
|
||||
if (!isEdit) return validCategories;
|
||||
const currentCat = validCategories.find((c) => c.id === id);
|
||||
const currentPath = currentCat?.path || "";
|
||||
return validCategories.filter((cat) => cat.id !== id && (!currentPath || !cat.path?.startsWith(currentPath + "/")));
|
||||
}, [categories, isEdit, id]);
|
||||
|
||||
return (
|
||||
@@ -204,16 +207,16 @@ export default function NewCategory() {
|
||||
name="parentId"
|
||||
value={formik.values.parentId}
|
||||
onChange={formik.handleChange}
|
||||
disabled={!isEdit} // Disabled (Read-only) during creation, enabled during Edit
|
||||
disabled={Boolean(parentIdParam)}
|
||||
>
|
||||
<option value="">None (Root Level)</option>
|
||||
{allowedParentOptions.map((cat) => (
|
||||
{allowedParentOptions.filter(Boolean).map((cat) => (
|
||||
<option key={cat.id} value={cat.id}>
|
||||
{cat.name} ({cat.code})
|
||||
</option>
|
||||
))}
|
||||
</Select>
|
||||
{!isEdit && (
|
||||
{!isEdit && Boolean(parentIdParam) && (
|
||||
<p className="text-[10px] text-muted-foreground mt-1">
|
||||
Locked to parent context. Click inline tree actions to create subcategories.
|
||||
</p>
|
||||
|
||||
Reference in New Issue
Block a user