From 61c84e2533da23835a1d1da075befc7b66336b6f Mon Sep 17 00:00:00 2001 From: Leon-ED Date: Tue, 25 Jun 2024 01:13:53 +0200 Subject: [PATCH] feat: Affichage uniquement des disruption de la ligne dans certains cas --- src/services/Wagon.ts | 42 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 40 insertions(+), 2 deletions(-) diff --git a/src/services/Wagon.ts b/src/services/Wagon.ts index 04b15d2..29e5d0a 100644 --- a/src/services/Wagon.ts +++ b/src/services/Wagon.ts @@ -78,7 +78,9 @@ export class Wagon { const json = await response.json() - const lineDto = json.data.lines.find((x: any) => x.number.toUpperCase() === lineNumber.toUpperCase()) + const lineDto = json.data.lines.find( + (x: any) => x.number.toUpperCase() === lineNumber.toUpperCase() + ) const line = this.lineFromDTO(lineDto) @@ -142,6 +144,33 @@ export class Wagon { return "info" } + /** + * + * @param disruptions - Liste des perturbations + * @param currentLineName - Nom de la ligne actuelle + * @returns boolean True si il y a une perturbation de type incident ou stoppedService sur la ligne actuelle + */ + private static shouldOnlyShowDisruptionForCurrentLine( + disruptions: SimpleDisruption[], + currentLineName: string + ): boolean { + const targetTypes = ["incident", "stoppedService"] + // Nombre de perturbations nécessaires pour afficher UNIQUEMENT les perturbations sur la ligne actuelle + const numberOfDisruptionsNeeded = 3 + // Retourne True si il y a une perturbation de type incident ou stoppedService sur la ligne actuelle + // Ou s'il y a plus de 3 perturbations sur la ligne actuelle + return ( + disruptions.some((disruption) => { + return ( + disruption.line.number === currentLineName && + targetTypes.includes(disruption.type) + ) + }) || + disruptions.filter((disruption) => { + return disruption.line.number === currentLineName + }).length >= numberOfDisruptionsNeeded + ) + } public static async getDisruptions( lat: number, @@ -187,7 +216,16 @@ export class Wagon { type: this.getDisruptionType(disruption.cause, disruption.effect), }) } - + const urlSearchParams = new URLSearchParams(window.location.search) + const currentLineName = urlSearchParams.get("for") + if ( + currentLineName && + this.shouldOnlyShowDisruptionForCurrentLine(disruptions, currentLineName) + ) { + return disruptions.filter( + (disruption) => disruption.line.number === currentLineName + ) + } return disruptions } }