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>
|
// target=<collection>, group=<repeater field>, field=<child field>
|
||||||
const group = params.get("group") || "";
|
const group = params.get("group") || "";
|
||||||
const field = params.get("field") || "";
|
const field = params.get("field") || "";
|
||||||
|
const collection = params.get("target") || "";
|
||||||
|
|
||||||
// 1) use filter endpoint form
|
// 1) Preferred: /fields/{collection}
|
||||||
const fieldsUrl = `${API}/fields?filter[collection][_eq]=${encodeURIComponent(target)}`;
|
let fieldsUrl = `${API}/fields/${encodeURIComponent(collection)}`;
|
||||||
const metaRes = await fetch(fieldsUrl, { cache: "no-store", credentials: "include" });
|
let rows: any[] = [];
|
||||||
if (!metaRes.ok) throw new Error(`Directus ${metaRes.status} fetching ${fieldsUrl}`);
|
|
||||||
const metaJson = await metaRes.json();
|
|
||||||
const rows = metaJson?.data ?? [];
|
|
||||||
|
|
||||||
// 2) read children from options.fields (not meta)
|
// Fetch with NO credentials (public-readable)
|
||||||
const parent = rows.find((r: any) => r?.field === group);
|
let metaRes = await fetch(fieldsUrl, { cache: "no-store" });
|
||||||
const children = (parent?.options?.fields as any[]) || [];
|
if (metaRes.ok) {
|
||||||
let child = children.find((f: any) => f?.field === field);
|
const j = await metaRes.json().catch(() => ({}));
|
||||||
|
rows = Array.isArray(j?.data) ? j.data : Array.isArray(j) ? j : [];
|
||||||
// Approach 2: fallback to flat "group.field" entry if present
|
} else {
|
||||||
if (!child) {
|
// 2) Fallback: /fields?filter[collection][_eq]=...
|
||||||
const full = `${group}.${field}`;
|
fieldsUrl = `${API}/fields?filter[collection][_eq]=${encodeURIComponent(collection)}`;
|
||||||
child = rows.find((r: any) => r?.field === full);
|
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
|
// Try nested child first: parent.meta.options.fields -> child where field === <field>
|
||||||
const choices: any[] = (child?.options?.choices as any[]) || [];
|
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) => ({
|
const mapped: Opt[] = choices.map((c: any) => ({
|
||||||
id: String(c.value ?? c.key ?? c.id),
|
id: String(c.value ?? c.key ?? c.id),
|
||||||
|
|
@ -145,7 +157,8 @@ function useOptions(path: string) {
|
||||||
setOpts(filtered);
|
setOpts(filtered);
|
||||||
setLoading(false);
|
setLoading(false);
|
||||||
}
|
}
|
||||||
return; // short-circuit: no fetch below
|
return; // short-circuit
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
setOpts([]);
|
setOpts([]);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue