Skip to content

Commit

Permalink
Corriger la suppresion de détection dans la zone
Browse files Browse the repository at this point in the history
Corrige les pickers en cas de suppression d'une détection d'un picker
sur le formulaire de zone.
En cas de suppression la valeur était ajoutée au tableau alors qu'elle
aurait du être retirée.

- On est obligé d'utiliser "change" (et non pas addItem et removeItem)
  car c'est la seule méthode qui filtre uniquement sur les événements de
l'utilisateur (les autres méthodes provoquerait une boucle infinie).
- Dans cette méthode on ne peut pas faire la distinction dans
  l'événement entre un ajout et une suppression.

Nous sommes donc obligé de reconstruire la liste à chaque fois,
heuresement la fonction existait déjà (utilisé pour la première
initialisation de la page).
  • Loading branch information
Anto59290 committed Mar 4, 2025
1 parent db7d665 commit c410251
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
3 changes: 2 additions & 1 deletion sv/static/sv/fichezone_form.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ function initializeChoices(elementId) {
}
let choices = new Choices(document.getElementById(elementId), options)
choices.passedElement.element.addEventListener('change', event=> {
pickedDetections.push(event.detail.value)
initializepickedDetections()
rebuildChoicesOptions()
})
allChoices.push(choices)
Expand All @@ -48,6 +48,7 @@ function initializeAllChoices() {
}

function initializepickedDetections() {
pickedDetections = []
allChoices.forEach((detectionChoice) =>{
detectionChoice.getValue().forEach((item) =>{
pickedDetections.push(item.value)
Expand Down
31 changes: 31 additions & 0 deletions sv/tests/test_fichezonedelimitee_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -408,3 +408,34 @@ def test_update_form_cant_have_same_detection_in_hors_zone_infestee_and_in_new_z
".fichezoneform__detections-hors-zone-infestee .choices__list--multiple",
str(other_detection.numero),
)


def test_update_form_remove_from_detections_list_refresh_choices_for_other_lists(
live_server,
page: Page,
choice_js_fill,
):
fiche_detection = FicheDetectionFactory()
fiche_zone_delimitee = FicheZoneFactory()
evenement = fiche_detection.evenement
evenement.fiche_zone_delimitee = fiche_zone_delimitee
evenement.save()

detection_1 = FicheDetectionFactory(
evenement=evenement, zone_infestee=ZoneInfesteeFactory(nom="Test 1", fiche_zone_delimitee=fiche_zone_delimitee)
)

FicheZoneDelimiteeFormPage(page, choice_js_fill)
page.goto(f"{live_server.url}{fiche_zone_delimitee.get_update_url()}")
page.locator(f"button[aria-label='Remove item: {detection_1.id}']").click(force=True)
choice_js_fill(
page,
".fichezoneform__detections-hors-zone-infestee .choices__input--cloned:first-of-type",
str(detection_1.numero),
str(detection_1.numero),
)
page.locator("#save-btn").click()

detection_1.refresh_from_db()
assert detection_1.zone_infestee is None
assert detection_1.hors_zone_infestee == fiche_zone_delimitee

0 comments on commit c410251

Please sign in to comment.