From 37168dbe99672cad788b1b88f0d3b82dd1551e3f Mon Sep 17 00:00:00 2001 From: lublagg Date: Thu, 1 Feb 2024 11:59:46 -0500 Subject: [PATCH] Convert selected filter values when units change. --- src/components/attribute-selector.tsx | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/components/attribute-selector.tsx b/src/components/attribute-selector.tsx index 7035602..207eebf 100644 --- a/src/components/attribute-selector.tsx +++ b/src/components/attribute-selector.tsx @@ -2,6 +2,7 @@ import React, { useEffect, useState } from "react"; import classnames from "classnames"; import { useStateContext } from "../hooks/use-state"; import { dailyMonthlyAttrMap, hourlyAttrMap } from "../types"; +import { dataTypeStore } from "../utils/noaaDataTypes"; import "./attribute-selector.scss"; @@ -24,8 +25,34 @@ export const AttributesSelector = () => { }, [attributeList.length, frequencies, selectedFrequency]); const handleUnitsClicked = () => { + const newUnits = units === "standard" ? "metric" : "standard"; + const newSelectedAttrFilters = frequencies[selectedFrequency].filters.map((filter) => { + const { attribute, operator } = filter; + const dataType = dataTypeStore.findByName(attribute); + if (dataType && dataType.convertUnits) { + const fromUnits = dataType.units[units]; + const toUnits = dataType.units[newUnits]; + if (operator === "between") { + const lowerValue = Math.round(dataType.convertUnits(fromUnits, toUnits, filter.lowerValue.toString())); + const upperValue = Math.round(dataType.convertUnits(fromUnits, toUnits, filter.upperValue.toString())); + return { + ...filter, + lowerValue, + upperValue + }; + } else if (operator !== "top" && operator !== "bottom" && operator !== "aboveMean" && operator !== "belowMean" && operator !== "all") { + const value = Math.round(dataType.convertUnits(fromUnits, toUnits, filter.value.toString())); + return { + ...filter, + value + }; + } + } + return filter; + }); setState(draft => { draft.units = draft.units === "standard" ? "metric" : "standard"; + draft.frequencies[selectedFrequency].filters = newSelectedAttrFilters; }); };