From 8894ffdd494369dd85007faa063eaebece5f751e Mon Sep 17 00:00:00 2001 From: Johan Ohly Date: Fri, 13 Sep 2024 19:19:17 +0200 Subject: [PATCH] fix: statistics not updating when new flights are added --- .../modals/statistics/StatisticsModal.svelte | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/lib/components/modals/statistics/StatisticsModal.svelte b/src/lib/components/modals/statistics/StatisticsModal.svelte index 80b8604..f677f5c 100644 --- a/src/lib/components/modals/statistics/StatisticsModal.svelte +++ b/src/lib/components/modals/statistics/StatisticsModal.svelte @@ -20,13 +20,16 @@ flights: FlightData[]; } = $props(); - let flightCount = flights.length; - let totalDistance = flights.reduce((acc, curr) => (acc += curr.distance), 0); - let totalDuration = flights.reduce( - (acc, curr) => (acc += curr.duration ?? 0), - 0, + let flightCount = $derived.by(() => flights.length); + let totalDistance = $derived.by(() => + flights.reduce((acc, curr) => (acc += curr.distance ?? 0), 0), + ); + let totalDuration = $derived.by(() => + flights.reduce((acc, curr) => (acc += curr.duration ?? 0), 0), + ); + let airports = $derived.by( + () => new Set(flights.flatMap((f) => [f.from.name, f.to.name])).size, ); - let airports = new Set(flights.flatMap((f) => [f.from.name, f.to.name])).size;