@@ -23,7 +23,7 @@ export type AssessmentWorkspaceProps = DispatchProps & OwnProps & StateProps
2323export type StateProps = {
2424 activeTab : number
2525 assessment ?: IAssessment
26- editorValue ? : string
26+ editorValue : string | null
2727 editorWidth : string
2828 isRunning : boolean
2929 output : InterpreterOutput [ ]
@@ -58,6 +58,7 @@ export type DispatchProps = {
5858 handleReplOutputClear : ( ) => void
5959 handleReplValueChange : ( newValue : string ) => void
6060 handleResetWorkspace : ( ) => void
61+ handleSave : ( id : number , answer : number | string ) => void
6162 handleSideContentHeightChange : ( heightChange : number ) => void
6263 handleUpdateCurrentAssessmentId : ( assessmentId : number , questionId : number ) => void
6364}
@@ -76,13 +77,16 @@ class AssessmentWorkspace extends React.Component<
7677 * occurs after the call to checkWorkspaceReset finishes.
7778 */
7879 public componentDidMount ( ) {
79- this . checkWorkspaceReset ( this . props )
8080 this . props . handleAssessmentFetch ( this . props . assessmentId )
8181 if ( this . props . questionId === 0 ) {
8282 this . setState ( { showOverlay : true } )
8383 }
8484 }
8585
86+ public componentDidUpdate ( ) {
87+ this . checkWorkspaceReset ( this . props )
88+ }
89+
8690 public render ( ) {
8791 if ( this . props . assessment === undefined || this . props . assessment . questions . length === 0 ) {
8892 return (
@@ -112,15 +116,18 @@ class AssessmentWorkspace extends React.Component<
112116 ? this . props . assessment . questions . length - 1
113117 : this . props . questionId
114118 const question : IQuestion = this . props . assessment . questions [ questionId ]
119+ const editorValue =
120+ question . type === QuestionTypes . programming
121+ ? question . answer !== null
122+ ? ( ( question as IProgrammingQuestion ) . answer as string )
123+ : ( question as IProgrammingQuestion ) . solutionTemplate
124+ : null
115125 const workspaceProps : WorkspaceProps = {
116126 controlBarProps : this . controlBarProps ( this . props , questionId ) ,
117127 editorProps :
118128 question . type === QuestionTypes . programming
119129 ? {
120- editorValue :
121- this . props . editorValue !== undefined
122- ? this . props . editorValue
123- : ( question as IProgrammingQuestion ) . solutionTemplate ,
130+ editorValue : editorValue ! ,
124131 handleEditorEval : this . props . handleEditorEval ,
125132 handleEditorValueChange : this . props . handleEditorValueChange
126133 }
@@ -151,8 +158,6 @@ class AssessmentWorkspace extends React.Component<
151158 /**
152159 * Checks if there is a need to reset the workspace, then executes
153160 * a dispatch (in the props) if needed.
154- *
155- * @param props the props passed to the component
156161 */
157162 private checkWorkspaceReset ( props : AssessmentWorkspaceProps ) {
158163 /* Don't reset workspace if assessment not fetched yet. */
@@ -168,12 +173,22 @@ class AssessmentWorkspace extends React.Component<
168173 this . props . storedAssessmentId !== assessmentId ||
169174 this . props . storedQuestionId !== questionId
170175 ) {
171- const chapter = this . props . assessment . questions [ questionId ] . library . chapter
172- const externalName = this . props . assessment . questions [ questionId ] . library . externalLibraryName
173- const externals = this . props . assessment . questions [ questionId ] . library . externals
176+ const question = this . props . assessment . questions [ questionId ]
177+ const chapter = question . library . chapter
178+ const externalName = question . library . externalLibraryName
179+ const externals = question . library . externals
180+ const editorValue =
181+ question . type === QuestionTypes . programming
182+ ? question . answer !== null
183+ ? ( ( question as IProgrammingQuestion ) . answer as string )
184+ : ( question as IProgrammingQuestion ) . solutionTemplate
185+ : null
174186 this . props . handleUpdateCurrentAssessmentId ( assessmentId , questionId )
175187 this . props . handleResetWorkspace ( )
176188 this . props . handleClearContext ( chapter , externals , externalName )
189+ if ( editorValue ) {
190+ this . props . handleEditorValueChange ( editorValue )
191+ }
177192 }
178193 }
179194
@@ -223,6 +238,11 @@ class AssessmentWorkspace extends React.Component<
223238 onClickNext : ( ) => history . push ( assessmentWorkspacePath + `/${ ( questionId + 1 ) . toString ( ) } ` ) ,
224239 onClickPrevious : ( ) =>
225240 history . push ( assessmentWorkspacePath + `/${ ( questionId - 1 ) . toString ( ) } ` ) ,
241+ onClickSave : ( ) =>
242+ this . props . handleSave (
243+ this . props . assessment ! . questions [ questionId ] . id ,
244+ this . props . editorValue !
245+ ) ,
226246 sourceChapter : this . props . assessment ! . questions [ questionId ] . library . chapter
227247 }
228248 }
0 commit comments