Skip to content

Commit 259e462

Browse files
committed
feat: clickable row entries
1 parent 3d66e85 commit 259e462

File tree

3 files changed

+102
-100
lines changed

3 files changed

+102
-100
lines changed

apps/frontend/src/app/collaboration/[id]/page.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import {
2525
import CollaborativeEditor, {
2626
CollaborativeEditorHandle,
2727
} from "@/components/CollaborativeEditor/CollaborativeEditor";
28-
import { CreateOrUpdateHistory } from "@/app/services/history";
28+
import { CreateHistory } from "@/app/services/history";
2929
import { WebrtcProvider } from "y-webrtc";
3030
import { QuestionDetailFull } from "@/components/question/QuestionDetailFull/QuestionDetailFull";
3131

@@ -145,7 +145,7 @@ export default function CollaborationPage(props: CollaborationProps) {
145145
if (!collaborationId) {
146146
throw new Error("Collaboration ID not found");
147147
}
148-
const data = await CreateOrUpdateHistory(
148+
const data = await CreateHistory(
149149
{
150150
title: questionTitle ?? "",
151151
code: code,

apps/frontend/src/app/question/[id]/page.tsx

Lines changed: 92 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,15 @@
11
"use client";
22
import Header from "@/components/Header/header";
3-
import {
4-
Button,
5-
Col,
6-
Layout,
7-
message,
8-
Row,
9-
Tag,
10-
Select,
11-
Table,
12-
Input,
13-
} from "antd";
3+
import { Col, Layout, message, Row, Table } from "antd";
144
import { Content } from "antd/es/layout/layout";
15-
import {
16-
LeftOutlined,
17-
RightOutlined,
18-
CaretRightOutlined,
19-
CodeOutlined,
20-
SendOutlined,
21-
HistoryOutlined,
22-
} from "@ant-design/icons";
5+
import { CodeOutlined, HistoryOutlined } from "@ant-design/icons";
236
import "./styles.scss";
247
import { useEffect, useRef, useState } from "react";
258
import { GetSingleQuestion } from "../../services/question";
269
import React from "react";
27-
import TextArea from "antd/es/input/TextArea";
2810
import { useSearchParams } from "next/navigation";
29-
import { ProgrammingLanguageOptions } from "@/utils/SelectOptions";
3011
import { useRouter } from "next/navigation";
3112
import { QuestionDetailFull } from "@/components/question/QuestionDetailFull/QuestionDetailFull";
32-
import CollaborativeEditor, {
33-
CollaborativeEditorHandle,
34-
} from "@/components/CollaborativeEditor/CollaborativeEditor";
35-
import { WebrtcProvider } from "y-webrtc";
3613
import { Compartment, EditorState } from "@codemirror/state";
3714
import { basicSetup, EditorView } from "codemirror";
3815
import { javascript } from "@codemirror/lang-javascript";
@@ -44,6 +21,7 @@ interface Submission {
4421
language: string;
4522
matchedUser: string;
4623
code: string;
24+
historyDocRefId: string;
4725
}
4826

4927
export default function QuestionPage() {
@@ -73,10 +51,14 @@ export default function QuestionPage() {
7351
const [complexity, setComplexity] = useState<string | undefined>(undefined);
7452
const [categories, setCategories] = useState<string[]>([]); // Store the selected filter categories
7553
const [description, setDescription] = useState<string | undefined>(undefined);
76-
const [username, setUsername] = useState<string>("");
54+
const [username, setUsername] = useState<string | undefined>(undefined);
7755
const [userQuestionHistories, setUserQuestionHistories] =
7856
useState<History[]>();
7957
const [submission, setSubmission] = useState<Submission>();
58+
const [isHistoryLoading, setIsHistoryLoading] = useState<boolean>(true);
59+
const [currentSubmissionId, setCurrentSubmissionId] = useState<
60+
string | undefined
61+
>(undefined);
8062

8163
const state = EditorState.create({
8264
doc: "",
@@ -107,29 +89,46 @@ export default function QuestionPage() {
10789
});
10890
}, [docRefId]);
10991

92+
useEffect(() => {
93+
if (username === undefined) return;
94+
GetUserQuestionHistories(username, docRefId)
95+
.then((data: any) => {
96+
console.log(data);
97+
setUserQuestionHistories(data);
98+
})
99+
.finally(() => {
100+
setIsHistoryLoading(false);
101+
});
102+
}, [username]);
103+
110104
useEffect(() => {
111105
ValidateUser().then((data: VerifyTokenResponseType) => {
112106
setUsername(data.data.username);
113107
});
114108
}, []);
115109

116110
useEffect(() => {
111+
if (currentSubmissionId === undefined) return;
112+
117113
const view = new EditorView({
118114
state,
119115
parent: editorRef.current || undefined,
120116
});
121117

122118
// TODO: get from a specific history which was selected.
123-
GetHistory("182d0ae6db66fdbefb657f09df3a44a8").then((data: any) => {
119+
// Show latest history by default, or load specific history
120+
GetHistory(currentSubmissionId).then((data: any) => {
121+
const submittedAt = new Date(data.createdAt);
124122
setSubmission({
125-
submittedAt: data.createdAt,
123+
submittedAt: submittedAt.toLocaleString("en-US"),
126124
language: data.language,
127125
matchedUser: data.matchedUser,
128126
code: data.code,
127+
historyDocRefId: data.historyDocRefId,
129128
});
130129

131130
view.dispatch(
132-
view.state.update({
131+
state.update({
133132
changes: { from: 0, to: state.doc.length, insert: data.code },
134133
})
135134
);
@@ -139,30 +138,21 @@ export default function QuestionPage() {
139138
// Cleanup on component unmount
140139
view.destroy();
141140
};
142-
}, []);
143-
144-
useEffect(() => {
145-
GetUserQuestionHistories(username, docRefId).then((data: any) => {
146-
setUserQuestionHistories(data);
147-
});
148-
}, [docRefId, username]);
149-
150-
useEffect(() => {
151-
GetUserQuestionHistories(username, docRefId).then((data: any) => {
152-
setUserQuestionHistories(data);
153-
});
154-
}, [docRefId, username]);
141+
}, [currentSubmissionId]);
155142

156143
const columns = [
157144
{
158-
title: "Id",
145+
title: "ID",
159146
dataIndex: "id",
160147
key: "id",
161148
},
162149
{
163150
title: "Submitted at",
164151
dataIndex: "createdAt",
165152
key: "createdAt",
153+
render: (date: string) => {
154+
return new Date(date).toLocaleString();
155+
},
166156
},
167157
{
168158
title: "Language",
@@ -176,6 +166,10 @@ export default function QuestionPage() {
176166
},
177167
];
178168

169+
const handleRowClick = (s: Submission) => {
170+
setCurrentSubmissionId(s.historyDocRefId);
171+
};
172+
179173
return (
180174
<div>
181175
{contextHolder}
@@ -206,56 +200,67 @@ export default function QuestionPage() {
206200
rowKey="id"
207201
dataSource={userQuestionHistories}
208202
columns={columns}
209-
loading={isLoading}
203+
onRow={(record: any) => {
204+
return {
205+
onClick: () => handleRowClick(record),
206+
style: { cursor: "pointer" },
207+
};
208+
}}
209+
loading={isHistoryLoading}
210210
/>
211211
</div>
212212
</div>
213213
</Row>
214-
<Row className="code-row">
215-
<div className="code-container">
216-
<div className="code-top-container">
217-
<div className="code-title">
218-
<CodeOutlined className="title-icons" />
219-
Submitted Code
220-
</div>
221-
</div>
222-
<div
223-
style={{
224-
margin: "10px",
225-
display: "flex",
226-
flexDirection: "row",
227-
}}
228-
>
229-
<div className="submission-header-detail">
230-
Submitted at: {submission?.submittedAt || "-"}
231-
</div>
232-
<div className="submission-header-detail">
233-
Language: {submission?.language || "-"}
234-
</div>
235-
<div className="submission-header-detail">
236-
Matched with: {submission?.matchedUser || "-"}
237-
</div>
238-
</div>
214+
{currentSubmissionId && (
215+
<Row className="code-row">
216+
<div className="code-container">
217+
<>
218+
<div className="code-top-container">
219+
<div className="code-title">
220+
<CodeOutlined className="title-icons" />
221+
Submitted Code
222+
</div>
223+
</div>
239224

240-
{/* TODO: add details of attempt here */}
241-
{/* TODO: set value of code, refactor to look like collab editor but not editable */}
242-
<div
243-
style={{
244-
margin: "10px",
245-
height: "40vh",
246-
}}
247-
>
248-
<div
249-
ref={editorRef}
250-
style={{
251-
height: "100%",
252-
overflow: "scroll",
253-
border: "1px solid #ddd",
254-
}}
255-
></div>
225+
{/* Details of submission */}
226+
<div
227+
style={{
228+
margin: "10px",
229+
display: "flex",
230+
flexDirection: "row",
231+
}}
232+
>
233+
<div className="submission-header-detail">
234+
Submitted at: {submission?.submittedAt || "-"}
235+
</div>
236+
<div className="submission-header-detail">
237+
Language: {submission?.language || "-"}
238+
</div>
239+
<div className="submission-header-detail">
240+
Matched with: {submission?.matchedUser || "-"}
241+
</div>
242+
</div>
243+
244+
{/* Code Editor */}
245+
<div
246+
style={{
247+
margin: "10px",
248+
height: "40vh",
249+
}}
250+
>
251+
<div
252+
ref={editorRef}
253+
style={{
254+
height: "100%",
255+
overflow: "scroll",
256+
border: "1px solid #ddd",
257+
}}
258+
></div>
259+
</div>
260+
</>
256261
</div>
257-
</div>
258-
</Row>
262+
</Row>
263+
)}
259264
</Col>
260265
</Row>
261266
</Content>

apps/frontend/src/app/services/history.ts

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,17 @@ export interface History {
1515
updatedAt?: string;
1616
}
1717

18-
export const CreateOrUpdateHistory = async (
18+
export const CreateHistory = async (
1919
history: History,
2020
historyDocRefId: string
2121
): Promise<History> => {
22-
const response = await fetch(
23-
`${HISTORY_SERVICE_URL}histories/${historyDocRefId}`,
24-
{
25-
method: "PUT",
26-
headers: {
27-
"Content-Type": "application/json",
28-
},
29-
body: JSON.stringify(history),
30-
}
31-
);
22+
const response = await fetch(`${HISTORY_SERVICE_URL}histories/`, {
23+
method: "POST",
24+
headers: {
25+
"Content-Type": "application/json",
26+
},
27+
body: JSON.stringify(history),
28+
});
3229

3330
if (response.status === 200) {
3431
return response.json();

0 commit comments

Comments
 (0)