Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make list endpoint accept an optional parameter for filtering #82

Merged
merged 8 commits into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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