6
6
Input ,
7
7
Layout ,
8
8
Modal ,
9
+ message ,
9
10
Row ,
10
11
Select ,
11
12
Tabs ,
@@ -31,24 +32,33 @@ import { ProgrammingLanguageOptions } from "@/utils/SelectOptions";
31
32
import CollaborativeEditor , {
32
33
CollaborativeEditorHandle ,
33
34
} from "@/components/CollaborativeEditor/CollaborativeEditor" ;
35
+ import { CreateOrUpdateHistory } from "@/app/services/history" ;
36
+ import { Language } from "@codemirror/language" ;
37
+ import { WebrtcProvider } from "y-webrtc" ;
34
38
35
39
interface CollaborationProps { }
36
40
37
41
export default function CollaborationPage ( props : CollaborationProps ) {
38
42
const router = useRouter ( ) ;
43
+ // const providerRef = useRef<WebrtcProvider | null>(null);
39
44
40
45
const editorRef = useRef < CollaborativeEditorHandle > ( null ) ;
41
46
42
47
const [ isLoading , setIsLoading ] = useState < boolean > ( false ) ;
43
48
44
49
// Code Editor States
50
+ const [ historyDocRefId , setHistoryDocRefId ] = useState < string | undefined > (
51
+ undefined
52
+ ) ;
53
+ const [ code , setCode ] = useState < string > ( "" ) ;
45
54
const [ questionTitle , setQuestionTitle ] = useState < string | undefined > (
46
55
undefined
47
56
) ;
57
+ const [ questionDocRefId , setQuestionDocRefId ] = useState < string | undefined > ( undefined ) ;
48
58
const [ complexity , setComplexity ] = useState < string | undefined > ( undefined ) ;
49
59
const [ categories , setCategories ] = useState < string [ ] > ( [ ] ) ; // Store the selected filter categories
50
60
const [ description , setDescription ] = useState < string | undefined > ( undefined ) ;
51
- const [ selectedLanguage , setSelectedLanguage ] = useState ( "javascript " ) ; // State to hold the selected language item
61
+ const [ selectedLanguage , setSelectedLanguage ] = useState ( "Javascript " ) ; // State to hold the selected language item
52
62
53
63
// Session states
54
64
const [ collaborationId , setCollaborationId ] = useState < string | undefined > (
@@ -61,6 +71,7 @@ export default function CollaborationPage(props: CollaborationProps) {
61
71
return storedTime ? parseInt ( storedTime ) : 0 ;
62
72
} ) ; // State for count-up timer (TODO: currently using localstorage to store time, change to db stored time in the future)
63
73
const stopwatchRef = useRef < NodeJS . Timeout | null > ( null ) ;
74
+ const [ matchedTopics , setMatchedTopics ] = useState < string [ ] | undefined > ( undefined ) ;
64
75
65
76
// Chat states
66
77
const [ messageToSend , setMessageToSend ] = useState < string | undefined > (
@@ -116,25 +127,68 @@ export default function CollaborationPage(props: CollaborationProps) {
116
127
) ;
117
128
} ;
118
129
130
+ // Message
131
+ const [ messageApi , contextHolder ] = message . useMessage ( ) ;
132
+
133
+ const successMessage = ( message : string ) => {
134
+ messageApi . open ( {
135
+ type : "success" ,
136
+ content : message ,
137
+ } ) ;
138
+ } ;
139
+
140
+ const sendCodeSavedStatusToMatchedUser = ( ) => {
141
+ if ( ! providerRef . current ) {
142
+ throw new Error ( "Provider not initialized" ) ;
143
+ }
144
+ providerRef . current . awareness . setLocalStateField ( "codeSavedStatus" , true ) ;
145
+ }
146
+
147
+ const handleSubmitCode = async ( ) => {
148
+ if ( ! collaborationId ) {
149
+ throw new Error ( "Collaboration ID not found" ) ;
150
+ }
151
+ const data = await CreateOrUpdateHistory ( {
152
+ title : questionTitle ?? "" ,
153
+ code : code ,
154
+ language : selectedLanguage ,
155
+ user : currentUser ?? "" ,
156
+ matchedUser : matchedUser ?? "" ,
157
+ matchId : collaborationId ?? "" ,
158
+ matchedTopics : matchedTopics ?? [ ] ,
159
+ questionDocRefId : questionDocRefId ?? "" ,
160
+ questionDifficulty : complexity ?? "" ,
161
+ questionTopics : categories ,
162
+ } , collaborationId ) ;
163
+ successMessage ( "Code saved successfully!" ) ;
164
+ sendCodeSavedStatusToMatchedUser ( ) ;
165
+ }
166
+
167
+ const handleCodeChange = ( code : string ) => {
168
+ setCode ( code ) ;
169
+ }
170
+
119
171
// Fetch the question on initialisation
120
172
useEffect ( ( ) => {
121
173
if ( ! isLoading ) {
122
174
setIsLoading ( true ) ;
123
175
}
124
176
125
177
// Retrieve details from localstorage
126
- const docRefId : string = localStorage . getItem ( "docRefId " ) ?? "" ;
178
+ const questionDocRefId : string = localStorage . getItem ( "questionDocRefId " ) ?? "" ;
127
179
const collabId : string = localStorage . getItem ( "collabId" ) ?? "" ;
128
180
const matchedUser : string = localStorage . getItem ( "matchedUser" ) ?? "" ;
129
181
const currentUser : string = localStorage . getItem ( "user" ) ?? "" ;
182
+ const matchedTopics : string [ ] = localStorage . getItem ( "matchedTopics" ) ?. split ( "," ) ?? [ ] ;
130
183
131
184
// Set states from localstorage
132
185
setCollaborationId ( collabId ) ;
133
186
setMatchedUser ( matchedUser ) ;
134
187
setCurrentUser ( currentUser ) ;
188
+ setMatchedTopics ( matchedTopics ) ;
189
+ setQuestionDocRefId ( questionDocRefId ) ;
135
190
136
- // Fetch question and set question states
137
- GetSingleQuestion ( docRefId ) . then ( ( data : Question ) => {
191
+ GetSingleQuestion ( questionDocRefId ) . then ( ( data : Question ) => {
138
192
setQuestionTitle ( `${ data . id } . ${ data . title } ` ) ;
139
193
setComplexity ( data . complexity ) ;
140
194
setCategories ( data . categories ) ;
@@ -202,13 +256,18 @@ export default function CollaborationPage(props: CollaborationProps) {
202
256
// Remove localstorage variables for collaboration
203
257
localStorage . removeItem ( "session-duration" ) ; // TODO: Remove this after collaboration backend data stored
204
258
localStorage . removeItem ( "user" ) ;
205
- localStorage . removeItem ( "collabId" ) ;
206
- localStorage . removeItem ( "docRefId" ) ;
207
259
localStorage . removeItem ( "matchedUser" ) ;
260
+ localStorage . removeItem ( "collabId" ) ;
261
+ localStorage . removeItem ( "questionDocRefId" ) ;
262
+ localStorage . removeItem ( "matchedTopics" ) ;
263
+
264
+ // Redirect back to matching page
265
+ router . push ( "/matching" ) ;
208
266
} ;
209
267
210
268
return (
211
269
< Layout className = "collaboration-layout" >
270
+ { contextHolder }
212
271
< Header selectedKey = { undefined } />
213
272
< Content className = "collaboration-content" >
214
273
< Modal
@@ -318,10 +377,10 @@ export default function CollaborationPage(props: CollaborationProps) {
318
377
Code
319
378
</ div >
320
379
{ /* TODO: Link to execution service for code submission */ }
321
- < Button
322
- icon = { < SendOutlined /> }
323
- iconPosition = "end"
324
- className = "code-submit-button"
380
+ < Button
381
+ icon = { < SendOutlined /> }
382
+ iconPosition = "end"
383
+ onClick = { ( ) => handleSubmitCode ( ) }
325
384
>
326
385
Submit
327
386
</ Button >
@@ -334,6 +393,9 @@ export default function CollaborationPage(props: CollaborationProps) {
334
393
language = { selectedLanguage }
335
394
setMatchedUser = { setMatchedUser }
336
395
handleCloseCollaboration = { handleCloseCollaboration }
396
+ // providerRef={providerRef}
397
+ matchedUser = { matchedUser }
398
+ onCodeChange = { handleCodeChange }
337
399
/>
338
400
) }
339
401
</ div >
0 commit comments