Skip to content

Commit ed68ebf

Browse files
committed
Add refresh on edit/add question
1 parent 8009d40 commit ed68ebf

File tree

7 files changed

+41
-12
lines changed

7 files changed

+41
-12
lines changed

backend/question/src/routes/questionRoutes.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ router.post(
5151

5252
// Retrieve all questions
5353
router.post("/all", async (req: Request, res: Response) => {
54-
const pagination = parseInt(req.body.pagination as string, 10) || 1; // Default page is 1
54+
let pagination = parseInt(req.body.pagination as string, 10) || 1; // Default page is 1
5555
const page_size = parseInt(req.body.page_size as string, 10) || 10; // Default limit is 10
5656
const skip = (pagination - 1) * page_size; // Calculate how many documents to skip
5757
try {
@@ -72,11 +72,13 @@ router.post("/all", async (req: Request, res: Response) => {
7272
.exec();
7373

7474
const total = await Question.countDocuments().exec();
75+
const totalPages = Math.ceil(total / page_size);
76+
if (totalPages < pagination) pagination = 1;
7577

7678
return res.json({
7779
questions,
7880
currentPage: pagination,
79-
totalPages: Math.ceil(total / page_size),
81+
totalPages: totalPages,
8082
totalQuestions: total,
8183
});
8284
} catch (error) {

backend/question/src/tests/app.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,8 +247,8 @@ describe("Test Question API", () => {
247247
// Test /api/all
248248
describe("Test Get All", () => {
249249
// Get all with questions
250-
test("GET /api/all - should retrieve all questions", async () => {
251-
const res = await request.get("/api/all").send();
250+
test("POST /api/all - should retrieve all questions", async () => {
251+
const res = await request.post("/api/all").send();
252252
const sampleQuestion = res.body.questions[0];
253253
expect(res.statusCode).toBe(200);
254254
expect(Array.isArray(res.body.questions)).toBe(true);

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,13 @@ import { topicsList } from "@/utils/constants";
2424

2525
interface AddQuestionDialogProps {
2626
handleClose: () => void;
27+
setRefreshKey: React.Dispatch<React.SetStateAction<number>>;
2728
}
2829

29-
const AddQuestionDialog = ({ handleClose }: AddQuestionDialogProps) => {
30+
const AddQuestionDialog = ({
31+
handleClose,
32+
setRefreshKey,
33+
}: AddQuestionDialogProps) => {
3034
const [isSubmitting, setIsSubmitting] = useState(false);
3135

3236
const formSchema = z.object({
@@ -78,6 +82,7 @@ const AddQuestionDialog = ({ handleClose }: AddQuestionDialogProps) => {
7882
});
7983
})
8084
.finally(() => {
85+
setRefreshKey((prev) => prev + 1);
8186
setIsSubmitting(false);
8287
handleClose();
8388
});

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import { topicsList } from "@/utils/constants";
2929
interface EditQuestionDialogProp {
3030
handleClose: () => void;
3131
questionId: string;
32+
setRefreshKey: React.Dispatch<React.SetStateAction<number>>;
3233
}
3334

3435
interface EditQuestionValues {
@@ -48,6 +49,7 @@ const initialValues: EditQuestionValues = {
4849
const EditQuestionDialog = ({
4950
questionId,
5051
handleClose,
52+
setRefreshKey,
5153
}: EditQuestionDialogProp) => {
5254
const [isSubmitting, setIsSubmitting] = useState(false);
5355
const [leetcodeData, setLeetcodeData] =
@@ -111,6 +113,7 @@ const EditQuestionDialog = ({
111113
});
112114
})
113115
.finally(() => {
116+
setRefreshKey((prev) => prev + 1);
114117
handleClose();
115118
setIsSubmitting(false);
116119
});

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ const LeetcodeDashboardHeader = () => {
2626

2727
const LeetcodeDashboard = () => {
2828
const [modalIsOpen, setIsOpen] = useState(false);
29+
const [refreshKey, setRefreshKey] = useState(0); // State to trigger re-fetch
2930

3031
function openModal() {
3132
setIsOpen(true);
@@ -72,11 +73,17 @@ const LeetcodeDashboard = () => {
7273
variants={modalAnimation}
7374
transition={{ duration: 0.3 }}
7475
>
75-
<AddQuestionDialog handleClose={closeModal} />
76+
<AddQuestionDialog
77+
handleClose={closeModal}
78+
setRefreshKey={setRefreshKey}
79+
/>
7680
</motion.div>
7781
</Modal>
7882
</div>
79-
<LeetcodeDashboardTable />
83+
<LeetcodeDashboardTable
84+
refreshKey={refreshKey}
85+
setRefreshKey={setRefreshKey}
86+
/>
8087
</Container>
8188
);
8289
};

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,19 @@ const Cell = ({
4444
return <div className={cn("text-center", className)}>{children}</div>;
4545
};
4646

47-
export function LeetcodeDashboardTable() {
47+
interface LeetcodeDashboardTableProps {
48+
refreshKey: number;
49+
setRefreshKey: React.Dispatch<React.SetStateAction<number>>;
50+
}
51+
52+
export function LeetcodeDashboardTable({
53+
refreshKey,
54+
setRefreshKey,
55+
}: LeetcodeDashboardTableProps) {
4856
const [data, setData] = useState<QuestionMinified[]>([]);
4957
const [editingQuestionId, setEditingQuestionId] = React.useState<
5058
string | null
5159
>(null);
52-
const [refreshKey, setRefreshKey] = useState(0); // State to trigger re-fetch
5360

5461
function handleDelete(questionId: string) {
5562
Swal.fire({
@@ -64,6 +71,10 @@ export function LeetcodeDashboardTable() {
6471
await deleteSingleLeetcodeQuestion(questionId);
6572
Swal.fire("Question deleted successfully!", "", "success");
6673

74+
setPagination({
75+
pageIndex: 0,
76+
pageSize: 10,
77+
});
6778
// Trigger data refresh
6879
setRefreshKey((prev) => prev + 1);
6980
} catch (error) {
@@ -157,6 +168,7 @@ export function LeetcodeDashboardTable() {
157168
<EditQuestionDialog
158169
questionId={questionId}
159170
handleClose={closeModal}
171+
setRefreshKey={setRefreshKey}
160172
/>
161173
</motion.div>
162174
</Modal>

frontend/src/types/find-match.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
// eslint-disable no-invalid-page-export
22

33
export enum QuestionDifficulty {
4-
EASY = "easy",
5-
MEDIUM = "medium",
6-
HARD = "hard",
4+
EASY = "Easy",
5+
MEDIUM = "Medium",
6+
HARD = "Hard",
77
}
88

99
export enum QuestionLanguages {

0 commit comments

Comments
 (0)