"use client"; import { useEffect, useState, ChangeEvent, FormEvent } from "react"; type Mapping = { id: number; feedname: string; affiliatecategory: string; buildercategoryid: number; notes?: string; }; type MappingForm = { feedname: string; affiliatecategory: string; buildercategoryid: string; notes?: string; }; export default function CategoryMappingAdmin() { const [mappings, setMappings] = useState([]); const [form, setForm] = useState({ feedname: "", affiliatecategory: "", buildercategoryid: "", notes: "" }); const [editingId, setEditingId] = useState(null); const [editForm, setEditForm] = useState({ feedname: "", affiliatecategory: "", buildercategoryid: "", notes: "" }); const [productCategories, setProductCategories] = useState([]); // Fetch all mappings const fetchMappings = async () => { const res = await fetch("/api/category-mapping"); const data = await res.json(); setMappings(data.data || []); }; const fetchProductCategories = async () => { const res = await fetch("/api/product-categories"); const data = await res.json(); setProductCategories(data.data || []); }; useEffect(() => { fetchMappings(); fetchProductCategories(); }, []); // Add new mapping const handleAdd = async (e: FormEvent) => { e.preventDefault(); await fetch("/api/category-mapping", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ ...form, buildercategoryid: parseInt(form.buildercategoryid, 10) }), }); setForm({ feedname: "", affiliatecategory: "", buildercategoryid: "", notes: "" }); fetchMappings(); }; // Edit mapping const handleEdit = (mapping: Mapping) => { setEditingId(mapping.id); setEditForm({ ...mapping, buildercategoryid: mapping.buildercategoryid.toString() }); }; const handleUpdate = async (e: FormEvent) => { e.preventDefault(); await fetch("/api/category-mapping", { method: "PUT", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ ...editForm, buildercategoryid: parseInt(editForm.buildercategoryid, 10), id: editingId }), }); setEditingId(null); fetchMappings(); }; // Delete mapping const handleDelete = async (id: number) => { await fetch("/api/category-mapping", { method: "DELETE", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ id }), }); fetchMappings(); }; return (

Affiliate Category Mapping Admin

AR15 Category Hierarchy

{productCategories .filter(cat => cat.type === 'AR15') .map(cat => { const parent = productCategories.find(p => p.id === cat.parent_category_id); return ( ); })}
ID Category Parent Type
{cat.id} {cat.name} {parent ? parent.name : 'Top Level'} {cat.type}
setForm(f => ({ ...f, feedname: e.target.value }))} required /> setForm(f => ({ ...f, affiliatecategory: e.target.value }))} required /> setForm(f => ({ ...f, buildercategoryid: e.target.value }))} required /> setForm(f => ({ ...f, notes: e.target.value }))} />
{mappings.map((m) => ( {editingId === m.id ? ( <> ) : ( <> )} ))}
Feed Name Affiliate Category Builder Category ID Notes Actions
setEditForm(f => ({ ...f, feedname: e.target.value }))} /> setEditForm(f => ({ ...f, affiliatecategory: e.target.value }))} /> setEditForm(f => ({ ...f, buildercategoryid: e.target.value }))} /> setEditForm(f => ({ ...f, notes: e.target.value }))} /> {m.feedname} {m.affiliatecategory} {m.buildercategoryid} {m.notes}
); }