Skip to content

Commit 81d41b3

Browse files
committed
Add title and difficulty filter
1 parent 96be35c commit 81d41b3

File tree

3 files changed

+78
-9
lines changed

3 files changed

+78
-9
lines changed

backend/question/src/routes/questionRoutes.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,10 @@ router.post("/all", async (req: Request, res: Response) => {
6565
const { title, complexity, category } = req.body;
6666

6767
const query: any = { deleted: false };
68-
if (title) query.title = { $regex: title, $options: "i" };
69-
if (complexity) query.complexity = { $in: complexity };
70-
if (category) query.category = { $in: category };
68+
if (title !== "") query.title = { $regex: title, $options: "i" };
69+
if (complexity && complexity.length > 0)
70+
query.complexity = { $in: complexity };
71+
if (category && category.length > 0) query.category = { $in: category };
7172

7273
try {
7374
const questions = await Question.find(query, {

frontend/src/api/leetcode-dashboard.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ export const createSingleLeetcodeQuestion = async (
2828

2929
export const getLeetcodeDashboardData = async (
3030
pagination: number,
31-
pageSize: number
31+
pageSize: number,
32+
title: string,
33+
complexity: string[]
3234
): Promise<QuestionAll> => {
3335
const url = `${QUESTION_SERVICE}/all`;
3436
const response = await fetch(url, {
@@ -39,6 +41,8 @@ export const getLeetcodeDashboardData = async (
3941
body: JSON.stringify({
4042
pagination: pagination,
4143
pageSize: pageSize,
44+
title: title,
45+
complexity: complexity,
4246
}),
4347
});
4448
const data = await response.json();

frontend/src/app/(auth)/leetcode-dashboard/LeetcodeDashboardTable.tsx

Lines changed: 69 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,17 @@ import {
2626
deleteSingleLeetcodeQuestion,
2727
getLeetcodeDashboardData,
2828
} from "@/api/leetcode-dashboard";
29-
import { QuestionMinified } from "@/types/find-match";
29+
import { QuestionDifficulty, QuestionMinified } from "@/types/find-match";
3030
import MoonLoader from "react-spinners/MoonLoader";
3131
import EditQuestionDialog from "@/app/(auth)/leetcode-dashboard/EditQuestionDialog";
3232
import { motion } from "framer-motion";
3333
import Modal from "react-modal";
3434
import Swal from "sweetalert2";
3535
import { useEffect, useState } from "react";
36+
import { Input } from "@/components/ui/input";
37+
import { ListFilter, Search } from "lucide-react";
38+
import { MultiSelect } from "@/components/ui/multiselect";
39+
import { capitalizeWords } from "@/utils/string_utils";
3640

3741
const Cell = ({
3842
className,
@@ -105,6 +109,20 @@ export function LeetcodeDashboardTable({
105109
});
106110

107111
const [totalPages, setTotalPage] = React.useState<number>(1);
112+
const [searchTitle, setSearchTitle] = React.useState<string>("");
113+
const [searchDifficulty, setSearchDifficulty] = React.useState<string[]>([]);
114+
const [isFilterOpen, setIsFilterOpen] = React.useState<boolean>(false);
115+
116+
const questionDifficulty = Object.values(QuestionDifficulty).map((q1) => {
117+
return {
118+
label: capitalizeWords(q1),
119+
value: q1,
120+
};
121+
});
122+
123+
const toggleDropdown = () => {
124+
setIsFilterOpen(!isFilterOpen);
125+
};
108126

109127
const columns: ColumnDef<QuestionMinified>[] = [
110128
{
@@ -184,12 +202,21 @@ export function LeetcodeDashboardTable({
184202
useEffect(() => {
185203
getLeetcodeDashboardData(
186204
pagination.pageIndex + 1,
187-
pagination.pageSize
205+
pagination.pageSize,
206+
searchTitle,
207+
searchDifficulty
188208
).then((data) => {
189-
setData(data.questions);
190209
setTotalPage(data.totalPages);
210+
if (data.totalPages < pagination.pageIndex + 1) {
211+
setPagination((prev) => ({
212+
...prev,
213+
pageIndex: data.totalPages - 1,
214+
}));
215+
} else {
216+
setData(data.questions);
217+
}
191218
});
192-
}, [refreshKey, pagination.pageIndex]);
219+
}, [refreshKey, pagination.pageIndex, searchTitle, searchDifficulty]);
193220

194221
const table = useReactTable({
195222
data,
@@ -211,7 +238,44 @@ export function LeetcodeDashboardTable({
211238
<TableHeader className="w-full">
212239
<TableRow className="text-white bg-primary-900 font-medium hover:bg-transparent h-[5rem] text-md">
213240
<TableCell colSpan={5} className="pl-10">
214-
Past Collaborations
241+
<div className="flex items-center">
242+
<span>LeetCode Question Bank</span>
243+
<span className="ml-auto flex place-items-center gap-6 pr-4">
244+
<div className="relative">
245+
<Search className="absolute left-3 top-1/2 transform -translate-y-1/2 text-primary-400 w-4" />
246+
<Input
247+
className="w-[16rem] pl-10 !placeholder-primary-400"
248+
placeholder="Search Question Name"
249+
onChange={(e) => setSearchTitle(e.target.value)}
250+
/>
251+
</div>
252+
<div className="relative">
253+
<Button
254+
className="gap-2 bg-transparent text-primary-400 border-[1px] hover:bg-primary-300"
255+
onClick={toggleDropdown}
256+
>
257+
<ListFilter />
258+
Filter By
259+
</Button>
260+
{isFilterOpen && (
261+
<div className="absolute right-0 mt-2 h-80 w-52 bg-primary-800 text-primary-300 border border-gray-300 rounded shadow-lg z-10">
262+
<div className="flex flex-col place-items-center mt-4">
263+
<div className="w-[90%]">
264+
<div className="text-xs">Difficulty</div>
265+
<MultiSelect
266+
options={questionDifficulty}
267+
onValueChange={setSearchDifficulty}
268+
placeholder="Select options"
269+
variant="inverted"
270+
className="mt-1"
271+
/>
272+
</div>
273+
</div>
274+
</div>
275+
)}
276+
</div>
277+
</span>
278+
</div>
215279
</TableCell>
216280
</TableRow>
217281
{table.getHeaderGroups().map((headerGroup) => (

0 commit comments

Comments
 (0)