Skip to content

Commit

Permalink
Make list endpoint accept an optional parameter for filtering (#82)
Browse files Browse the repository at this point in the history
* created new branch

* feat: implemented validation

* feat: draft of filtering

* fix: filtering

* fix: fixed searchparams

* fix: fixed linting issues

* fix: fixed formatting

* Update packages/api/src/router/review.ts

Co-authored-by: Rishikesh Kanabar <[email protected]>

---------

Co-authored-by: Rishikesh Kanabar <[email protected]>
  • Loading branch information
gpalmer27 and RishikeshNK authored Oct 4, 2024
1 parent 826fb24 commit ff00668
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 9 deletions.
22 changes: 19 additions & 3 deletions apps/web/src/app/(pages)/(dashboard)/roles/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,32 @@

import { useState } from "react";

import type { ReviewType } from "@cooper/db/schema";
import type {
ReviewType,
WorkEnvironmentType,
WorkTermType,
} from "@cooper/db/schema";
import { cn } from "@cooper/ui";

import { ReviewCard } from "~/app/_components/reviews/review-card";
import { ReviewCardPreview } from "~/app/_components/reviews/review-card-preview";
import SearchFilter from "~/app/_components/search/search-filter";
import { api } from "~/trpc/react";

export default function Roles() {
const reviews = api.review.list.useQuery();
export default function Roles({
searchParams,
}: {
searchParams?: {
workTerm?: WorkTermType;
workEnvironment?: WorkEnvironmentType;
};
}) {
const reviews = api.review.list.useQuery({
options: {
cycle: searchParams?.workTerm,
term: searchParams?.workEnvironment,
},
});

const [selectedReview, setSelectedReview] = useState<ReviewType | undefined>(
reviews.data ? reviews.data[0] : undefined,
Expand Down
31 changes: 25 additions & 6 deletions packages/api/src/router/review.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,36 @@
import type { TRPCRouterRecord } from "@trpc/server";
import { z } from "zod";

import { desc, eq } from "@cooper/db";
import { and, desc, eq } from "@cooper/db";
import { CreateReviewSchema, Review } from "@cooper/db/schema";

import { protectedProcedure, publicProcedure } from "../trpc";

export const reviewRouter = {
list: publicProcedure.query(({ ctx }) => {
return ctx.db.query.Review.findMany({
orderBy: desc(Review.id),
});
}),
list: publicProcedure
.input(
z.object({
options: z
.object({
cycle: z.enum(["SPRING", "FALL", "SUMMER"]).optional(),
term: z.enum(["INPERSON", "HYBRID", "REMOTE"]).optional(),
})
.optional(),
}),
)
.query(({ ctx, input }) => {
const { options } = input;

const conditions = [
options?.cycle && eq(Review.workTerm, options.cycle),
options?.term && eq(Review.workEnvironment, options.term),
].filter(Boolean);

return ctx.db.query.Review.findMany({
orderBy: desc(Review.id),
where: conditions.length > 0 ? and(...conditions) : undefined,
});
}),

getByRole: publicProcedure
.input(z.object({ id: z.string() }))
Expand Down

0 comments on commit ff00668

Please sign in to comment.