From c1246a605aa775a60b38221a91feb706e4b3e808 Mon Sep 17 00:00:00 2001 From: Nikolay Bonev Date: Wed, 16 Oct 2024 16:50:42 +0300 Subject: [PATCH] fixing whereClause for search & clear search button empty state --- app/components/list/filters/clear-search.tsx | 24 +++++++++++++++---- app/components/list/index.tsx | 2 +- .../asset/advanced-index-query.server.ts | 10 ++++++-- app/modules/asset/service.server.ts | 1 - 4 files changed, 28 insertions(+), 9 deletions(-) diff --git a/app/components/list/filters/clear-search.tsx b/app/components/list/filters/clear-search.tsx index f368aa952..f7168c6e8 100644 --- a/app/components/list/filters/clear-search.tsx +++ b/app/components/list/filters/clear-search.tsx @@ -1,5 +1,6 @@ import type { ButtonProps } from "~/components/shared/button"; import { Button } from "~/components/shared/button"; +import { useSearchParams } from "~/hooks/search-params"; export const ClearSearch = ({ buttonProps, @@ -7,8 +8,21 @@ export const ClearSearch = ({ }: { buttonProps?: ButtonProps; children?: string; -}) => ( - -); +}) => { + const [, setSearchParams] = useSearchParams(); + return ( + + ); +}; diff --git a/app/components/list/index.tsx b/app/components/list/index.tsx index 8a6bce957..b8dca9ea8 100644 --- a/app/components/list/index.tsx +++ b/app/components/list/index.tsx @@ -100,7 +100,7 @@ export const List = React.forwardRef(function List( const { items, totalItems, perPage, modelName } = useLoaderData(); const { singular, plural } = modelName; - const totalIncomingItems = items.length; + const totalIncomingItems = items?.length; const hasItems = totalIncomingItems > 0; const selectedBulkItemsCount = useAtomValue(selectedBulkItemsCountAtom); const setSelectedBulkItems = useSetAtom(setSelectedBulkItemsAtom); diff --git a/app/modules/asset/advanced-index-query.server.ts b/app/modules/asset/advanced-index-query.server.ts index e95a764b2..e1aacd00d 100644 --- a/app/modules/asset/advanced-index-query.server.ts +++ b/app/modules/asset/advanced-index-query.server.ts @@ -12,8 +12,14 @@ export function generateWhereClause( if (search) { const words = search.trim().split(/\s+/).filter(Boolean); if (words.length > 0) { - const searchVector = words.join(" & "); - whereClause = Prisma.sql`${whereClause} AND (to_tsvector('english', a."title" || ' ' || COALESCE(a."description", '')) @@ to_tsquery('english', ${searchVector}))`; + const searchQuery = words + .map((word) => `${word}:* | ${word}`) + .join(" | "); + whereClause = Prisma.sql` + ${whereClause} AND ( + to_tsvector('english', a."title" || ' ' || COALESCE(a."description", '')) @@ to_tsquery('english', ${searchQuery}) + ) + `; } } diff --git a/app/modules/asset/service.server.ts b/app/modules/asset/service.server.ts index 61d9ab6c6..d6d302cae 100644 --- a/app/modules/asset/service.server.ts +++ b/app/modules/asset/service.server.ts @@ -724,7 +724,6 @@ export async function getAdvancedPaginatedAndFilterableAssets({ ${assetReturnFragment} FROM sorted_asset_query aq; `; - const result = await db.$queryRaw(query); const totalAssets = result[0].total_count; const assets: AdvancedIndexAsset[] = result[0].assets;