From 8972b51ba4abd879271e1dcd2f76ff044f5cfe94 Mon Sep 17 00:00:00 2001 From: Abby Wheelis Date: Mon, 28 Aug 2023 16:03:20 -0600 Subject: [PATCH] add percent change to carbon card if there is a previous week, we use current and previous to calculate a percent change over the two and display it to the user --- www/js/metrics/CarbonFootprintCard.tsx | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/www/js/metrics/CarbonFootprintCard.tsx b/www/js/metrics/CarbonFootprintCard.tsx index 424193042..1ff14f2ad 100644 --- a/www/js/metrics/CarbonFootprintCard.tsx +++ b/www/js/metrics/CarbonFootprintCard.tsx @@ -126,6 +126,14 @@ const CarbonFootprintCard = ({ userMetrics, aggMetrics }: Props) => { return summaryMap; } + const calculatePercentChange = function(pastWeekRange, previousWeekRange) { + let greaterLesserPct = { + low: (pastWeekRange.low/previousWeekRange.low) * 100 - 100, + high: (pastWeekRange.high/previousWeekRange.high) * 100 - 100, + } + return greaterLesserPct; + } + const createOrCollapseRange = function(low, high) { let range = []; if(high == low) { @@ -179,6 +187,10 @@ const CarbonFootprintCard = ({ userMetrics, aggMetrics }: Props) => { valueArray = createOrCollapseRange(userPrevWeek.low, userPrevWeek.high); value = valueArray[1] ? valueArray[0] + '-' + valueArray[1] : valueArray[0]; tempUserCarbon.push({label: "previous week", value: value}); + + let pctChange = calculatePercentChange(userPastWeek, userPrevWeek); + let changeRange = createOrCollapseRange(pctChange.low, pctChange.high); + setEmissionsChange(changeRange[1] ? changeRange[0] + '-' + changeRange[1] : changeRange[0]); } //calculate worst-case carbon footprint @@ -196,6 +208,14 @@ const CarbonFootprintCard = ({ userMetrics, aggMetrics }: Props) => { } }, [userMetrics?.distance]) + let changeSection; + if(emissionsChange) { + changeSection = + {"change in emissions"} + {`${formatForDisplay(emissionsChange)} ${t("% this week")}`} + ; + } + return (