From a4826ca5a80e8013b2e96f54017f9a3b569b0449 Mon Sep 17 00:00:00 2001 From: Ben Halverson Date: Sat, 7 Mar 2026 01:46:49 -0800 Subject: [PATCH] wip --- src/components/ColorPicker.test.tsx | 11 +++- src/components/ColorPicker.tsx | 33 ++++++++-- src/components/ColorPickerV2.tsx | 89 +++++++++++++++++++++++++++ src/components/ColorPickerWrapper.tsx | 19 ++++++ src/config.ts | 2 + src/context/ColorContext.tsx | 2 +- src/interfaces/colors.ts | 11 +++- src/pages/Product.test.tsx | 1 + src/pages/Product.tsx | 4 +- 9 files changed, 157 insertions(+), 15 deletions(-) create mode 100644 src/components/ColorPickerV2.tsx create mode 100644 src/components/ColorPickerWrapper.tsx diff --git a/src/components/ColorPicker.test.tsx b/src/components/ColorPicker.test.tsx index a3e2429..96fe97f 100644 --- a/src/components/ColorPicker.test.tsx +++ b/src/components/ColorPicker.test.tsx @@ -3,12 +3,17 @@ import userEvent from "@testing-library/user-event"; import { vi } from "vitest"; import ColorPicker from "../components/ColorPicker"; import { ColorProvider } from "../context/ColorContext"; -import type { ColorsResponse } from "../interfaces"; vi.mock("../config", () => ({ BASE_URL: "https://example.test" })); -// Mock data for color options -const mockColors: ColorsResponse[] = [ +// v1 /colors mock response shape +type V1ColorResponse = { + filament: string; + hexColor: string; + colorTag: string; +}; + +const mockColors: V1ColorResponse[] = [ { filament: "PLA", hexColor: "FF5733", colorTag: "Red" }, { filament: "PLA", hexColor: "33FF57", colorTag: "Green" }, { filament: "PLA", hexColor: "3357FF", colorTag: "Blue" }, diff --git a/src/components/ColorPicker.tsx b/src/components/ColorPicker.tsx index 24d9704..393b646 100644 --- a/src/components/ColorPicker.tsx +++ b/src/components/ColorPicker.tsx @@ -16,8 +16,29 @@ const ColorPicker: React.FC = ({ filamentType }) => { if (filamentType) url.searchParams.set("filamentType", filamentType); const response = await fetch(url.toString()); - const colors = (await response.json()) as ColorsResponse[]; - dispatch({ type: "SET_COLOR_OPTIONS", payload: colors }); + + // v1 /colors response shape + type V1ColorResponse = { + filament: string; + hexColor: string; + colorTag: string; + }; + + const rawColors = (await response.json()) as V1ColorResponse[]; + + const mappedColors: ColorsResponse[] = rawColors.map((c) => ({ + name: c.filament, + provider: "", // not provided by v1 API + public: true, + available: true, + profile: c.colorTag, + color: c.filament, + hexValue: `#${c.hexColor.replace(/^#/, "")}`, + publicId: c.colorTag, + })); + + console.log("Fetched colors (v1 mapped):", mappedColors); + dispatch({ type: "SET_COLOR_OPTIONS", payload: mappedColors }); } catch (error) { console.error("Failed to fetch colors:", error); } finally { @@ -40,8 +61,8 @@ const ColorPicker: React.FC = ({ filamentType }) => { className="flex items-center space-x-3"> {colorOptions?.map((colorOption, index) => ( `relative -m-0.5 flex cursor-pointer items-center justify-center rounded-full p-0.5 focus:outline-none ${ checked ? "ring-2 ring-offset-1 ring-blue-500" : "" @@ -52,9 +73,9 @@ const ColorPicker: React.FC = ({ filamentType }) => {