From 9dd4e70d336585adda11ea453f70383fdd51364e Mon Sep 17 00:00:00 2001 From: Brandon Presley Date: Wed, 7 Feb 2024 12:02:36 +1300 Subject: [PATCH] Add selection for unit to convert to in graphs --- ViewGraph.tsx | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/ViewGraph.tsx b/ViewGraph.tsx index 0e87f82..b6201ea 100644 --- a/ViewGraph.tsx +++ b/ViewGraph.tsx @@ -23,6 +23,13 @@ export default function ViewGraph() { const [volumes, setVolumes] = useState(); const [metric, setMetric] = useState(Metrics.OneRepMax); const [period, setPeriod] = useState(Periods.Monthly); + const [unit, setUnit] = useState('kg'); + + const convertWeight = (weight: number, unitFrom: string, unitTo: string) => { + if (unitFrom === unitTo) return weight; + if (unitFrom === 'lb' && unitTo === 'kg') return weight * 0.453592; + if (unitFrom === 'kg' && unitTo === 'lb') return weight * 2.20462; + }; useEffect(() => { let difference = "-7 days"; @@ -57,12 +64,14 @@ export default function ViewGraph() { builder .addSelect("ROUND(MAX(weight), 2)", "weight") .getRawMany() + .then(newWeights => newWeights.map(set => ({ ...set, weight: convertWeight(set.weight, set.unit, unit) }))) .then(setWeights); break; case Metrics.Volume: builder .addSelect("ROUND(SUM(weight * reps), 2)", "value") .getRawMany() + .then(newWeights => newWeights.map(set => ({ ...set, value: convertWeight(set.value, set.unit, unit) }))) .then(setVolumes); break; default: @@ -73,12 +82,13 @@ export default function ViewGraph() { "weight" ) .getRawMany() + .then(newWeights => newWeights.map(set => ({ ...set, weight: convertWeight(set.weight, set.unit, unit) }))) .then((newWeights) => { console.log({ weights: newWeights }); setWeights(newWeights); }); } - }, [params.name, metric, period]); + }, [params.name, metric, period, unit]); const weightChart = useMemo(() => { if (weights === undefined) return null; @@ -151,6 +161,17 @@ export default function ViewGraph() { onChange={(value) => setPeriod(value as Periods)} value={period} /> + +