Skip to content

Commit cc08736

Browse files
authored
[feat 🔥] Search With Skills
1 parent 9aa1a3a commit cc08736

1 file changed

Lines changed: 18 additions & 16 deletions

File tree

src/components/contents/ProfileGrid.tsx

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { db } from "@/database";
33
import { users } from "@/schemas";
44
import ProfileCard from "./ProfileCard";
55
import Paginations from "../pagination/Paginations";
6-
import { ilike, or, sql } from "drizzle-orm";
6+
import { ilike, or, sql, arrayContains } from "drizzle-orm";
77
import Notfound from "../shared/Notfound";
88
import Search from "../shared/search/Search";
99

@@ -16,27 +16,29 @@ const ProfileGrid = async ({ page, searchParams }: Props) => {
1616
const currentPage = parseInt(page); // like 1
1717
const itemPerPage = 5; // we want to show 5 item in per pages
1818
const offset = (currentPage - 1) * itemPerPage; // (1 - 1) * 3 = 0
19+
const searchSkills = searchParams ? searchParams.split(',') : []; // Assuming skills are searched by a comma-separated string in searchParams
1920

2021
const [lengths, profiles] = await Promise.all([
2122
db.select({ count: sql<number>`count(*)` }).from(users),
2223
searchParams
2324
? db
24-
.select()
25-
.from(users)
26-
.where(
27-
or(
28-
ilike(users.name, `%${searchParams}%`),
29-
ilike(users.location, `%${searchParams}%`),
30-
),
31-
)
32-
.limit(itemPerPage)
33-
.offset(offset)
25+
.select()
26+
.from(users)
27+
.where(
28+
or(
29+
ilike(users.name, `%${searchParams}%`),
30+
ilike(users.location, `%${searchParams}%`),
31+
...(searchSkills.length > 0 ? [arrayContains(users.skills, searchSkills)] : [])
32+
),
33+
)
34+
.limit(itemPerPage)
35+
.offset(offset)
3436
: db
35-
.select()
36-
.from(users)
37-
.orderBy(sql.raw("RANDOM()"))
38-
.limit(itemPerPage)
39-
.offset(offset),
37+
.select()
38+
.from(users)
39+
.orderBy(sql.raw("RANDOM()"))
40+
.limit(itemPerPage)
41+
.offset(offset),
4042
]);
4143
const count = lengths[0].count;
4244
return (

0 commit comments

Comments
 (0)