From 944324ffef31ff4198ac60f6773903cc9a8c746e Mon Sep 17 00:00:00 2001 From: Rizky Date: Fri, 12 Jul 2024 22:05:00 -0700 Subject: [PATCH] fix search has many --- comps/filter/parser/single-filter.ts | 81 +++++++++++++++------------- comps/form/base/BaseForm.tsx | 2 - 2 files changed, 45 insertions(+), 38 deletions(-) diff --git a/comps/filter/parser/single-filter.ts b/comps/filter/parser/single-filter.ts index b633f70..c139ad8 100755 --- a/comps/filter/parser/single-filter.ts +++ b/comps/filter/parser/single-filter.ts @@ -12,42 +12,7 @@ export const parseSingleFilter = (filter: FilterLocal, fields: GFCol[]) => { switch (type) { case "search-all": - fields - .filter( - (e) => - e.type === "varchar" || - e.type === "text" || - e.type === "string" || - e.type === "has-one" - ) - .map((e) => { - if (e.type === "has-one") { - for (const f of e.relation?.fields || []) { - if ( - !f.is_pk && - (f.type === "varchar" || - f.type === "text" || - f.type === "string") - ) { - OR.push({ - [e.name]: { - [f.name]: { - contains: "%" + value + "%", - mode: "insensitive", - }, - }, - }); - } - } - } else { - OR.push({ - [e.name]: { - contains: "%" + value + "%", - mode: "insensitive", - }, - }); - } - }); + searchAll(fields, OR, value); break; case "text": { @@ -138,3 +103,47 @@ export const parseSingleFilter = (filter: FilterLocal, fields: GFCol[]) => { if (OR.length > 0) where.OR = OR; return where; }; + +const searchAll = (fields: GFCol[], OR: any[], value: any) => { + fields + .filter( + (e) => + e.type === "varchar" || + e.type === "text" || + e.type === "string" || + e.type === "has-one" || + e.type === "has-many" + ) + .map((e) => { + if (e.type === "has-one") { + for (const f of e.relation?.fields || []) { + if ( + !f.is_pk && + (f.type === "varchar" || f.type === "text" || f.type === "string") + ) { + OR.push({ + [e.name]: { + [f.name]: { + contains: "%" + value + "%", + mode: "insensitive", + }, + }, + }); + } + } + } else if (e.type === "has-many") { + const MANY_OR: any[] = []; + if (e.relation) { + searchAll(e.relation.fields, MANY_OR, value); + OR.push({ [e.name]: { some: { OR: MANY_OR } } }); + } + } else { + OR.push({ + [e.name]: { + contains: "%" + value + "%", + mode: "insensitive", + }, + }); + } + }); +}; diff --git a/comps/form/base/BaseForm.tsx b/comps/form/base/BaseForm.tsx index 432a05e..76a69da 100755 --- a/comps/form/base/BaseForm.tsx +++ b/comps/form/base/BaseForm.tsx @@ -22,11 +22,9 @@ export const BaseForm = >( } form.submit = useCallback(async () => { - console.log("CEK"); if (form.status === "ready") { form.status = "submitting"; form.render(); - console.log("CEK"); const result = await on_submit?.(form); setTimeout(() => {