5
5
Col ,
6
6
Input ,
7
7
Layout ,
8
+ message ,
8
9
Row ,
9
10
Select ,
10
11
Tabs ,
@@ -15,7 +16,7 @@ import {
15
16
import { Content } from "antd/es/layout/layout" ;
16
17
import "./styles.scss" ;
17
18
import { useRouter , useSearchParams } from "next/navigation" ;
18
- import { useEffect , useState } from "react" ;
19
+ import { useEffect , useRef , useState } from "react" ;
19
20
import { GetSingleQuestion , Question } from "@/app/services/question" ;
20
21
import {
21
22
ClockCircleOutlined ,
@@ -27,29 +28,39 @@ import {
27
28
} from "@ant-design/icons" ;
28
29
import { ProgrammingLanguageOptions } from "@/utils/SelectOptions" ;
29
30
import CollaborativeEditor from "@/components/CollaborativeEditor/CollaborativeEditor" ;
31
+ import { CreateOrUpdateHistory } from "@/app/services/history" ;
32
+ import { Language } from "@codemirror/language" ;
33
+ import { WebrtcProvider } from "y-webrtc" ;
30
34
31
35
interface CollaborationProps { }
32
36
33
37
export default function CollaborationPage ( props : CollaborationProps ) {
34
38
const router = useRouter ( ) ;
39
+ const providerRef = useRef < WebrtcProvider | null > ( null ) ;
35
40
36
41
const [ isLoading , setIsLoading ] = useState < boolean > ( false ) ;
37
42
38
43
// Code Editor States
44
+ const [ historyDocRefId , setHistoryDocRefId ] = useState < string | undefined > (
45
+ undefined
46
+ ) ;
47
+ const [ code , setCode ] = useState < string > ( "" ) ;
39
48
const [ questionTitle , setQuestionTitle ] = useState < string | undefined > (
40
49
undefined
41
50
) ;
51
+ const [ questionDocRefId , setQuestionDocRefId ] = useState < string | undefined > ( undefined ) ;
42
52
const [ complexity , setComplexity ] = useState < string | undefined > ( undefined ) ;
43
53
const [ categories , setCategories ] = useState < string [ ] > ( [ ] ) ; // Store the selected filter categories
44
54
const [ description , setDescription ] = useState < string | undefined > ( undefined ) ;
45
- const [ selectedLanguage , setSelectedLanguage ] = useState ( "javascript " ) ; // State to hold the selected language item
55
+ const [ selectedLanguage , setSelectedLanguage ] = useState ( "Javascript " ) ; // State to hold the selected language item
46
56
47
57
// Session states
48
58
const [ collaborationId , setCollaborationId ] = useState < string | undefined > (
49
59
undefined
50
60
) ;
51
61
const [ currentUser , setCurrentUser ] = useState < string | undefined > ( undefined ) ;
52
62
const [ matchedUser , setMatchedUser ] = useState < string | undefined > ( undefined ) ;
63
+ const [ matchedTopics , setMatchedTopics ] = useState < string [ ] | undefined > ( undefined ) ;
53
64
54
65
// Chat states
55
66
const [ messageToSend , setMessageToSend ] = useState < string | undefined > (
@@ -61,6 +72,47 @@ export default function CollaborationPage(props: CollaborationProps) {
61
72
undefined
62
73
) ;
63
74
75
+ // Message
76
+ const [ messageApi , contextHolder ] = message . useMessage ( ) ;
77
+
78
+ const successMessage = ( message : string ) => {
79
+ messageApi . open ( {
80
+ type : "success" ,
81
+ content : message ,
82
+ } ) ;
83
+ } ;
84
+
85
+ const sendCodeSavedStatusToMatchedUser = ( ) => {
86
+ if ( ! providerRef . current ) {
87
+ throw new Error ( "Provider not initialized" ) ;
88
+ }
89
+ providerRef . current . awareness . setLocalStateField ( "codeSavedStatus" , true ) ;
90
+ }
91
+
92
+ const handleSubmitCode = async ( ) => {
93
+ if ( ! collaborationId ) {
94
+ throw new Error ( "Collaboration ID not found" ) ;
95
+ }
96
+ const data = await CreateOrUpdateHistory ( {
97
+ title : questionTitle ?? "" ,
98
+ code : code ,
99
+ language : selectedLanguage ,
100
+ user : currentUser ?? "" ,
101
+ matchedUser : matchedUser ?? "" ,
102
+ matchId : collaborationId ?? "" ,
103
+ matchedTopics : matchedTopics ?? [ ] ,
104
+ questionDocRefId : questionDocRefId ?? "" ,
105
+ questionDifficulty : complexity ?? "" ,
106
+ questionTopics : categories ,
107
+ } , collaborationId ) ;
108
+ successMessage ( "Code saved successfully!" ) ;
109
+ sendCodeSavedStatusToMatchedUser ( ) ;
110
+ }
111
+
112
+ const handleCodeChange = ( code : string ) => {
113
+ setCode ( code ) ;
114
+ }
115
+
64
116
// Retrieve the docRefId from query params during page navigation
65
117
// const searchParams = useSearchParams();
66
118
@@ -71,17 +123,20 @@ export default function CollaborationPage(props: CollaborationProps) {
71
123
}
72
124
73
125
// Retrieve details from localstorage
74
- const docRefId : string = localStorage . getItem ( "docRefId " ) ?? "" ;
126
+ const questionDocRefId : string = localStorage . getItem ( "questionDocRefId " ) ?? "" ;
75
127
const collabId : string = localStorage . getItem ( "collabId" ) ?? "" ;
76
128
const matchedUser : string = localStorage . getItem ( "matchedUser" ) ?? "" ;
77
129
const currentUser : string = localStorage . getItem ( "user" ) ?? "" ;
130
+ const matchedTopics : string [ ] = localStorage . getItem ( "matchedTopics" ) ?. split ( "," ) ?? [ ] ;
78
131
79
132
// Set states from localstorage
80
133
setCollaborationId ( collabId ) ;
81
134
setMatchedUser ( matchedUser ) ;
82
135
setCurrentUser ( currentUser ) ;
136
+ setMatchedTopics ( matchedTopics ) ;
137
+ setQuestionDocRefId ( questionDocRefId ) ;
83
138
84
- GetSingleQuestion ( docRefId ) . then ( ( data : Question ) => {
139
+ GetSingleQuestion ( questionDocRefId ) . then ( ( data : Question ) => {
85
140
setQuestionTitle ( `${ data . id } . ${ data . title } ` ) ;
86
141
setComplexity ( data . complexity ) ;
87
142
setCategories ( data . categories ) ;
@@ -121,15 +176,17 @@ export default function CollaborationPage(props: CollaborationProps) {
121
176
// Remove localstorage variables for collaboration
122
177
localStorage . removeItem ( "user" ) ;
123
178
localStorage . removeItem ( "matchedUser" ) ;
124
- localStorage . removeItem ( "collaId" ) ;
125
- localStorage . removeItem ( "docRefId" ) ;
179
+ localStorage . removeItem ( "collabId" ) ;
180
+ localStorage . removeItem ( "questionDocRefId" ) ;
181
+ localStorage . removeItem ( "matchedTopics" ) ;
126
182
127
183
// Redirect back to matching page
128
184
router . push ( "/matching" ) ;
129
185
} ;
130
186
131
187
return (
132
188
< Layout className = "collaboration-layout" >
189
+ { contextHolder }
133
190
< Header selectedKey = { undefined } />
134
191
< Content className = "collaboration-content" >
135
192
< Row gutter = { 0 } className = "collab-row" >
@@ -189,7 +246,11 @@ export default function CollaborationPage(props: CollaborationProps) {
189
246
Code
190
247
</ div >
191
248
{ /* TODO: Link to execution service for code submission */ }
192
- < Button icon = { < SendOutlined /> } iconPosition = "end" >
249
+ < Button
250
+ icon = { < SendOutlined /> }
251
+ iconPosition = "end"
252
+ onClick = { ( ) => handleSubmitCode ( ) }
253
+ >
193
254
Submit
194
255
</ Button >
195
256
</ div >
@@ -207,6 +268,9 @@ export default function CollaborationPage(props: CollaborationProps) {
207
268
user = { currentUser }
208
269
collaborationId = { collaborationId }
209
270
language = { selectedLanguage }
271
+ providerRef = { providerRef }
272
+ matchedUser = { matchedUser }
273
+ onCodeChange = { handleCodeChange }
210
274
/>
211
275
) }
212
276
</ div >
0 commit comments