fixed a hydratioin issue
This commit is contained in:
@@ -420,21 +420,22 @@ export default function GunbuilderPage() {
|
|||||||
|
|
||||||
const data: GunbuilderProductFromApi[] = await res.json();
|
const data: GunbuilderProductFromApi[] = await res.json();
|
||||||
|
|
||||||
const hydrated: Part[] = data
|
// Map products by id for quick lookup
|
||||||
.map((p): Part | null => {
|
const byId = new Map<string, GunbuilderProductFromApi>();
|
||||||
const normalizedRole = (p.partRole ?? "")
|
for (const p of data) byId.set(String(p.id), p);
|
||||||
.trim()
|
|
||||||
.toLowerCase()
|
|
||||||
.replace(/_/g, "-");
|
|
||||||
|
|
||||||
const categoryId = resolveCategoryId(normalizedRole);
|
// Build the hydrated parts by iterating the BUILD STATE (slot -> id)
|
||||||
if (!categoryId) return null;
|
const hydrated: Part[] = Object.entries(build)
|
||||||
|
.filter(([, id]) => Boolean(id))
|
||||||
|
.map(([slot, id]) => {
|
||||||
|
const p = byId.get(String(id));
|
||||||
|
if (!p) return null;
|
||||||
|
|
||||||
const buyUrl = p.buyUrl ?? undefined;
|
const buyUrl = p.buyUrl ?? undefined;
|
||||||
|
|
||||||
return {
|
return {
|
||||||
id: String(p.id),
|
id: String(p.id),
|
||||||
categoryId,
|
categoryId: slot as any, // ✅ slot is the source of truth
|
||||||
name: p.name,
|
name: p.name,
|
||||||
brand: p.brand,
|
brand: p.brand,
|
||||||
price: p.price ?? 0,
|
price: p.price ?? 0,
|
||||||
@@ -442,11 +443,12 @@ export default function GunbuilderPage() {
|
|||||||
affiliateUrl: buyUrl,
|
affiliateUrl: buyUrl,
|
||||||
url: buyUrl,
|
url: buyUrl,
|
||||||
notes: undefined,
|
notes: undefined,
|
||||||
};
|
} as Part;
|
||||||
})
|
})
|
||||||
.filter((p): p is Part => p !== null);
|
.filter((x): x is Part => x !== null);
|
||||||
|
|
||||||
setParts(hydrated);
|
setParts(hydrated);
|
||||||
|
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
if (err?.name === "AbortError") return;
|
if (err?.name === "AbortError") return;
|
||||||
setError(err?.message ?? "Failed to hydrate selected parts");
|
setError(err?.message ?? "Failed to hydrate selected parts");
|
||||||
|
|||||||
Reference in New Issue
Block a user