1
1
"use client" ;
2
2
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" ;
14
4
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" ;
23
6
import "./styles.scss" ;
24
7
import { useEffect , useRef , useState } from "react" ;
25
8
import { GetSingleQuestion } from "../../services/question" ;
26
9
import React from "react" ;
27
- import TextArea from "antd/es/input/TextArea" ;
28
10
import { useSearchParams } from "next/navigation" ;
29
- import { ProgrammingLanguageOptions } from "@/utils/SelectOptions" ;
30
11
import { useRouter } from "next/navigation" ;
31
12
import { QuestionDetailFull } from "@/components/question/QuestionDetailFull/QuestionDetailFull" ;
32
- import CollaborativeEditor , {
33
- CollaborativeEditorHandle ,
34
- } from "@/components/CollaborativeEditor/CollaborativeEditor" ;
35
- import { WebrtcProvider } from "y-webrtc" ;
36
13
import { Compartment , EditorState } from "@codemirror/state" ;
37
14
import { basicSetup , EditorView } from "codemirror" ;
38
15
import { javascript } from "@codemirror/lang-javascript" ;
@@ -44,6 +21,7 @@ interface Submission {
44
21
language : string ;
45
22
matchedUser : string ;
46
23
code : string ;
24
+ historyDocRefId : string ;
47
25
}
48
26
49
27
export default function QuestionPage ( ) {
@@ -73,10 +51,14 @@ export default function QuestionPage() {
73
51
const [ complexity , setComplexity ] = useState < string | undefined > ( undefined ) ;
74
52
const [ categories , setCategories ] = useState < string [ ] > ( [ ] ) ; // Store the selected filter categories
75
53
const [ description , setDescription ] = useState < string | undefined > ( undefined ) ;
76
- const [ username , setUsername ] = useState < string > ( "" ) ;
54
+ const [ username , setUsername ] = useState < string | undefined > ( undefined ) ;
77
55
const [ userQuestionHistories , setUserQuestionHistories ] =
78
56
useState < History [ ] > ( ) ;
79
57
const [ submission , setSubmission ] = useState < Submission > ( ) ;
58
+ const [ isHistoryLoading , setIsHistoryLoading ] = useState < boolean > ( true ) ;
59
+ const [ currentSubmissionId , setCurrentSubmissionId ] = useState <
60
+ string | undefined
61
+ > ( undefined ) ;
80
62
81
63
const state = EditorState . create ( {
82
64
doc : "" ,
@@ -107,29 +89,46 @@ export default function QuestionPage() {
107
89
} ) ;
108
90
} , [ docRefId ] ) ;
109
91
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
+
110
104
useEffect ( ( ) => {
111
105
ValidateUser ( ) . then ( ( data : VerifyTokenResponseType ) => {
112
106
setUsername ( data . data . username ) ;
113
107
} ) ;
114
108
} , [ ] ) ;
115
109
116
110
useEffect ( ( ) => {
111
+ if ( currentSubmissionId === undefined ) return ;
112
+
117
113
const view = new EditorView ( {
118
114
state,
119
115
parent : editorRef . current || undefined ,
120
116
} ) ;
121
117
122
118
// 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 ) ;
124
122
setSubmission ( {
125
- submittedAt : data . createdAt ,
123
+ submittedAt : submittedAt . toLocaleString ( "en-US" ) ,
126
124
language : data . language ,
127
125
matchedUser : data . matchedUser ,
128
126
code : data . code ,
127
+ historyDocRefId : data . historyDocRefId ,
129
128
} ) ;
130
129
131
130
view . dispatch (
132
- view . state . update ( {
131
+ state . update ( {
133
132
changes : { from : 0 , to : state . doc . length , insert : data . code } ,
134
133
} )
135
134
) ;
@@ -139,30 +138,21 @@ export default function QuestionPage() {
139
138
// Cleanup on component unmount
140
139
view . destroy ( ) ;
141
140
} ;
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 ] ) ;
155
142
156
143
const columns = [
157
144
{
158
- title : "Id " ,
145
+ title : "ID " ,
159
146
dataIndex : "id" ,
160
147
key : "id" ,
161
148
} ,
162
149
{
163
150
title : "Submitted at" ,
164
151
dataIndex : "createdAt" ,
165
152
key : "createdAt" ,
153
+ render : ( date : string ) => {
154
+ return new Date ( date ) . toLocaleString ( ) ;
155
+ } ,
166
156
} ,
167
157
{
168
158
title : "Language" ,
@@ -176,6 +166,10 @@ export default function QuestionPage() {
176
166
} ,
177
167
] ;
178
168
169
+ const handleRowClick = ( s : Submission ) => {
170
+ setCurrentSubmissionId ( s . historyDocRefId ) ;
171
+ } ;
172
+
179
173
return (
180
174
< div >
181
175
{ contextHolder }
@@ -206,56 +200,67 @@ export default function QuestionPage() {
206
200
rowKey = "id"
207
201
dataSource = { userQuestionHistories }
208
202
columns = { columns }
209
- loading = { isLoading }
203
+ onRow = { ( record : any ) => {
204
+ return {
205
+ onClick : ( ) => handleRowClick ( record ) ,
206
+ style : { cursor : "pointer" } ,
207
+ } ;
208
+ } }
209
+ loading = { isHistoryLoading }
210
210
/>
211
211
</ div >
212
212
</ div >
213
213
</ 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 >
239
224
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
+ </ >
256
261
</ div >
257
- </ div >
258
- </ Row >
262
+ </ Row >
263
+ ) }
259
264
</ Col >
260
265
</ Row >
261
266
</ Content >
0 commit comments