role-mapping-fixes

This commit is contained in:
2025-12-06 20:08:00 -05:00
parent 05b30d8682
commit caf4817d12
2 changed files with 8 additions and 15 deletions
+7 -14
View File
@@ -4,21 +4,19 @@ import { useEffect, useMemo, useState } from "react";
import Link from "next/link"; import Link from "next/link";
import { useParams, useRouter } from "next/navigation"; import { useParams, useRouter } from "next/navigation";
import { CATEGORIES } from "@/data/gunbuilderParts"; import { CATEGORIES } from "@/data/gunbuilderParts";
import { CATEGORY_TO_PART_ROLES } from "@/data/partRoleMappings";
import type { CategoryId, Part } from "@/types/gunbuilder"; import type { CategoryId, Part } from "@/types/gunbuilder";
type ViewMode = "card" | "list"; type ViewMode = "card" | "list";
type GunbuilderProductFromApi = { type GunbuilderProductFromApi = {
id: string; id: number;
name: string; name: string;
brand: string; brand: string;
platform: string; platform: string;
partRole: string; partRole: string;
price: number | null; price: number | null;
mainImageUrl: string | null; imageUrl: string | null;
buyUrl: string | null; buyUrl: string | null;
// optional stock flag if/when backend sends it
inStock?: boolean | null; inStock?: boolean | null;
}; };
@@ -83,19 +81,14 @@ export default function CategoryPage() {
async function fetchCategoryParts() { async function fetchCategoryParts() {
try { try {
setLoading(true); setLoading(true);
setError(null); setError(null);
// FIX: determine which backend partRoles map to this category
const roles = CATEGORY_TO_PART_ROLES[categoryId] ?? [];
const search = new URLSearchParams(); const search = new URLSearchParams();
search.set("platform", "AR-15"); search.set("platform", "AR-15");
roles.forEach((r) => search.append("partRoles", r)); search.set("categorySlug", categoryId);
const url = `${API_BASE_URL}/api/products/gunbuilder${ const url = `${API_BASE_URL}/api/gunbuilder/products/by-category?${search.toString()}`;
roles.length ? `?${search.toString()}` : ""
}`;
const res = await fetch(url, { signal: controller.signal }); const res = await fetch(url, { signal: controller.signal });
@@ -106,12 +99,12 @@ export default function CategoryPage() {
const data: GunbuilderProductFromApi[] = await res.json(); const data: GunbuilderProductFromApi[] = await res.json();
const normalized: UiPart[] = data.map((p) => ({ const normalized: UiPart[] = data.map((p) => ({
id: p.id, id: String(p.id), // normalize to string for BuildState
categoryId, categoryId,
name: p.name, name: p.name,
brand: p.brand, brand: p.brand,
price: p.price ?? 0, price: p.price ?? 0,
imageUrl: p.mainImageUrl ?? undefined, imageUrl: p.imageUrl ?? undefined, // match backend field name
url: p.buyUrl ?? undefined, url: p.buyUrl ?? undefined,
notes: undefined, notes: undefined,
inStock: p.inStock ?? true, inStock: p.inStock ?? true,
+1 -1
View File
@@ -10,7 +10,7 @@ export const CATEGORY_TO_PART_ROLES: Record<CategoryId, string[]> = {
buffer: ["buffer-kit"], buffer: ["buffer-kit"],
lowerParts: ["lower-parts-kit"], lowerParts: ["lower-parts-kit"],
sights: ["sight"], sights: ["sight"],
lower: ["lower-receiver"], lower: ["LOWER_RECEIVER_STRIPPED"],
optic: ["optic"], optic: ["optic"],
stock: ["stock"], stock: ["stock"],