Skip to content

Commit 2736aee

Browse files
committed
Move getQuestion to collab room
Pass question into question display instead. Use Question schema instead of creating a new interface
1 parent e197d36 commit 2736aee

File tree

2 files changed

+49
-50
lines changed

2 files changed

+49
-50
lines changed

frontend/components/collab/collab-room.tsx

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,51 @@
1-
import React from "react";
1+
"use client";
2+
3+
import React, { useState, useEffect } from "react";
24
import { Button } from "@/components/ui/button";
35
import { X } from "lucide-react";
46
import Chat from "./chat";
57
import QuestionDisplay from "./question-display";
68
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";
714

815
export default function CollabRoom({ roomId }: { roomId: string }) {
16+
const auth = useAuth();
17+
const token = auth?.token;
18+
19+
const [question, setQuestion] = useState<Question | null>(null);
20+
const [loading, setLoading] = useState(true);
21+
22+
useEffect(() => {
23+
async function fetchQuestion() {
24+
try {
25+
// Call to the collab microservice to get questionId by roomId
26+
const response = await getQuestionId(roomId);
27+
const data = await response.json();
28+
29+
if (data.questionId) {
30+
// Fetch the question details using the questionId
31+
if (token) {
32+
const questionResponse = await getQuestion(token, data.questionId);
33+
const questionData = await questionResponse.json();
34+
setQuestion(questionData);
35+
} else {
36+
console.error("Token is not available");
37+
}
38+
}
39+
} catch (error) {
40+
console.error("Error fetching question:", error);
41+
} finally {
42+
setLoading(false);
43+
}
44+
}
45+
46+
fetchQuestion();
47+
}, [roomId]);
48+
949
return (
1050
<div className="h-full flex flex-col mx-4 p-4">
1151
<header className="flex justify-between border-b">
@@ -17,7 +57,11 @@ export default function CollabRoom({ roomId }: { roomId: string }) {
1757
</header>
1858
<div className="flex flex-1">
1959
<div className="w-2/5 p-4 flex flex-col space-y-4">
20-
<QuestionDisplay roomId={roomId} />
60+
{loading ? (
61+
<LoadingScreen />
62+
) : (
63+
<QuestionDisplay question={question} />
64+
)}
2165
<Chat roomId={roomId} />
2266
</div>
2367
<CodeEditor roomId={roomId} />

frontend/components/collab/question-display.tsx

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

1613
const difficultyColors = {
1714
Easy: "bg-green-500",
1815
Medium: "bg-yellow-500",
1916
Hard: "bg-red-500",
2017
};
2118

22-
interface Question {
23-
title: string;
24-
categories: string;
25-
complexity: keyof typeof difficultyColors;
26-
description: string;
27-
}
28-
29-
export default function QuestionDisplay({ roomId }: { roomId: string }) {
30-
const auth = useAuth();
31-
const token = auth?.token;
32-
const [question, setQuestion] = useState<Question | null>(null);
33-
const [loading, setLoading] = useState(true);
34-
35-
useEffect(() => {
36-
async function fetchQuestion() {
37-
try {
38-
// Call to the collab microservice to get questionId by roomId
39-
const response = await getQuestionId(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-
62-
if (loading) {
63-
return <LoadingScreen />;
64-
}
19+
export default function QuestionDisplay({ question }: { question: Question | null }) {
6520

6621
if (!question) {
6722
return <div>Question not found</div>;

0 commit comments

Comments
 (0)