Skip to content

Commit

Permalink
fix: count query where
Browse files Browse the repository at this point in the history
  • Loading branch information
kierianlee committed Feb 24, 2024
1 parent b9d7bab commit 120156a
Showing 1 changed file with 13 additions and 14 deletions.
27 changes: 13 additions & 14 deletions src/app/(with-layout)/dashboard/[id]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export default async function Dashboard({
}) {
const session = await getServerSession(authOptions);
if (!session) {
return <></>;
return redirect("/");
}

const page = parseInt(searchParams.page?.toString() || "1");
Expand Down Expand Up @@ -62,23 +62,22 @@ export default async function Dashboard({
redirect("/dashboard");
}

const where = and(
eq(submissionsTable.formId, form.id),
...(filterField && filterType && filterValue
? [
filterType === QueryType.CONTAINS
? sql`fields ->> ${filterField} ilike ${`%${filterValue}%`}`
: sql`fields ->> ${filterField} = ${filterValue}`,
]
: []),
);
const [submissions, [{ count: submissionsCount }], submissionKeys] =
await Promise.all([
db
.select()
.from(submissionsTable)
.where(
and(
eq(submissionsTable.formId, form.id),
...(filterField && filterType && filterValue
? [
filterType === QueryType.CONTAINS
? sql`fields ->> ${filterField} ilike ${`%${filterValue}%`}`
: sql`fields ->> ${filterField} = ${filterValue}`,
]
: []),
),
)
.where(where)
.orderBy(
sortField === "Date"
? sortDir === "asc"
Expand All @@ -90,7 +89,7 @@ export default async function Dashboard({
)
.limit(take)
.offset((page - 1) * take),
db.select({ count: count() }).from(submissionsTable),
db.select({ count: count() }).from(submissionsTable).where(where),
db
.execute(
sql`
Expand Down

0 comments on commit 120156a

Please # to comment.