Skip to content

Commit 4fc34f2

Browse files
committed
Use structs.ts in QuestionCard and QuestionPage
1 parent 5e3ac1d commit 4fc34f2

File tree

6 files changed

+35
-42
lines changed

6 files changed

+35
-42
lines changed

peerprep/api/structs.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
export enum Difficulty {
2+
All = 0,
23
Easy = 1,
34
Medium,
4-
Hard
5+
Hard,
56
}
67

78
export interface QuestionBody {
89
difficulty: Difficulty;
910
title: string;
1011
description: string;
12+
categories: string[];
1113
}
1214

1315
export interface TestCase {

peerprep/components/questionpage/QuestionCard.tsx

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
import React from "react";
2-
import { Question, difficulties } from "../shared/Question";
2+
import { Question, StatusBody, Difficulty } from "@/api/structs";
33
import PeerprepButton from "../shared/PeerprepButton";
4+
import { redirect } from "next/navigation";
45

56
type QuestionCardProps = {
67
question: Question;
78
};
89

910
const QuestionCard: React.FC<QuestionCardProps> = ({ question }) => {
10-
const getDifficultyClass = (difficulty: string) => {
11+
const getDifficultyClass = (difficulty: Difficulty) => {
1112
switch (difficulty) {
12-
case "easy":
13+
case Difficulty.Easy:
1314
return "text-green-500"; // Green for easy
14-
case "medium":
15+
case Difficulty.Medium:
1516
return "text-yellow-500"; // Yellow for medium
16-
case "hard":
17+
case Difficulty.Hard:
1718
return "text-red-500"; // Red for hard
1819
default:
1920
return "text-gray-100"; // Default color
@@ -27,15 +28,16 @@ const QuestionCard: React.FC<QuestionCardProps> = ({ question }) => {
2728
<p className="text-sm">
2829
Difficulty:{" "}
2930
<span
30-
className={`capitalize ${getDifficultyClass(
31-
difficulties[question.difficulty]
32-
)}`}
31+
className={`capitalize ${getDifficultyClass(question.difficulty)}`}
3332
>
34-
{difficulties[question.difficulty]}
33+
{Difficulty[question.difficulty]}
3534
</span>
3635
</p>
3736
<p className="text-sm">
38-
Categories: <span>{question.categories.join(", ")}</span>
37+
Categories:{" "}
38+
<span>
39+
{question.categories ? question.categories.join(", ") : "None"}
40+
</span>
3941
</p>
4042
</div>
4143

peerprep/components/questionpage/QuestionList.tsx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
"use client";
22
import React, { useEffect, useState } from "react";
33
import QuestionCard from "./QuestionCard";
4-
import { Question, difficulties } from "../shared/Question";
4+
import { Question, StatusBody, Difficulty } from "@/api/structs";
55
import PeerprepDropdown from "../shared/PeerprepDropdown";
66
import PeerprepSearchBar from "../shared/PeerprepSearchBar";
77

88
const QuestionList: React.FC = () => {
99
const [questions, setQuestions] = useState<Question[]>([]);
1010
const [loading, setLoading] = useState(true);
1111
const [difficultyFilter, setDifficultyFilter] = useState<string>(
12-
difficulties[0]
12+
Difficulty[0]
1313
);
1414
const [categoryFilter, setCategoryFilter] = useState<string>("all");
1515
// will prolly have to search by name later
@@ -44,14 +44,14 @@ const QuestionList: React.FC = () => {
4444

4545
const filteredQuestions = questions.filter((question) => {
4646
const matchesDifficulty =
47-
difficultyFilter === difficulties[0] ||
48-
difficulties[question.difficulty] === difficultyFilter;
47+
difficultyFilter === Difficulty[0] ||
48+
Difficulty[question.difficulty] === difficultyFilter;
4949
const matchesCategory =
5050
categoryFilter === categories[0] ||
51-
question.categories.includes(categoryFilter);
51+
(question.categories ?? []).includes(categoryFilter);
5252
const matchesSearch =
5353
searchFilter === "" ||
54-
question.title.toLowerCase().includes(searchFilter.toLowerCase());
54+
(question.title ?? "").toLowerCase().includes(searchFilter.toLowerCase());
5555

5656
return matchesDifficulty && matchesCategory && matchesSearch;
5757
});
@@ -70,7 +70,7 @@ const QuestionList: React.FC = () => {
7070
label="Difficulty"
7171
value={difficultyFilter}
7272
onChange={(e) => setDifficultyFilter(e.target.value)}
73-
options={difficulties}
73+
options={Object.keys(Difficulty).filter((key) => isNaN(Number(key)))}
7474
/>
7575
<PeerprepDropdown
7676
label="Category"

peerprep/components/shared/PeerprepButton.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const PeerprepButton: React.FC<PeerprepButtonProps> = ({
1515
return (
1616
<button
1717
onClick={onClick}
18-
className={`px-4 py-2 text-white bg-gray-600 rounded hover:bg-gray-500 focus:outline-none focus:ring focus:ring-gray-400${className}`}
18+
className={`px-4 py-2 text-white bg-gray-600 rounded hover:bg-gray-500 focus:outline-none focus:ring focus:ring-gray-400 ${className}`}
1919
>
2020
{children}
2121
</button>

peerprep/components/shared/PeerprepDropdown.tsx

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
11
"use client";
22
import React from "react";
3-
4-
type PeerprepDropdownProps = {
3+
type PeerprepDropdownProps<T> = {
54
label: string;
6-
value: string;
5+
value: T;
76
onChange: (e: React.ChangeEvent<HTMLSelectElement>) => void;
8-
options: string[];
7+
options: T[];
98
className?: string;
109
};
1110

12-
const PeerprepDropdown: React.FC<PeerprepDropdownProps> = ({
11+
const PeerprepDropdown = <T extends string | number>({
1312
label,
1413
value,
1514
onChange,
1615
options,
1716
className,
18-
}) => {
17+
}: PeerprepDropdownProps<T>): JSX.Element => {
1918
return (
2019
<div className="flex flex-col space-y-1 space-x-2">
2120
<label className="text-base font-medium">{label}</label>
@@ -24,11 +23,13 @@ const PeerprepDropdown: React.FC<PeerprepDropdownProps> = ({
2423
onChange={onChange}
2524
className={`text-black border rounded p-2 focus:outline-none focus:ring-2 focus:ring-gray-300 ${className}`}
2625
>
27-
{options.map((option) => (
28-
<option className="capitalize" key={option} value={option}>
29-
{option.charAt(0).toUpperCase() + option.slice(1)}
30-
</option>
31-
))}
26+
{options
27+
.filter((option) => option)
28+
.map((option) => (
29+
<option className="capitalize" key={option} value={option}>
30+
{String(option).charAt(0).toUpperCase() + String(option).slice(1)}
31+
</option>
32+
))}
3233
</select>
3334
</div>
3435
);

peerprep/components/shared/Question.tsx

Lines changed: 0 additions & 12 deletions
This file was deleted.

0 commit comments

Comments
 (0)