herum-shared 1.0.11 → 1.0.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1408,25 +1408,24 @@ class QuizMultiAnswerQuestionComponent {
1408
1408
  }
1409
1409
  ngOnChanges(changes) {
1410
1410
  if (changes['question'] && this.question) {
1411
- this.answersForm = this.fb.group({});
1411
+ const isToInitializeFormControls = Object.keys(this.answersForm.controls).length && changes['question'].currentValue.id !== changes['question'].previousValue?.id;
1412
+ if (isToInitializeFormControls)
1413
+ this.answersForm = this.fb.group({});
1412
1414
  this.question.closedContent?.answers?.forEach((answer, index) => {
1413
1415
  const controlName = this.answerControlNamePrefix + index;
1414
1416
  this.answersForm.addControl(controlName, new FormControl(this.getFormControlValue(index)));
1415
- if (Object.keys(this.answersForm.controls).length && changes['question'].currentValue.id !== changes['question'].previousValue?.id) {
1417
+ if (isToInitializeFormControls) {
1416
1418
  this.answersForm.get(controlName).valueChanges
1417
1419
  .pipe(takeUntil(this.destroySubject$))
1418
1420
  .subscribe((isChosen) => this._onAnswersChange(this.question.closedContent?.answers[index], isChosen));
1419
1421
  }
1420
- else {
1422
+ else
1421
1423
  this.answersForm.controls[controlName].setValue(this.getFormControlValue(index), { emitEvent: false });
1422
- }
1423
1424
  });
1424
- if (this.areAnswersRevealed || this.isCheckingMode) {
1425
+ if (this.areAnswersRevealed || this.isCheckingMode)
1425
1426
  this.setAnswerTypeAttributes();
1426
- }
1427
- if (this.correctAnswerCount === null || this.correctAnswerCount === undefined) {
1427
+ if (this.correctAnswerCount === null || this.correctAnswerCount === undefined)
1428
1428
  this.correctAnswerCount = this.question.closedContent?.answers?.filter(answer => answer.isCorrect).length;
1429
- }
1430
1429
  }
1431
1430
  }
1432
1431
  setAnswerTypeAttributes() {
@@ -1456,13 +1455,20 @@ class QuizMultiAnswerQuestionComponent {
1456
1455
  return quizData.questions.find((question) => question.closedContent?.answers?.find((answer) => answer.id === answerId)?.isCorrect);
1457
1456
  }
1458
1457
  _onAnswersChange(answer, isChosen) {
1459
- let userAnswer = this.userAnswers.find(userAnswer => userAnswer.id == answer.id);
1460
- userAnswer ? (userAnswer.isChosen = isChosen) :
1461
- this.userAnswers.push({
1462
- id: answer.id,
1463
- index: answer.index,
1464
- isChosen
1465
- });
1458
+ const userAnswerIndex = this.userAnswers.findIndex(userAnswer => userAnswer.id == answer.id);
1459
+ const nextUserAnswer = {
1460
+ id: answer.id,
1461
+ index: answer.index,
1462
+ isChosen: isChosen
1463
+ };
1464
+ if (userAnswerIndex === -1)
1465
+ this.userAnswers.push(nextUserAnswer);
1466
+ else if (!isChosen)
1467
+ this.userAnswers[userAnswerIndex].isChosen = false;
1468
+ else {
1469
+ this.userAnswers.splice(userAnswerIndex, 1);
1470
+ this.userAnswers.push(nextUserAnswer);
1471
+ }
1466
1472
  this.isValid = this.userAnswers.length == this.correctAnswerCount;
1467
1473
  this.onAnswersChange.emit(this.userAnswers);
1468
1474
  this.onValidationChange.emit(this.isValid);