switching fields and users calls to API proxy
This commit is contained in:
parent
b7b3fb53f9
commit
fda015531c
4 changed files with 132 additions and 62 deletions
|
|
@ -109,36 +109,15 @@ function useOptions(path: string) {
|
|||
};
|
||||
}
|
||||
} else if (rawPath === "repeater-choices") {
|
||||
// target=<collection>, group=<repeater field>, field=<child field>
|
||||
const group = params.get("group") || "";
|
||||
const field = params.get("field") || "";
|
||||
const collection = params.get("target") || "";
|
||||
|
||||
// Always go through our server proxy to read Directus field meta
|
||||
const proxyUrl = `/api/directus/fields?collection=${encodeURIComponent(collection)}`;
|
||||
const metaRes = await fetch(proxyUrl, { cache: "no-store" });
|
||||
if (!metaRes.ok) throw new Error(`Proxy ${metaRes.status} fetching ${proxyUrl}`);
|
||||
|
||||
const metaJson = await metaRes.json().catch(() => ({}));
|
||||
const rows: any[] = Array.isArray(metaJson?.data) ? metaJson.data : Array.isArray(metaJson) ? metaJson : [];
|
||||
|
||||
// Nested repeater children live under parent.meta.options.fields
|
||||
const parent = rows.find((r: any) => r?.field === group);
|
||||
const nestedChildren = parent?.meta?.options?.fields || [];
|
||||
let child =
|
||||
nestedChildren.find((f: any) => f?.field === field) ||
|
||||
rows.find((r: any) => r?.field === `${group}.${field}`); // flat fallback
|
||||
|
||||
// Choices may be 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),
|
||||
label: String(c.text ?? c.label ?? c.name ?? c.value),
|
||||
}));
|
||||
const proxy = `/api/directus/choices?collection=${encodeURIComponent(collection)}&group=${encodeURIComponent(group)}&field=${encodeURIComponent(field)}`;
|
||||
const r = await fetch(proxy, { cache: "no-store" });
|
||||
if (!r.ok) throw new Error(`Proxy ${r.status} fetching ${proxy}`);
|
||||
const j = await r.json().catch(() => ({}));
|
||||
const mapped: Opt[] = Array.isArray(j?.data) ? j.data : [];
|
||||
|
||||
if (alive) {
|
||||
const needle = (q || "").trim().toLowerCase();
|
||||
|
|
@ -146,7 +125,7 @@ function useOptions(path: string) {
|
|||
setOpts(filtered);
|
||||
setLoading(false);
|
||||
}
|
||||
return; // short-circuit
|
||||
return;
|
||||
|
||||
} else {
|
||||
// unknown path → empty
|
||||
|
|
@ -279,17 +258,18 @@ export default function SettingsSubmit({ initialTarget }: { initialTarget?: Targ
|
|||
|
||||
useEffect(() => {
|
||||
let alive = true;
|
||||
fetch(`${API}/users/me?fields=id,username,display_name,first_name,last_name,email`, {
|
||||
cache: "no-store",
|
||||
credentials: "include",
|
||||
})
|
||||
|
||||
fetch(`/api/auth/me`, { cache: "no-store", credentials: "include" })
|
||||
.then((r) => (r.ok ? r.json() : Promise.reject(r)))
|
||||
.then((j) => {
|
||||
if (alive) setMe(j?.data || j || null);
|
||||
if (!alive) return;
|
||||
// j is the user object directly (not wrapped in { data })
|
||||
setMe(j || null);
|
||||
})
|
||||
.catch(() => {
|
||||
if (alive) setMeErr("not-signed-in");
|
||||
});
|
||||
|
||||
return () => {
|
||||
alive = false;
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue