Skip to content

Commit 026b83b

Browse files
committed
Shift question request back to question display
Fix history by letting the question display handle question request
1 parent b53140f commit 026b83b

File tree

2 files changed

+68
-59
lines changed

2 files changed

+68
-59
lines changed

frontend/components/collab/collab-room.tsx

Lines changed: 8 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,17 @@
11
"use client";
22

3-
import React, { useState, useEffect } from "react";
3+
import React, { useState } from "react";
44
import { Button } from "@/components/ui/button";
55
import { X } from "lucide-react";
66
import Chat from "./chat";
77
import QuestionDisplay from "./question-display";
88
import CodeEditor from "./code-editor";
9-
import LoadingScreen from "@/components/common/loading-screen";
10-
import { getQuestion } from "@/lib/api/question-service/get-question";
11-
import { useAuth } from "@/app/auth/auth-context";
12-
import { getQuestionId } from "@/lib/api/collab-service/get-questionId";
13-
import { Question } from "@/lib/schemas/question-schema";
14-
import { useToast } from "@/components/hooks/use-toast";
159
import Link from "next/link";
10+
import { Question } from "@/lib/schemas/question-schema";
1611

1712
export default function CollabRoom({ roomId }: { roomId: string }) {
18-
const auth = useAuth();
19-
const token = auth?.token;
20-
const { toast } = useToast();
21-
22-
const [question, setQuestion] = useState<Question | null>(null);
23-
const [loading, setLoading] = useState(true);
2413
const [code, setCode] = useState<string>("");
25-
26-
useEffect(() => {
27-
async function fetchQuestion() {
28-
try {
29-
if (!auth || !auth.token) {
30-
toast({
31-
title: "Access denied",
32-
description: "No authentication token found",
33-
variant: "destructive",
34-
});
35-
return;
36-
}
37-
38-
// Call to the collab microservice to get questionId by roomId
39-
const response = await getQuestionId(auth.token, roomId);
40-
const data = await response.json();
41-
42-
if (data.questionId) {
43-
// Fetch the question details using the questionId
44-
if (token) {
45-
const questionResponse = await getQuestion(token, data.questionId);
46-
const questionData = await questionResponse.json();
47-
setQuestion(questionData);
48-
} else {
49-
console.error("Token is not available");
50-
}
51-
}
52-
} catch (error) {
53-
console.error("Error fetching question:", error);
54-
} finally {
55-
setLoading(false);
56-
}
57-
}
58-
59-
fetchQuestion();
60-
}, [roomId]);
61-
14+
const [exposedQuestion, setExposedQuestion] = useState<Question | null>(null);
6215
return (
6316
<div className="h-screen flex flex-col mx-4 p-4 overflow-hidden">
6417
<header className="flex justify-between border-b">
@@ -72,12 +25,11 @@ export default function CollabRoom({ roomId }: { roomId: string }) {
7225
</header>
7326
<div className="flex flex-1 overflow-hidden">
7427
<div className="w-2/5 p-4 flex flex-col space-y-4 overflow-hidden">
75-
{loading ? (
76-
<LoadingScreen />
77-
) : (
78-
<QuestionDisplay question={question} />
79-
)}
80-
<Chat roomId={roomId} question={question} code={code} />
28+
<QuestionDisplay
29+
roomId={roomId}
30+
setExposedQuestion={setExposedQuestion}
31+
/>
32+
<Chat roomId={roomId} question={exposedQuestion} code={code} />
8133
</div>
8234
<CodeEditor roomId={roomId} setCode={setCode} />
8335
</div>

frontend/components/collab/question-display.tsx

Lines changed: 60 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"use client";
2-
import React from "react";
2+
import React, { useState, useEffect } from "react";
33
import clsx from "clsx";
44
import {
55
Card,
@@ -8,8 +8,13 @@ import {
88
CardHeader,
99
CardTitle,
1010
} from "@/components/ui/card";
11+
import { getQuestion } from "@/lib/api/question-service/get-question";
12+
import { useAuth } from "@/app/auth/auth-context";
13+
import { getQuestionId } from "@/lib/api/collab-service/get-questionId";
14+
import { useToast } from "@/components/hooks/use-toast";
1115
import { Badge } from "@/components/ui/badge";
1216
import { Question } from "@/lib/schemas/question-schema";
17+
import LoadingScreen from "@/components/common/loading-screen";
1318

1419
const difficultyColors = {
1520
Easy: "bg-green-500",
@@ -20,12 +25,64 @@ const difficultyColors = {
2025
export default function QuestionDisplay({
2126
className,
2227
date,
23-
question,
28+
roomId,
29+
setExposedQuestion,
2430
}: {
2531
className?: string;
2632
date?: Date;
27-
question: Question | null;
33+
roomId: string;
34+
setExposedQuestion?: (question: Question) => void;
2835
}) {
36+
const auth = useAuth();
37+
const token = auth?.token;
38+
const { toast } = useToast();
39+
40+
const [question, setQuestion] = useState<Question | null>(null);
41+
const [loading, setLoading] = useState(true);
42+
43+
useEffect(() => {
44+
async function fetchQuestion() {
45+
try {
46+
if (!auth || !auth.token) {
47+
toast({
48+
title: "Access denied",
49+
description: "No authentication token found",
50+
variant: "destructive",
51+
});
52+
return;
53+
}
54+
55+
// Call to the collab microservice to get questionId by roomId
56+
const response = await getQuestionId(auth.token, roomId);
57+
const data = await response.json();
58+
59+
if (data.questionId) {
60+
// Fetch the question details using the questionId
61+
if (token) {
62+
const questionResponse = await getQuestion(token, data.questionId);
63+
const questionData = await questionResponse.json();
64+
setQuestion(questionData);
65+
if (setExposedQuestion) {
66+
setExposedQuestion(questionData);
67+
}
68+
} else {
69+
console.error("Token is not available");
70+
}
71+
}
72+
} catch (error) {
73+
console.error("Error fetching question:", error);
74+
} finally {
75+
setLoading(false);
76+
}
77+
}
78+
79+
fetchQuestion();
80+
}, [roomId]);
81+
82+
if (loading) {
83+
return <LoadingScreen />;
84+
}
85+
2986
if (!question) {
3087
return <div>Question not found</div>;
3188
}

0 commit comments

Comments
 (0)