put adminleftnavigation in a component, toi unclutter the page

This commit is contained in:
2025-12-12 23:19:33 -05:00
parent e200667611
commit 3ddb48fe58
5 changed files with 611 additions and 85 deletions
+103
View File
@@ -0,0 +1,103 @@
// lib/api/platforms.ts
export type Platform = {
id: number;
key: string;
label: string;
isActive: boolean;
};
export type CreatePlatformDto = {
key: string;
label: string;
isActive?: boolean;
};
export type UpdatePlatformDto = Partial<CreatePlatformDto>;
const API_BASE =
process.env.NEXT_PUBLIC_API_BASE_URL ?? "http://localhost:8080";
function normalizePlatform(raw: any): Platform {
const id = Number(raw?.id);
const key = String(raw?.key ?? "").trim();
const label = String(raw?.label ?? "").trim();
const activeRaw = raw?.isActive ?? raw?.is_active ?? true;
const isActive =
typeof activeRaw === "boolean"
? activeRaw
: typeof activeRaw === "number"
? activeRaw === 1
: String(activeRaw).toLowerCase() === "true";
return { id, key, label, isActive };
}
export async function fetchPlatforms(tokenHeaders: HeadersInit): Promise<Platform[]> {
const res = await fetch(`${API_BASE}/api/platforms`, {
headers: { ...tokenHeaders },
});
if (!res.ok) {
throw new Error(`Failed to load platforms (${res.status})`);
}
const data = (await res.json()) as unknown;
const items = Array.isArray(data) ? data : [];
return items.map(normalizePlatform).filter((p) => Number.isFinite(p.id) && p.key && p.label);
}
export async function createPlatform(
tokenHeaders: HeadersInit,
dto: CreatePlatformDto
): Promise<Platform> {
const res = await fetch(`${API_BASE}/api/platforms`, {
method: "POST",
headers: {
"Content-Type": "application/json",
...tokenHeaders,
},
body: JSON.stringify(dto),
});
if (!res.ok) {
const text = await res.text().catch(() => "");
throw new Error(text || `Failed to create platform (${res.status})`);
}
return normalizePlatform(await res.json());
}
export async function updatePlatform(
tokenHeaders: HeadersInit,
id: number,
dto: UpdatePlatformDto
): Promise<Platform> {
const res = await fetch(`${API_BASE}/api/platforms/${id}`, {
method: "PUT",
headers: {
"Content-Type": "application/json",
...tokenHeaders,
},
body: JSON.stringify(dto),
});
if (!res.ok) {
const text = await res.text().catch(() => "");
throw new Error(text || `Failed to update platform (${res.status})`);
}
return normalizePlatform(await res.json());
}
export async function deletePlatform(tokenHeaders: HeadersInit, id: number): Promise<void> {
const res = await fetch(`${API_BASE}/api/platforms/${id}`, {
method: "DELETE",
headers: { ...tokenHeaders },
});
if (!res.ok) {
const text = await res.text().catch(() => "");
throw new Error(text || `Failed to delete platform (${res.status})`);
}
}