diff --git a/app/src/components/calculators/WaterCalculator.js b/app/src/components/calculators/WaterCalculator.js index e63d2146..61b16e60 100644 --- a/app/src/components/calculators/WaterCalculator.js +++ b/app/src/components/calculators/WaterCalculator.js @@ -16,12 +16,6 @@ import {Media} from "../../contexts/contexts"; // result comes back in those same units. The React component picks imperial vs metric and // supplies the matching per-category rates and container capacities. -// Divide `a` by `b`, returning null when the result would not be a finite number. -const safeDiv = (a, b) => { - const result = a / b; - return Number.isFinite(result) ? result : null; -}; - // Total water the household needs per day: sum of (count * rate) for each category. // Missing/blank/non-positive counts or rates contribute nothing rather than NaN. export function dailyDemand(counts, rates) { @@ -151,7 +145,8 @@ const DEMAND_INFO = 'Per-person daily water includes drinking plus basic hygiene + 'adequate-intake figures (men ~3.7 L, women ~2.7 L of total water per day). The ' + 'Pregnant/Nursing category adds the extra CDC/Ready.gov calls out — about +0.5 gallon ' + 'for pregnant and +1 gallon for nursing women. CDC also advises storing more for sick ' - + 'people, pets, and hot climates — edit the rates to match your household.'; + + 'people, pets, and hot climates. Use the "Additional daily demand" field below for pets, ' + + 'livestock, or other needs not covered by the per-person categories.'; // Per-category colors (Paul Tol bright palette) so the count inputs read at a glance. const CATEGORY_META = [ @@ -178,13 +173,25 @@ function fmt(value, unit) { return unit ? `${roundDigits(value, 2)} ${unit}` : `${roundDigits(value, 2)}`; } +// Keep a numeric input non-negative without disrupting in-progress decimal entry. +// A can only become negative via a leading "-", so stripping it is +// enough; returning the raw string otherwise lets the user finish typing "1.5". +function clampNonNegative(value) { + if (value === '') { + return ''; + } + return value.startsWith('-') ? value.slice(1) : value; +} + export function WaterCalculator() { const [metric, setMetric] = useLocalStorage('water_calculator_metric', false); const [preset, setPreset] = useLocalStorage('water_calculator_preset', 'minimum'); const [counts, setCounts] = useLocalStorage('water_calculator_counts', {men: '1', women: '1', children: '', pregnant: ''}); + const [extra, setExtra] = useLocalStorage('water_calculator_extra', ''); const [rates, setRates] = React.useState({...WATER_PRESETS.minimum.imperial}); - const [durationIndex, setDurationIndex] = React.useState(DURATION_STOPS.indexOf(14)); + const [durationIndex, setDurationIndex] = useLocalStorage('water_calculator_duration_index', + DURATION_STOPS.indexOf(14)); // Reset the editable rates to the preset's defaults whenever the preset or unit changes. React.useEffect(() => { @@ -195,9 +202,13 @@ export function WaterCalculator() { const volumeUnit = metric ? 'L' : 'gal'; const rateUnit = metric ? 'L/day' : 'gal/day'; const weightUnit = metric ? 'kg' : 'lb'; - const days = DURATION_STOPS[durationIndex]; + // A persisted index could fall out of range if DURATION_STOPS ever changes; clamp it. + const durationStop = Math.min(Math.max(0, durationIndex), DURATION_STOPS.length - 1); + const days = DURATION_STOPS[durationStop]; - const daily = dailyDemand(counts, rates); + const baseDaily = dailyDemand(counts, rates); + const extraDaily = Number(extra) > 0 ? Number(extra) : 0; + const daily = baseDaily + extraDaily; const total = totalWater(daily, days); const weight = waterWeight(total, metric); @@ -215,13 +226,34 @@ export function WaterCalculator() { const rateInput = ({key, label, color}) => setRates({...rates, [key]: e.target.value})} + onChange={e => setRates({...rates, [key]: clampNonNegative(e.target.value)})} label={}/>; - const presetButtons = setPreset(preset === 'comfortable' ? 'minimum' : 'comfortable')}/>; + const presetButtons = ( +
+ {Object.entries(WATER_PRESETS).map(([key, p]) => ( + + ))} + + (switching resets rates to preset defaults) + +
+ ); // One row per container: how many of it the total water would fill, and the spare // capacity in the last one. Shown for every container so the user can compare. @@ -265,10 +297,21 @@ export function WaterCalculator() { {rateInput(meta)})} +
Additional daily demand
+
+ setExtra(clampNonNegative(e.target.value))} + label={}/> +

+ Pets, livestock, guests, sick household members, or anything else not covered above. +

+
+
How long?
setDurationIndex(Number(e.target.value))} style={{width: '100%'}}/>
@@ -288,7 +331,7 @@ export function WaterCalculator() { {fmt(total, volumeUnit)} - Total Weight + Water Weight {fmt(weight, weightUnit)}