// app/admin/platforms/page.tsx "use client"; import { useEffect, useMemo, useState } from "react"; import { Plus, Pencil, Trash2, X } from "lucide-react"; import { useAuth } from "@/context/AuthContext"; import { createPlatform, deletePlatform, fetchPlatforms, updatePlatform, type Platform, type CreatePlatformDto, type UpdatePlatformDto, } from "@/lib/api/platforms"; export default function AdminPlatformsPage() { const { getAuthHeaders } = useAuth(); const [platforms, setPlatforms] = useState([]); const [loading, setLoading] = useState(true); const [saving, setSaving] = useState(false); const [error, setError] = useState(null); const [showModal, setShowModal] = useState(false); const [modalMode, setModalMode] = useState<"create" | "edit">("create"); const [selected, setSelected] = useState(null); const [form, setForm] = useState({ key: "", label: "", isActive: true, }); async function load() { try { setLoading(true); setError(null); const data = await fetchPlatforms(getAuthHeaders()); setPlatforms(data); } catch (e: any) { console.error(e); setError(e?.message ?? "Failed to load platforms"); setPlatforms([]); } finally { setLoading(false); } } useEffect(() => { void load(); // eslint-disable-next-line react-hooks/exhaustive-deps }, []); const sorted = useMemo(() => { return [...platforms].sort((a, b) => a.key.localeCompare(b.key)); }, [platforms]); function openCreate() { setModalMode("create"); setSelected(null); setForm({ key: "", label: "", isActive: true }); setShowModal(true); } function openEdit(p: Platform) { setModalMode("edit"); setSelected(p); setForm({ key: p.key, label: p.label, isActive: p.isActive }); setShowModal(true); } async function onDelete(p: Platform) { if (!window.confirm(`Delete platform "${p.key}"? This cannot be undone.`)) return; try { setSaving(true); setError(null); await deletePlatform(getAuthHeaders(), p.id); await load(); } catch (e: any) { console.error(e); setError(e?.message ?? "Failed to delete platform"); } finally { setSaving(false); } } async function onSubmit(e: React.FormEvent) { e.preventDefault(); const key = (form.key ?? "").trim(); const label = (form.label ?? "").trim(); if (!key || !label) { setError("Key and Label are required."); return; } try { setSaving(true); setError(null); if (modalMode === "create") { await createPlatform(getAuthHeaders(), { ...form, key, label }); } else if (selected) { const update: UpdatePlatformDto = {}; if (key !== selected.key) update.key = key; if (label !== selected.label) update.label = label; if ((form.isActive ?? true) !== selected.isActive) update.isActive = form.isActive; await updatePlatform(getAuthHeaders(), selected.id, update); } setShowModal(false); await load(); } catch (e: any) { console.error(e); setError(e?.message ?? "Failed to save platform"); } finally { setSaving(false); } } return (

Platforms

Manage canonical platforms used across products and filtering.

{error && (
{error}
)} {loading ? (
Loading…
) : sorted.length === 0 ? (
No platforms found.
) : (
{sorted.map((p, idx) => ( ))}
ID Key Label Active Actions
{p.id} {p.key} {p.label} {p.isActive ? "ACTIVE" : "INACTIVE"}
)} {showModal && (

{modalMode === "create" ? "Create Platform" : "Edit Platform"}

setForm((s) => ({ ...s, key: e.target.value .toUpperCase() .replace(/\s+/g, "") // remove spaces .replace(/-/g, ""), })) } className="w-full rounded-md border border-zinc-800 bg-zinc-900 px-3 py-2 text-sm text-zinc-100 placeholder-zinc-500 focus:border-amber-400/80 focus:outline-none focus:ring-1 focus:ring-amber-400" placeholder="AR15" required />
setForm((s) => ({ ...s, label: e.target.value })) } className="w-full rounded-md border border-zinc-800 bg-zinc-900 px-3 py-2 text-sm text-zinc-100 placeholder-zinc-500 focus:border-amber-400/80 focus:outline-none focus:ring-1 focus:ring-amber-400" placeholder="AR-15" required />
)}
); }