diff --git a/app/api/directus/fields/route.ts b/app/api/directus/fields/route.ts new file mode 100644 index 00000000..e69de29b diff --git a/app/components/forms/SettingsSubmit.tsx b/app/components/forms/SettingsSubmit.tsx index 6d2fad2a..0cf9cc60 100644 --- a/app/components/forms/SettingsSubmit.tsx +++ b/app/components/forms/SettingsSubmit.tsx @@ -107,38 +107,28 @@ function useOptions(path: string) { }); }; } - } else if (rawPath === "repeater-choices") { // target=, group=, field= const group = params.get("group") || ""; const field = params.get("field") || ""; const collection = params.get("target") || ""; - // 1) Preferred: /fields/{collection} - let fieldsUrl = `${API}/fields/${encodeURIComponent(collection)}`; - let rows: any[] = []; - - // Fetch with NO credentials (public-readable) - let metaRes = await fetch(fieldsUrl, { cache: "no-store" }); - if (metaRes.ok) { - const j = await metaRes.json().catch(() => ({})); - rows = Array.isArray(j?.data) ? j.data : Array.isArray(j) ? j : []; - } else { - // 2) Fallback: /fields?filter[collection][_eq]=... - fieldsUrl = `${API}/fields?filter[collection][_eq]=${encodeURIComponent(collection)}`; - metaRes = await fetch(fieldsUrl, { cache: "no-store" }); - if (!metaRes.ok) throw new Error(`Directus ${metaRes.status} fetching ${fieldsUrl}`); - const j = await metaRes.json().catch(() => ({})); - rows = Array.isArray(j?.data) ? j.data : Array.isArray(j) ? j : []; - } + // 🔁 CHANGE: fetch fields via server proxy (uses Directus token server-side) + const metaRes = await fetch( + `/api/directus/fields?collection=${encodeURIComponent(collection)}`, + { cache: "no-store" } + ); + if (!metaRes.ok) throw new Error(`Fields ${metaRes.status}`); + const metaJson = await metaRes.json().catch(() => ({})); + const rows: any[] = Array.isArray(metaJson?.data) ? metaJson.data : []; // Try nested child first: parent.meta.options.fields -> child where field === const parent = rows.find((r: any) => r?.field === group); const nestedChildren = parent?.meta?.options?.fields || []; let child = - nestedChildren.find((f: any) => f?.field === field) + nestedChildren.find((f: any) => f?.field === field) || // Flat fallback: "group.field" - || rows.find((r: any) => r?.field === `${group}.${field}`); + rows.find((r: any) => r?.field === `${group}.${field}`); // Choices can live on child.options.choices or child.meta.options.choices const choices: any[] = @@ -158,9 +148,8 @@ function useOptions(path: string) { setLoading(false); } return; // short-circuit - } else { - + // unknown path → empty setOpts([]); setLoading(false); return; @@ -315,7 +304,7 @@ export default function SettingsSubmit({ initialTarget }: { initialTarget?: Targ // Options const mats = useOptions("material"); - const coats = useOptions("material_coating"); + the coats = useOptions("material_coating"); const colors = useOptions("material_color"); const opacs = useOptions("material_opacity"); const soft = useOptions("laser_software"); // required for ALL targets