-
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1443 from mumuki/feature-submissions-store
Feature submissions store
- Loading branch information
Showing
8 changed files
with
179 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
app/assets/javascripts/mumuki_laboratory/application/current-exercise.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/** @type {number} */ | ||
mumuki.currentExerciseId = null; | ||
(() => { | ||
mumuki.load(() => { | ||
// Set global currentExerciseId | ||
const $muExerciseId = $('#mu-exercise-id'); | ||
if ($muExerciseId) { | ||
mumuki.currentExerciseId = Number($muExerciseId.val()); | ||
} else { | ||
mumuki.currentExerciseId = null; | ||
} | ||
}) | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
app/assets/javascripts/mumuki_laboratory/application/submissions-store.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
mumuki.SubmissionsStore = (() => { | ||
const SubmissionsStore = new class { | ||
/** | ||
* @param {number} exerciseId | ||
* @returns {SubmissionStatus} | ||
*/ | ||
getLastSubmissionStatus(exerciseId) { | ||
const submission = this.getLastSubmission(exerciseId); | ||
return submission ? submission.result.status : 'pending'; | ||
} | ||
|
||
/** | ||
* @param {number} exerciseId | ||
* @returns {SubmissionAndResult} | ||
*/ | ||
getLastSubmission(exerciseId) { | ||
const submissionAndResult = window.localStorage.getItem(this._keyFor(exerciseId)); | ||
if (!submissionAndResult) return null; | ||
return JSON.parse(submissionAndResult); | ||
} | ||
|
||
/** | ||
* @param {number} exerciseId | ||
* @param {SubmissionAndResult} submissionAndResult | ||
*/ | ||
setLastSubmission(exerciseId, submissionAndResult) { | ||
window.localStorage.setItem(this._keyFor(exerciseId), this._asString(submissionAndResult)); | ||
} | ||
|
||
/** | ||
* Retrieves the last cached, non-aborted result for the given submission | ||
* | ||
* @param {number} exerciseId | ||
* @param {Submission} submission | ||
* @returns {SubmissionResult} the cached result for this submission | ||
*/ | ||
getCachedResultFor(exerciseId, submission) { | ||
const lastSubmission = this.getLastSubmission(exerciseId); | ||
if (!lastSubmission | ||
|| lastSubmission.result.status === 'aborted' | ||
|| !this.submissionSolutionEquals(lastSubmission.submission, submission)) { | ||
return null; | ||
} | ||
return lastSubmission.result; | ||
} | ||
|
||
/** | ||
* Extract the submission's solution content | ||
* | ||
* @param {Submission} submission | ||
* @returns {string} | ||
*/ | ||
submissionSolutionContent(submission) { | ||
if (submission.solution) { | ||
return submission.solution.content; | ||
} else { | ||
return submission['solution[content]']; | ||
} | ||
} | ||
|
||
/** | ||
* Compares two solutions to determine if they are equivalent | ||
* from the point of view of the code evaluation | ||
* | ||
* @param {Submission} one | ||
* @param {Submission} other | ||
* @returns {boolean} | ||
*/ | ||
submissionSolutionEquals(one, other) { | ||
return this.submissionSolutionContent(one) === this.submissionSolutionContent(other); | ||
} | ||
|
||
// private API | ||
|
||
_asString(object) { | ||
return JSON.stringify(object); | ||
} | ||
|
||
_keyFor(exerciseId) { | ||
return `/exercise/${exerciseId}/submission`; | ||
} | ||
}; | ||
|
||
return SubmissionsStore; | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,12 @@ | ||
<div class="progress-list-flex"> | ||
<% guide.exercises.each do |e| %> | ||
<a <%= turbolinks_enable_for e %> href="<%= exercise_path(e)%>" aria-label="<%= e.navigable_name %>" title="<%= e.navigable_name %>" class="<%= class_for_progress_list_item(e, e == actual)%>"> | ||
<a | ||
<%= turbolinks_enable_for e %> | ||
href="<%= exercise_path(e)%>" | ||
aria-label="<%= e.navigable_name %>" | ||
title="<%= e.navigable_name %>" | ||
data-mu-exercise-id="<%= e.id %>" | ||
class="<%= class_for_progress_list_item(e, e == actual)%>"> | ||
</a> | ||
<% end %> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters