submit 'type' fixes
This commit is contained in:
parent
e966f8551d
commit
c0b9e017f0
1 changed files with 31 additions and 18 deletions
|
|
@ -112,27 +112,39 @@ function useOptions(path: string) {
|
|||
// target=<collection>, group=<repeater field>, field=<child field>
|
||||
const group = params.get("group") || "";
|
||||
const field = params.get("field") || "";
|
||||
const collection = params.get("target") || "";
|
||||
|
||||
// 1) use filter endpoint form
|
||||
const fieldsUrl = `${API}/fields?filter[collection][_eq]=${encodeURIComponent(target)}`;
|
||||
const metaRes = await fetch(fieldsUrl, { cache: "no-store", credentials: "include" });
|
||||
if (!metaRes.ok) throw new Error(`Directus ${metaRes.status} fetching ${fieldsUrl}`);
|
||||
const metaJson = await metaRes.json();
|
||||
const rows = metaJson?.data ?? [];
|
||||
// 1) Preferred: /fields/{collection}
|
||||
let fieldsUrl = `${API}/fields/${encodeURIComponent(collection)}`;
|
||||
let rows: any[] = [];
|
||||
|
||||
// 2) read children from options.fields (not meta)
|
||||
const parent = rows.find((r: any) => r?.field === group);
|
||||
const children = (parent?.options?.fields as any[]) || [];
|
||||
let child = children.find((f: any) => f?.field === field);
|
||||
|
||||
// Approach 2: fallback to flat "group.field" entry if present
|
||||
if (!child) {
|
||||
const full = `${group}.${field}`;
|
||||
child = rows.find((r: any) => r?.field === full);
|
||||
// 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 : [];
|
||||
}
|
||||
|
||||
// 3) choices from options.choices only
|
||||
const choices: any[] = (child?.options?.choices as any[]) || [];
|
||||
// Try nested child first: parent.meta.options.fields -> child where field === <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)
|
||||
// Flat fallback: "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[] =
|
||||
(child?.options?.choices as any[]) ??
|
||||
(child?.meta?.options?.choices as any[]) ??
|
||||
[];
|
||||
|
||||
const mapped: Opt[] = choices.map((c: any) => ({
|
||||
id: String(c.value ?? c.key ?? c.id),
|
||||
|
|
@ -145,7 +157,8 @@ function useOptions(path: string) {
|
|||
setOpts(filtered);
|
||||
setLoading(false);
|
||||
}
|
||||
return; // short-circuit: no fetch below
|
||||
return; // short-circuit
|
||||
|
||||
} else {
|
||||
|
||||
setOpts([]);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue