@@ -4,7 +4,7 @@ import React from 'react';
44import ReactDOM from 'react-dom' ;
55import { TestResults } from '@cdo/apps/constants' ;
66import { getStore } from '../redux' ;
7- import { mergeProgress } from '../progressRedux' ;
7+ import { clearProgress , mergeProgress } from '../progressRedux' ;
88import { SignInState } from '@cdo/apps/templates/currentUserRedux' ;
99import { setVerified } from '@cdo/apps/code-studio/verifiedTeacherRedux' ;
1010import {
@@ -33,6 +33,7 @@ import queryString from 'query-string';
3333import * as imageUtils from '@cdo/apps/imageUtils' ;
3434import trackEvent from '../../util/trackEvent' ;
3535import msg from '@cdo/locale' ;
36+ import _ from 'lodash' ;
3637
3738// Max milliseconds to wait for last attempt data from the server
3839var LAST_ATTEMPT_TIMEOUT = 5000 ;
@@ -51,8 +52,22 @@ const SHARE_IMAGE_NAME = '_share_image.png';
5152 * @param {Object<number, TestResult> } serverProgress Mapping from levelId to TestResult
5253 */
5354function mergeProgressData ( scriptName , serverProgress ) {
55+ if ( ! serverProgress ) {
56+ return ;
57+ }
5458 const store = getStore ( ) ;
55- store . dispatch ( mergeProgress ( serverProgress ) ) ;
59+
60+ // The server returned progress. This is the source of truth.
61+ // Note: Changes to the progressRedux also update the sessionStorage,
62+ // so this will clear and update the sessionStorage too.
63+ store . dispatch ( clearProgress ( ) ) ;
64+ store . dispatch (
65+ mergeProgress (
66+ _ . mapValues ( serverProgress , level =>
67+ level . submitted ? TestResults . SUBMITTED_RESULT : level . result
68+ )
69+ )
70+ ) ;
5671
5772 Object . keys ( serverProgress ) . forEach ( levelId => {
5873 // Write down new progress in sessionStorage
@@ -392,8 +407,7 @@ function loadAppAsync(appOptions) {
392407 appOptions . disableSocialShare = data . disableSocialShare ;
393408
394409 // Merge progress from server (loaded via AJAX)
395- const serverProgress = data . progress || { } ;
396- mergeProgressData ( appOptions . scriptName , serverProgress ) ;
410+ mergeProgressData ( appOptions . scriptName , data . progress ) ;
397411
398412 if ( ! lastAttemptLoaded ) {
399413 if ( data . lastAttempt ) {
@@ -435,6 +449,11 @@ function loadAppAsync(appOptions) {
435449 }
436450
437451 const store = getStore ( ) ;
452+
453+ // Note: We aren't guaranteed that currentUser.signInState will have been set at this point.
454+ // It gets set on document.ready. However, it is highly unlikely that the server will return
455+ // a response before document.ready is triggered. So far, this hasn't caused problems, so not
456+ // worrying about this for now.
438457 const signInState = store . getState ( ) . currentUser . signInState ;
439458 if ( signInState === SignInState . SignedIn ) {
440459 progress . showDisabledBubblesAlert ( ) ;
0 commit comments