55 Col ,
66 Input ,
77 Layout ,
8+ message ,
89 Row ,
910 Select ,
1011 Tabs ,
@@ -15,7 +16,7 @@ import {
1516import { Content } from "antd/es/layout/layout" ;
1617import "./styles.scss" ;
1718import { useRouter , useSearchParams } from "next/navigation" ;
18- import { useEffect , useState } from "react" ;
19+ import { useEffect , useRef , useState } from "react" ;
1920import { GetSingleQuestion , Question } from "@/app/services/question" ;
2021import {
2122 ClockCircleOutlined ,
@@ -27,29 +28,39 @@ import {
2728} from "@ant-design/icons" ;
2829import { ProgrammingLanguageOptions } from "@/utils/SelectOptions" ;
2930import CollaborativeEditor from "@/components/CollaborativeEditor/CollaborativeEditor" ;
31+ import { CreateOrUpdateHistory } from "@/app/services/history" ;
32+ import { Language } from "@codemirror/language" ;
33+ import { WebrtcProvider } from "y-webrtc" ;
3034
3135interface CollaborationProps { }
3236
3337export default function CollaborationPage ( props : CollaborationProps ) {
3438 const router = useRouter ( ) ;
39+ const providerRef = useRef < WebrtcProvider | null > ( null ) ;
3540
3641 const [ isLoading , setIsLoading ] = useState < boolean > ( false ) ;
3742
3843 // Code Editor States
44+ const [ historyDocRefId , setHistoryDocRefId ] = useState < string | undefined > (
45+ undefined
46+ ) ;
47+ const [ code , setCode ] = useState < string > ( "" ) ;
3948 const [ questionTitle , setQuestionTitle ] = useState < string | undefined > (
4049 undefined
4150 ) ;
51+ const [ questionDocRefId , setQuestionDocRefId ] = useState < string | undefined > ( undefined ) ;
4252 const [ complexity , setComplexity ] = useState < string | undefined > ( undefined ) ;
4353 const [ categories , setCategories ] = useState < string [ ] > ( [ ] ) ; // Store the selected filter categories
4454 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
4656
4757 // Session states
4858 const [ collaborationId , setCollaborationId ] = useState < string | undefined > (
4959 undefined
5060 ) ;
5161 const [ currentUser , setCurrentUser ] = useState < string | undefined > ( undefined ) ;
5262 const [ matchedUser , setMatchedUser ] = useState < string | undefined > ( undefined ) ;
63+ const [ matchedTopics , setMatchedTopics ] = useState < string [ ] | undefined > ( undefined ) ;
5364
5465 // Chat states
5566 const [ messageToSend , setMessageToSend ] = useState < string | undefined > (
@@ -61,6 +72,47 @@ export default function CollaborationPage(props: CollaborationProps) {
6172 undefined
6273 ) ;
6374
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+
64116 // Retrieve the docRefId from query params during page navigation
65117 // const searchParams = useSearchParams();
66118
@@ -71,17 +123,20 @@ export default function CollaborationPage(props: CollaborationProps) {
71123 }
72124
73125 // Retrieve details from localstorage
74- const docRefId : string = localStorage . getItem ( "docRefId " ) ?? "" ;
126+ const questionDocRefId : string = localStorage . getItem ( "questionDocRefId " ) ?? "" ;
75127 const collabId : string = localStorage . getItem ( "collabId" ) ?? "" ;
76128 const matchedUser : string = localStorage . getItem ( "matchedUser" ) ?? "" ;
77129 const currentUser : string = localStorage . getItem ( "user" ) ?? "" ;
130+ const matchedTopics : string [ ] = localStorage . getItem ( "matchedTopics" ) ?. split ( "," ) ?? [ ] ;
78131
79132 // Set states from localstorage
80133 setCollaborationId ( collabId ) ;
81134 setMatchedUser ( matchedUser ) ;
82135 setCurrentUser ( currentUser ) ;
136+ setMatchedTopics ( matchedTopics ) ;
137+ setQuestionDocRefId ( questionDocRefId ) ;
83138
84- GetSingleQuestion ( docRefId ) . then ( ( data : Question ) => {
139+ GetSingleQuestion ( questionDocRefId ) . then ( ( data : Question ) => {
85140 setQuestionTitle ( `${ data . id } . ${ data . title } ` ) ;
86141 setComplexity ( data . complexity ) ;
87142 setCategories ( data . categories ) ;
@@ -121,15 +176,17 @@ export default function CollaborationPage(props: CollaborationProps) {
121176 // Remove localstorage variables for collaboration
122177 localStorage . removeItem ( "user" ) ;
123178 localStorage . removeItem ( "matchedUser" ) ;
124- localStorage . removeItem ( "collaId" ) ;
125- localStorage . removeItem ( "docRefId" ) ;
179+ localStorage . removeItem ( "collabId" ) ;
180+ localStorage . removeItem ( "questionDocRefId" ) ;
181+ localStorage . removeItem ( "matchedTopics" ) ;
126182
127183 // Redirect back to matching page
128184 router . push ( "/matching" ) ;
129185 } ;
130186
131187 return (
132188 < Layout className = "collaboration-layout" >
189+ { contextHolder }
133190 < Header selectedKey = { undefined } />
134191 < Content className = "collaboration-content" >
135192 < Row gutter = { 0 } className = "collab-row" >
@@ -189,7 +246,11 @@ export default function CollaborationPage(props: CollaborationProps) {
189246 Code
190247 </ div >
191248 { /* 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+ >
193254 Submit
194255 </ Button >
195256 </ div >
@@ -207,6 +268,9 @@ export default function CollaborationPage(props: CollaborationProps) {
207268 user = { currentUser }
208269 collaborationId = { collaborationId }
209270 language = { selectedLanguage }
271+ providerRef = { providerRef }
272+ matchedUser = { matchedUser }
273+ onCodeChange = { handleCodeChange }
210274 />
211275 ) }
212276 </ div >
0 commit comments