-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed a bug that caused validation on displaying previously-submitted…
… answers a little overzealous.
- Loading branch information
1 parent
fd6ae0f
commit e779872
Showing
4 changed files
with
34 additions
and
29 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
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
25 changes: 0 additions & 25 deletions
25
projects/gameboard-ui/src/app/game/pipes/index-to-submitted-answer.pipe.ts
This file was deleted.
Oops, something went wrong.
30 changes: 30 additions & 0 deletions
30
projects/gameboard-ui/src/app/game/pipes/index-to-submitted-answers.pipe.ts
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,30 @@ | ||
import { Pipe, PipeTransform } from '@angular/core'; | ||
import { ChallengeSubmissionViewModel } from '@/api/challenges.models'; | ||
import { unique } from 'projects/gameboard-ui/src/tools'; | ||
|
||
// takes the index of a challenge question (e.g. 1 = the second question of the challenge), returns a string which concatenates | ||
// all unique answers submitted for the question | ||
@Pipe({ name: 'indexToSubmittedAnswers' }) | ||
export class IndexToSubmittedAnswersPipe implements PipeTransform { | ||
transform(questionIndex: number, submissions: ChallengeSubmissionViewModel[], hideLastResponse = false): string { | ||
if (questionIndex < 0 || !submissions || !submissions.length) | ||
throw new Error("Can't use IndexToSubmittedAnswer pipe without an index and submitted answers."); | ||
|
||
// ensure that all submissions have at least as many answers as are required to retrieve the questionIndexth answer | ||
const submissionLengths = submissions.map(s => s.answers.length); | ||
if (submissionLengths.some(length => length <= questionIndex)) { | ||
throw new Error(`Can't use IndexToSubmittedAnswer pipe with an out-of-range index (${questionIndex}, ${submissionLengths})`); | ||
} | ||
|
||
// the final submitted answer may be the correct answer (if they got the question right). We show that elsewhere, so hide | ||
// it if requested | ||
let displayedSubmissions = [...submissions]; | ||
if (hideLastResponse) | ||
displayedSubmissions = displayedSubmissions.slice(0, displayedSubmissions.length - 1); | ||
|
||
return unique( | ||
displayedSubmissions | ||
.map(submission => `${submission.answers[questionIndex] ? `"${submission.answers[questionIndex]}"` : "(no response)"}`) | ||
).join(", "); | ||
} | ||
} |