1
- import React , { useState , useEffect } from ' react'
2
- import "./AttemptView.css"
3
- import CodeEditor from '../Collaboration/CodeEditor'
4
- import Utility from "../../Utility/Utility"
5
- import axios from ' axios' ;
1
+ import React , { useState , useEffect } from " react" ;
2
+ import Editor from "@monaco-editor/react" ;
3
+ import "./AttemptView.css" ;
4
+ import Utility from "../../Utility/Utility" ;
5
+ import axios from " axios" ;
6
6
7
- import back_icon from "../Assets/back.png"
7
+ import back_icon from "../Assets/back.png" ;
8
8
9
- const COLLABORATION_SERVICE_HOST = "http://localhost:3005/" ; ;
9
+ const COLLABORATION_SERVICE_HOST = "http://localhost:3005/" ;
10
10
11
11
export const AttemptView = ( { attempt, setIsList} ) => {
12
- //to be replaced with attempt.code
13
- const [ code , setCode ] = useState ( "#Here is the code from your attempt" ) ;
14
- const [ lang , setLang ] = useState ( "python" )
15
-
16
- let tagClass = Utility . setDifficultyTag ( "user-q-tag" , attempt . complexity ) ;
17
-
18
- const fetchCode = async ( ) => {
19
- const result = await axios . get ( COLLABORATION_SERVICE_HOST + "getCollaborationHistory/" + attempt . sessionId )
20
-
21
- if ( result . status === 200 ) {
22
- console . log ( result )
23
- setCode ( result . data . codes ) ;
24
- setLang ( result . data . language ) ;
25
- }
26
- }
27
-
28
- useEffect ( ( ) => {
29
- fetchCode ( ) ;
30
- } , [ ] )
31
-
32
- return (
33
- < div className = "attempt-view-container" >
34
- < div className = "back-container" >
35
- < img src = { back_icon } className = "back-button" alt = "" onClick = { ( ) => setIsList ( true ) } />
36
- </ div >
37
- < div className = "attempt-details-container" >
38
- < div className = "attempt-q" >
39
- < div className = "attempt-q-header" >
40
- < div className = "attempt-q-name" > { attempt . questionTitle } </ div >
41
- < div className = "attempt-q-tags" >
42
- < div className = { tagClass } > { attempt . questionComplexity } </ div >
43
- < div className = "user-q-tag" > { attempt . questionCategory } </ div >
44
- </ div >
45
- </ div >
46
- < div className = "attempt-q-desc" >
47
- { attempt . questionDescription }
48
- </ div >
49
- </ div >
50
- < div className = "attempt-code" >
51
- { /* replace language with state variable - attempt.language*/ }
52
- < CodeEditor code = { code } setCode = { setCode } language = { lang } isReadOnly = { true } />
53
- </ div >
54
- </ div >
55
- </ div >
56
- )
57
- }
12
+ //to be replaced with attempt.code
13
+ const [ code , setCode ] = useState ( [ ] ) ;
14
+ const [ lang , setLang ] = useState ( "python" ) ;
15
+
16
+ let tagClass = Utility . setDifficultyTag ( "user-q-tag" , attempt . complexity ) ;
17
+
18
+ const fetchCode = async ( ) => {
19
+ const result = await axios . get (
20
+ COLLABORATION_SERVICE_HOST +
21
+ "getCollaborationHistory/" +
22
+ attempt . sessionId
23
+ ) ;
24
+
25
+ if ( result . status === 200 ) {
26
+ console . log ( result ) ;
27
+ setCode ( result . data . codes ) ;
28
+ if ( result . data . language === "None" ) {
29
+ setLang ( result . data . language ) ;
30
+ }
31
+ }
32
+ } ;
33
+
34
+ useEffect ( ( ) => {
35
+ fetchCode ( ) ;
36
+ } , [ ] ) ;
37
+
38
+ const toCode = ( collabInput ) => {
39
+ collabInput . sort ( ( a , b ) => {
40
+ return a . line - b . line ;
41
+ } ) ;
42
+ return collabInput . map ( ( item ) => item . code ) . join ( "\n" ) ;
43
+ } ;
44
+
45
+ return (
46
+ < div className = "attempt-view-container" >
47
+ < div className = "back-container" >
48
+ < img
49
+ src = { back_icon }
50
+ className = "back-button"
51
+ alt = ""
52
+ onClick = { ( ) => setIsList ( true ) }
53
+ />
54
+ </ div >
55
+ < div className = "attempt-details-container" >
56
+ < div className = "attempt-q" >
57
+ < div className = "attempt-q-header" >
58
+ < div className = "attempt-q-name" > { attempt . questionTitle } </ div >
59
+ < div className = "attempt-q-tags" >
60
+ < div className = { tagClass } > { attempt . questionComplexity } </ div >
61
+ < div className = "user-q-tag" > { attempt . questionCategory } </ div >
62
+ </ div >
63
+ </ div >
64
+ < div className = "attempt-q-desc" > { attempt . questionDescription } </ div >
65
+ </ div >
66
+ < div className = "attempt-code" >
67
+ < Editor
68
+ language = { lang . toLowerCase ( ) }
69
+ theme = "vs-light"
70
+ value = { toCode ( code ) }
71
+ options = { {
72
+ inlineSuggest : true ,
73
+ fontSize : "16px" ,
74
+ formatOnType : true ,
75
+ minimap : { enabled : false } ,
76
+ readOnly : true ,
77
+ } }
78
+ />
79
+ </ div >
80
+ </ div >
81
+ </ div >
82
+ ) ;
83
+ } ;
0 commit comments