fix: address code review findings

- Guard against empty awardTypeIds array in AwardRepository
- Refactor Stimulus controller to use data-action attributes instead of
  imperative addEventListener (fixes duplicate listener issue)

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
thibaud-leclere
2026-04-01 23:10:38 +02:00
parent b637b725d8
commit 51a9f49797
3 changed files with 17 additions and 19 deletions

View File

@@ -3,6 +3,14 @@ import { Controller } from '@hotwired/stimulus';
export default class extends Controller { export default class extends Controller {
static targets = ['hintType', 'awardSection', 'allAwards', 'awardType']; static targets = ['hintType', 'awardSection', 'allAwards', 'awardType'];
enforceMinOneChecked(event) {
const checked = this.hintTypeTargets.filter((e) => e.checked);
if (checked.length === 0) {
event.target.checked = true;
}
this.toggleAwardSection();
}
toggleAwardSection() { toggleAwardSection() {
const awardChecked = this.hintTypeTargets.find( const awardChecked = this.hintTypeTargets.find(
(el) => el.name === 'hint_award' (el) => el.name === 'hint_award'
@@ -22,20 +30,4 @@ export default class extends Controller {
const allChecked = this.awardTypeTargets.every((el) => el.checked); const allChecked = this.awardTypeTargets.every((el) => el.checked);
this.allAwardsTarget.checked = allChecked; this.allAwardsTarget.checked = allChecked;
} }
hintTypeTargetConnected() {
this.#bindMinOneChecked();
}
#bindMinOneChecked() {
this.hintTypeTargets.forEach((el) => {
el.addEventListener('change', () => {
const checked = this.hintTypeTargets.filter((e) => e.checked);
if (checked.length === 0) {
el.checked = true;
}
this.toggleAwardSection();
});
});
}
} }

View File

@@ -36,6 +36,10 @@ class AwardRepository extends ServiceEntityRepository
->andWhere('a.actor = :actorId') ->andWhere('a.actor = :actorId')
->setParameter('actorId', $actorId); ->setParameter('actorId', $actorId);
if ($awardTypeIds !== null && empty($awardTypeIds)) {
return null;
}
if ($awardTypeIds !== null) { if ($awardTypeIds !== null) {
$qb->andWhere('a.awardType IN (:typeIds)') $qb->andWhere('a.awardType IN (:typeIds)')
->setParameter('typeIds', $awardTypeIds); ->setParameter('typeIds', $awardTypeIds);

View File

@@ -75,18 +75,20 @@
<div class="config-hint-types"> <div class="config-hint-types">
<label class="config-checkbox"> <label class="config-checkbox">
<input type="checkbox" name="hint_film" value="1" checked <input type="checkbox" name="hint_film" value="1" checked
data-game-config-target="hintType"> data-game-config-target="hintType"
data-action="change->game-config#enforceMinOneChecked">
Film Film
</label> </label>
<label class="config-checkbox"> <label class="config-checkbox">
<input type="checkbox" name="hint_character" value="1" checked <input type="checkbox" name="hint_character" value="1" checked
data-game-config-target="hintType"> data-game-config-target="hintType"
data-action="change->game-config#enforceMinOneChecked">
Rôle Rôle
</label> </label>
<label class="config-checkbox"> <label class="config-checkbox">
<input type="checkbox" name="hint_award" value="1" checked <input type="checkbox" name="hint_award" value="1" checked
data-game-config-target="hintType" data-game-config-target="hintType"
data-action="change->game-config#toggleAwardSection"> data-action="change->game-config#enforceMinOneChecked">
Récompense Récompense
</label> </label>
</div> </div>