@@ -36,6 +36,15 @@ import { WebrtcProvider } from "y-webrtc";
36
36
import { Compartment , EditorState } from "@codemirror/state" ;
37
37
import { basicSetup , EditorView } from "codemirror" ;
38
38
import { javascript } from "@codemirror/lang-javascript" ;
39
+ import { GetHistory , GetUserQuestionHistories } from "@/app/services/history" ;
40
+ import { ValidateUser , VerifyTokenResponseType } from "@/app/services/user" ;
41
+
42
+ interface Submission {
43
+ submittedAt : string ;
44
+ language : string ;
45
+ matchedUser : string ;
46
+ code : string ;
47
+ }
39
48
40
49
export default function QuestionPage ( ) {
41
50
const [ isLoading , setIsLoading ] = useState < boolean > ( true ) ; // Store the states related to table's loading
@@ -52,7 +61,6 @@ export default function QuestionPage() {
52
61
53
62
const router = useRouter ( ) ;
54
63
const editorRef = useRef ( null ) ;
55
- const providerRef = useRef < WebrtcProvider | null > ( null ) ;
56
64
const languageConf = new Compartment ( ) ;
57
65
58
66
// Retrieve the docRefId from query params during page navigation
@@ -65,9 +73,13 @@ export default function QuestionPage() {
65
73
const [ complexity , setComplexity ] = useState < string | undefined > ( undefined ) ;
66
74
const [ categories , setCategories ] = useState < string [ ] > ( [ ] ) ; // Store the selected filter categories
67
75
const [ description , setDescription ] = useState < string | undefined > ( undefined ) ;
76
+ const [ username , setUsername ] = useState < string > ( "" ) ;
77
+ const [ userQuestionHistories , setUserQuestionHistories ] =
78
+ useState < History [ ] > ( ) ;
79
+ const [ submission , setSubmission ] = useState < Submission > ( ) ;
68
80
69
81
const state = EditorState . create ( {
70
- doc : "TODO: parse from code " ,
82
+ doc : "" ,
71
83
extensions : [
72
84
basicSetup ,
73
85
languageConf . of ( javascript ( ) ) ,
@@ -93,20 +105,53 @@ export default function QuestionPage() {
93
105
. finally ( ( ) => {
94
106
setIsLoading ( false ) ;
95
107
} ) ;
108
+ } , [ docRefId ] ) ;
96
109
110
+ useEffect ( ( ) => {
111
+ ValidateUser ( ) . then ( ( data : VerifyTokenResponseType ) => {
112
+ setUsername ( data . data . username ) ;
113
+ } ) ;
114
+ } , [ ] ) ;
115
+
116
+ useEffect ( ( ) => {
97
117
const view = new EditorView ( {
98
118
state,
99
119
parent : editorRef . current || undefined ,
100
120
} ) ;
101
121
122
+ // TODO: get from a specific history which was selected.
123
+ GetHistory ( "182d0ae6db66fdbefb657f09df3a44a8" ) . then ( ( data : any ) => {
124
+ setSubmission ( {
125
+ submittedAt : data . createdAt ,
126
+ language : data . language ,
127
+ matchedUser : data . matchedUser ,
128
+ code : data . code ,
129
+ } ) ;
130
+
131
+ view . dispatch (
132
+ view . state . update ( {
133
+ changes : { from : 0 , to : state . doc . length , insert : data . code } ,
134
+ } )
135
+ ) ;
136
+ } ) ;
137
+
102
138
return ( ) => {
103
139
// Cleanup on component unmount
104
140
view . destroy ( ) ;
105
141
} ;
106
- } , [ docRefId ] ) ;
142
+ } , [ ] ) ;
107
143
108
- // TODO: retrieve history
109
- const history : any [ ] = [ ] ;
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 ] ) ;
110
155
111
156
const columns = [
112
157
{
@@ -115,9 +160,9 @@ export default function QuestionPage() {
115
160
key : "id" ,
116
161
} ,
117
162
{
118
- title : "Attempted at" ,
119
- dataIndex : "attemptedAt " ,
120
- key : "attemptedAt " ,
163
+ title : "Submitted at" ,
164
+ dataIndex : "createdAt " ,
165
+ key : "createdAt " ,
121
166
} ,
122
167
{
123
168
title : "Language" ,
@@ -159,7 +204,7 @@ export default function QuestionPage() {
159
204
< div style = { { margin : "10px" } } >
160
205
< Table
161
206
rowKey = "id"
162
- dataSource = { history }
207
+ dataSource = { userQuestionHistories }
163
208
columns = { columns }
164
209
loading = { isLoading }
165
210
/>
@@ -174,6 +219,23 @@ export default function QuestionPage() {
174
219
Submitted Code
175
220
</ div >
176
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 >
177
239
178
240
{ /* TODO: add details of attempt here */ }
179
241
{ /* TODO: set value of code, refactor to look like collab editor but not editable */ }
@@ -190,7 +252,7 @@ export default function QuestionPage() {
190
252
overflow : "scroll" ,
191
253
border : "1px solid #ddd" ,
192
254
} }
193
- / >
255
+ > </ div >
194
256
</ div >
195
257
</ div >
196
258
</ Row >
0 commit comments