Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions frontend/api/lectures/fetchQuestions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { axiosInstance } from "@/api/axiosInstance";
import axios from "axios";
import { ENDPOINTS } from "@/constants/endpoints";
import { ApiResponse } from "@/types/apiResponseTypes";
import { FetchQuestionsResult } from "@/types/lectures/fetchQuestionsTypes";

export async function fetchQuestions(lectureId: string) {
try {
const response = await axiosInstance.get<
ApiResponse<FetchQuestionsResult>
>(ENDPOINTS.LECTURES.GET_CHAT(lectureId));
return response.data;
} catch (error: unknown) {
if (axios.isAxiosError(error) && error.response) {
return error.response.data as ApiResponse<FetchQuestionsResult>;
}
throw error;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: $spacing-xs;
}

.userInfo {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { useEffect, useState } from "react";
import Image from "next/image";
import { IMAGES } from "@/constants/images";
import { useLectureDetail } from "../LectureDetailContext";
import { FetchQuestionsResult } from "@/types/lectures/fetchQuestionsTypes";
import { fetchQuestions } from "@/api/lectures/fetchQuestions";

export interface Question {
sender: string;
Expand All @@ -19,11 +21,41 @@ export default function QuestionList() {
const [loading, setLoading] = useState(true);

useEffect(() => {
// TODO: API 호출로 변경
setQuestions([]);
setLoading(false);
let alive = true;

const load = async () => {
setLoading(true);

try {
const res = await fetchQuestions(lectureId);
if (!alive) return;

const list = (res.result ?? []) as FetchQuestionsResult[];

const mapped: Question[] = list.map((q) => ({
sender: q.studentName,
message: q.content,
timestamp: q.timestamp,
profileUrl: q.studentProfile ?? "",
}))
.sort((a, b) => new Date(b.timestamp).getTime() - new Date(a.timestamp).getTime());

setQuestions(mapped);
} catch {
if (!alive) return;
setQuestions([]);
} finally {
if (alive) setLoading(false);
}
};

load();
return () => {
alive = false;
};
}, [lectureId]);


const formatTimestamp = (timestamp: string) => {
try {
const date = new Date(timestamp);
Expand Down
4 changes: 2 additions & 2 deletions frontend/constants/endpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,12 @@ export const ENDPOINTS = {
`${BASE_API}/lectures/${lectureId}/recordings`,
GET_RECORDING: (lectureId: string) =>
`${BASE_API}/lectures/${lectureId}/recordings`,

// 채팅 관련
SAVE_CHAT: (lectureId: string) =>
`${BASE_API}/lectures/chatting/after/${lectureId}`,
GET_CHAT: (lectureId: string) =>
`${BASE_API}/lectures/${lectureId}/chatting`,
`${BASE_API}/lectures/${lectureId}/questions`,
},

// 퀴즈 관련
Expand Down
7 changes: 7 additions & 0 deletions frontend/types/lectures/fetchQuestionsTypes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export interface FetchQuestionsResult {
studentId: string;
studentName: string;
studentProfile: string | null;
timestamp: string;
content: string;
}