From 1273b6a6d82ee54a1871fcbf382b03f3337be10d Mon Sep 17 00:00:00 2001 From: Brandon Presley Date: Sat, 17 Dec 2022 16:44:11 +1300 Subject: [PATCH] Reduce state in StartPlan Fixes several issues related to old data. --- StartPlan.tsx | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/StartPlan.tsx b/StartPlan.tsx index fc83330..25aa2bb 100644 --- a/StartPlan.tsx +++ b/StartPlan.tsx @@ -8,7 +8,7 @@ import {PADDING} from './constants' import CountMany from './count-many' import {AppDataSource} from './data-source' import {getNow, setRepo, settingsRepo} from './db' -import GymSet, {defaultSet} from './gym-set' +import GymSet from './gym-set' import MassiveInput from './MassiveInput' import {PlanPageParams} from './plan-page-params' import Settings from './settings' @@ -21,7 +21,6 @@ export default function StartPlan() { const [reps, setReps] = useState(params.first?.reps.toString() || '0') const [weight, setWeight] = useState(params.first?.weight.toString() || '0') const [unit, setUnit] = useState(params.first?.unit || 'kg') - const [best, setBest] = useState(params.first || defaultSet) const [selected, setSelected] = useState(0) const [settings, setSettings] = useState() const [counts, setCounts] = useState() @@ -59,24 +58,18 @@ export default function StartPlan() { const select = useCallback( async (index: number, newCounts?: CountMany[]) => { setSelected(index) - console.log(`${StartPlan.name}.next:`, {best, index}) if (!counts && !newCounts) return const workout = counts ? counts[index] : newCounts[index] console.log(`${StartPlan.name}.next:`, {workout}) const newBest = await getBestSet(workout.name) - if (!newBest) - return setBest({ - ...best, - name: workout.name, - }) + if (!newBest) return delete newBest.id console.log(`${StartPlan.name}.next:`, {newBest}) setReps(newBest.reps.toString()) setWeight(newBest.weight.toString()) setUnit(newBest.unit) - setBest(newBest) }, - [counts, best], + [counts], ) useFocusEffect( @@ -88,6 +81,9 @@ export default function StartPlan() { const handleSubmit = async () => { const [{now}] = await getNow() + const workout = counts[selected] + const best = await getBestSet(workout.name) + delete best.id const newSet: GymSet = { ...best, weight: +weight,