import Ember from 'ember';
import ExpPlayer from 'exp-player/components/exp-player/component';

export default ExpPlayer.extend({
    // Show an early exit modal (return non-empty string), but only with browser default message- don't require translation of custom text.
    messageEarlyExitModal: ' ',

    currentUser: Ember.inject.service(),

    actions: {
        sessionCompleted() {
            // Mark the current user account as having completed a session, then, also update the session model
            // If this fails to save the account due to normal network issues, it will still update the session but
            // swallow the error
            return this.get('currentUser').getCurrentUser().then(([account]) => {
                // Deal with `extra` field sometimes being undefined
                let dest = account.get('extra') || {};
                dest['hasCompletedStudy'] = true;
                account.set('extra', dest);
                return account.save();
            }).catch( e => console.log('Could not mark account as having completed study:', e)
            ).finally(() => this._super(...arguments));
        },
    }
});

import Ember from 'ember';
import ExpPlayer from 'exp-player/components/exp-player/component';

export default ExpPlayer.extend({
    // Show an early exit modal (return non-empty string), but only with browser default message- don't require translation of custom text.
    messageEarlyExitModal: ' ',

    currentUser: Ember.inject.service(),

    actions: {
        sessionCompleted() {
            // Mark the current user account as having completed a session, then, also update the session model
            // If this fails to save the account due to normal network issues, it will still update the session but
            // swallow the error
            this.get('session').set('completed', true);

            return this.get('currentUser').getCurrentUser().then(([account]) => {
                // Deal with `extra` field sometimes being undefined
                let dest = account.get('extra') || {};
                dest['hasCompletedStudy'] = true;
                account.set('extra', dest);
                return account.save();
            }).catch( e => console.error('Could not mark account as having completed study:', e));
        }
    }
});

Fix race condition that prevented session from being marked as complete
Fix race condition that prevented session from being marked as complete

Ember actions are fire and forget (unless using closure actions + return values).

 So when the frame next/save action happened, it had the effect of saving the frame payload before the flag got set. This changes the order: it marks the session as complete, then saves the account, instead of tying the two saves together.

  This increases the risk of data / state inconsistency slightly (between session and account models) but it's the bets option shy of large internal refactorings.

[#LEI-330]
