From 8c42e8558579a8e0c6f3202055d5841b418c0199 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Janis=20Daniel=20Da=CC=88hne?=
 <janis.daehne2@student.uni-halle.de>
Date: Mon, 18 Nov 2019 15:51:43 +0100
Subject: [PATCH] - fixed issue #139 - sometimes the initial ace session was
 not properly overwritten in the tutor view (feedback editor)

---
 .../aceEditors/aceEditorsStorageHelper.ts     | 10 ++++++++
 .../feedbackPanel/feedbackEditorView.tsx      |  7 ++----
 .../crud/getSingleAssessmentReducer.ts        | 25 +++++++++++++++++++
 3 files changed, 37 insertions(+), 5 deletions(-)

diff --git a/src/components/codeEditors/aceEditors/aceEditorsStorageHelper.ts b/src/components/codeEditors/aceEditors/aceEditorsStorageHelper.ts
index 6554d9b9..619fb200 100644
--- a/src/components/codeEditors/aceEditors/aceEditorsStorageHelper.ts
+++ b/src/components/codeEditors/aceEditors/aceEditorsStorageHelper.ts
@@ -31,6 +31,11 @@ export class AceEditorsStorageHelper {
 
   static editorsMap: EditorMap = {}
 
+  /**
+   * this will overwrite the editor session map with the given initial tab
+   * @param id
+   * @param initialTab
+   */
   public static addEditor(id: string, initialTab: AceStoredTabLike) {
     const initialSession = createEditSession(initialTab.content, `ace/mode/${initialTab.language}` as any)
 
@@ -41,6 +46,11 @@ export class AceEditorsStorageHelper {
     this.editorsMap[id] = [initialWrappedTab]
   }
 
+  /**
+   * this will overwrite the editor session map with the given initial tabs
+   * @param id
+   * @param initialTabs
+   */
   public static addEditorWithTabs(id: string, initialTabs: AceStoredTabLike[]) {
 
     const tabs: EditorWrapperTab[] = []
diff --git a/src/components/sites/tutorViewSite/feedbackPanel/feedbackEditorView.tsx b/src/components/sites/tutorViewSite/feedbackPanel/feedbackEditorView.tsx
index 29996b5c..8c3ec841 100644
--- a/src/components/sites/tutorViewSite/feedbackPanel/feedbackEditorView.tsx
+++ b/src/components/sites/tutorViewSite/feedbackPanel/feedbackEditorView.tsx
@@ -63,16 +63,13 @@ class FeedbackEditorView extends React.Component<Props, any> {
 
     const editorTabs = AceEditorsStorageHelper.getEditor(tutorViewTutorFeedbackForStudentEditor)
 
+    //this should never happen because we overwrite any default session when we get the assessment from backend
+    //(see reducer)
     if (editorTabs && editorTabs.length === 1 && editorTabs[0].tab.id === -1) {
       //this was set by the initial tutor view load with the default assessment
       //because we mounted the feedback editor and then changed tabs...
       AceEditorsStorageHelper.removeEditor(tutorViewTutorFeedbackForStudentEditor)
       AceEditorsStorageHelper.addEditor(tutorViewTutorFeedbackForStudentEditor, tempEditorTab)
-
-    } else if (editorTabs === undefined) { //when we load another submission but the feedback editor is visible --> no mount --> no default session
-      //so create a default session (simple editor will re-use the this session if the associated tab has the same id)
-      //this allows us to clear all e.g. undo else we would undo and get the previous/next submission feedback
-      AceEditorsStorageHelper.addEditor(tutorViewTutorFeedbackForStudentEditor, tempEditorTab)
     }
 
     return (
diff --git a/src/state/reducers/tutorViewSite/crud/getSingleAssessmentReducer.ts b/src/state/reducers/tutorViewSite/crud/getSingleAssessmentReducer.ts
index 52d58a05..7dab23cb 100644
--- a/src/state/reducers/tutorViewSite/crud/getSingleAssessmentReducer.ts
+++ b/src/state/reducers/tutorViewSite/crud/getSingleAssessmentReducer.ts
@@ -7,6 +7,9 @@ import Logger from "../../../../helpers/logger";
 import {AssessmentFullBase} from "../../../../types/submissions";
 import {ReduxPromiseAction, ReduxPromiseFulfilledAction, ReduxPromiseRejectedAction} from "../../index";
 import {notExhaustive} from "../../_notExhausiveHelper";
+import {AceEditorsStorageHelper} from '../../../../components/codeEditors/aceEditors/aceEditorsStorageHelper'
+import {tutorViewTutorFeedbackForStudentEditor} from '../../../../constants'
+import {EditorTab} from '../../../../components/codeEditors/multiTabCodeEditorProps'
 
 export interface GET_singleAssessmentAction
   extends ActionBase, ReduxPromiseAction<AssessmentFullBase, GET_singleAssessment_PENDINGAction, GET_singleAssessment_FULFILLEDAction, GET_singleAssessment_REJECTEDAction> {
@@ -51,6 +54,28 @@ export function reducer(state: State = initial, action: AllActions): State {
         isLoading: true
       }
     case ActionType.GET_singleAssessment_FULFILLED:
+
+      if (state.exercise.id === -1) {
+        Logger.error('[getUserSolutionReducer] state.exercise must be loaded before loading the assessment!')
+      }
+
+      let releaseFinishedAndAutomaticAssessmentHasFinished =  state.exercise.exerciseRelease.isReleased &&
+        state.exercise.exerciseRelease.hasAutomaticAssessmentFinished
+
+      const tempEditorTab: EditorTab = {
+        id: 0, //not -1 else we cannot distinct between this and the default tab/session
+        content: (action.payload.hasAssessment && action.payload.feedbackForStudent !== null)
+                 ? action.payload.feedbackForStudent
+                 : '',
+        language: 'markdown',
+        isReadonly: releaseFinishedAndAutomaticAssessmentHasFinished === false || action.payload.hasAssessment === false,
+        fileNameWithExtensions: '',
+        displayIndex: 1,
+        isMainFile: true,
+        isContentVisibleForUser: true,
+      }
+      AceEditorsStorageHelper.addEditor(tutorViewTutorFeedbackForStudentEditor, tempEditorTab)
+
       return {
         ...state,
         isLoading: false,
-- 
GitLab