Skip to content

Commit

Permalink
Merge pull request #53 from concord-consortium/186956759-update-filte…
Browse files Browse the repository at this point in the history
…r-units

Convert selected filter values when units change. (PT-186956759)
  • Loading branch information
lublagg authored Feb 2, 2024
2 parents 530baca + 37168db commit 56e62a0
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/components/attribute-selector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand All @@ -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;
});
};

Expand Down

0 comments on commit 56e62a0

Please sign in to comment.