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:
@@ -341,9 +341,18 @@ export function CatalogPage() {
|
||||
};
|
||||
|
||||
// ─── 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) => {
|
||||
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();
|
||||
toast.success(`${type} toggled`);
|
||||
fetchTree();
|
||||
@@ -386,7 +395,7 @@ export function CatalogPage() {
|
||||
await handleCityDelete(country, city);
|
||||
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" });
|
||||
if (!res.ok) {
|
||||
const data = await res.json();
|
||||
@@ -490,7 +499,7 @@ export function CatalogPage() {
|
||||
}
|
||||
} else {
|
||||
const body = { name: renameValue.trim() };
|
||||
const typePath = `${type}s`;
|
||||
const typePath = typeToPath(type);
|
||||
const res = await fetch(`/api/${typePath}/${id}`, {
|
||||
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") => {
|
||||
try {
|
||||
const res = await fetch(`/api/${type}s/${id}/sort`, {
|
||||
const res = await fetch(`/api/${typeToPath(type)}/${id}/sort`, {
|
||||
method: "PATCH",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ direction }),
|
||||
|
||||
Reference in New Issue
Block a user