Skip to content

Commit

Permalink
fixing whereClause for search & clear search button empty state
Browse files Browse the repository at this point in the history
  • Loading branch information
DonKoko committed Oct 16, 2024
1 parent 17e0994 commit c1246a6
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 9 deletions.
24 changes: 19 additions & 5 deletions app/components/list/filters/clear-search.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,28 @@
import type { ButtonProps } from "~/components/shared/button";
import { Button } from "~/components/shared/button";
import { useSearchParams } from "~/hooks/search-params";

export const ClearSearch = ({
buttonProps,
children,
}: {
buttonProps?: ButtonProps;
children?: string;
}) => (
<Button to="." {...buttonProps}>
{children}
</Button>
);
}) => {
const [, setSearchParams] = useSearchParams();
return (
<Button
to="."
{...buttonProps}
onClick={() => {
setSearchParams((prev) => {
prev.delete("s");

return prev;
});
}}
>
{children}
</Button>
);
};
2 changes: 1 addition & 1 deletion app/components/list/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ export const List = React.forwardRef<HTMLDivElement, ListProps>(function List(
const { items, totalItems, perPage, modelName } =
useLoaderData<IndexResponse>();
const { singular, plural } = modelName;
const totalIncomingItems = items.length;
const totalIncomingItems = items?.length;
const hasItems = totalIncomingItems > 0;
const selectedBulkItemsCount = useAtomValue(selectedBulkItemsCountAtom);
const setSelectedBulkItems = useSetAtom(setSelectedBulkItemsAtom);
Expand Down
10 changes: 8 additions & 2 deletions app/modules/asset/advanced-index-query.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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})
)
`;
}
}

Expand Down
1 change: 0 additions & 1 deletion app/modules/asset/service.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -724,7 +724,6 @@ export async function getAdvancedPaginatedAndFilterableAssets({
${assetReturnFragment}
FROM sorted_asset_query aq;
`;

const result = await db.$queryRaw<AdvancedIndexQueryResult>(query);
const totalAssets = result[0].total_count;
const assets: AdvancedIndexAsset[] = result[0].assets;
Expand Down

0 comments on commit c1246a6

Please sign in to comment.