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