fix(admin): catalog tree — typeToPath mapping (categorys->categories, subcategorys->subcategories)

- handleToggleActive/handleDelete/handleRename/handleSort used ${type}s which produced 'categorys'/'subcategorys' -> 404
- Added typeToPath helper: category->categories, subcategory->subcategories, location->locations, product->products
This commit is contained in:
NW
2026-08-10 12:27:26 +01:00
parent 9ac7afa36e
commit cef8fbbee4

View File

@@ -341,9 +341,18 @@ export function CatalogPage() {
}; };
// ─── Tree node actions ───────────────────────── // ─── Tree node actions ─────────────────────────
// Маппинг type → API path (category → categories, subcategory → subcategories)
const typeToPath = (type: string) => {
if (type === "category") return "categories";
if (type === "subcategory") return "subcategories";
if (type === "location") return "locations";
if (type === "product") return "products";
return `${type}s`;
};
const handleToggleActive = async (type: string, id: number) => { const handleToggleActive = async (type: string, id: number) => {
try { try {
const res = await fetch(`/api/${type}s/${id}`, { method: "PATCH" }); const res = await fetch(`/api/${typeToPath(type)}/${id}`, { method: "PATCH" });
if (!res.ok) throw new Error(); if (!res.ok) throw new Error();
toast.success(`${type} toggled`); toast.success(`${type} toggled`);
fetchTree(); fetchTree();
@@ -386,7 +395,7 @@ export function CatalogPage() {
await handleCityDelete(country, city); await handleCityDelete(country, city);
return; return;
} }
const typePath = deleteTarget.type === "product" ? "products" : `${deleteTarget.type}s`; const typePath = deleteTarget.type === "product" ? "products" : typeToPath(deleteTarget.type);
const res = await fetch(`/api/${typePath}/${deleteTarget.id}`, { method: "DELETE" }); const res = await fetch(`/api/${typePath}/${deleteTarget.id}`, { method: "DELETE" });
if (!res.ok) { if (!res.ok) {
const data = await res.json(); const data = await res.json();
@@ -490,7 +499,7 @@ export function CatalogPage() {
} }
} else { } else {
const body = { name: renameValue.trim() }; const body = { name: renameValue.trim() };
const typePath = `${type}s`; const typePath = typeToPath(type);
const res = await fetch(`/api/${typePath}/${id}`, { const res = await fetch(`/api/${typePath}/${id}`, {
method: "PUT", headers: { "Content-Type": "application/json" }, body: JSON.stringify(body), method: "PUT", headers: { "Content-Type": "application/json" }, body: JSON.stringify(body),
}); });
@@ -506,7 +515,7 @@ export function CatalogPage() {
const handleSort = async (type: string, id: number, direction: "up" | "down") => { const handleSort = async (type: string, id: number, direction: "up" | "down") => {
try { try {
const res = await fetch(`/api/${type}s/${id}/sort`, { const res = await fetch(`/api/${typeToPath(type)}/${id}/sort`, {
method: "PATCH", method: "PATCH",
headers: { "Content-Type": "application/json" }, headers: { "Content-Type": "application/json" },
body: JSON.stringify({ direction }), body: JSON.stringify({ direction }),