From 3714db438ee0bedf83d0cb2b1b10e4f5c7d63530 Mon Sep 17 00:00:00 2001 From: Brandon Presley Date: Sun, 16 Oct 2022 16:46:38 +1300 Subject: [PATCH] Fix max number of sets for plan Previously we were trying to get the max # of sets from our query on the number of sets completed for today. This meant if we hadn't completed any sets today, we would get no result for that workout. --- StartPlan.tsx | 21 ++++++++++++++++----- count-many.ts | 1 - set.service.ts | 2 +- 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/StartPlan.tsx b/StartPlan.tsx index 706929e..9189e18 100644 --- a/StartPlan.tsx +++ b/StartPlan.tsx @@ -16,7 +16,7 @@ import MassiveInput from './MassiveInput'; import {useSnackbar} from './MassiveSnack'; import {PlanPageParams} from './plan-page-params'; import Set from './set'; -import {addSet, countManyToday} from './set.service'; +import {addSet, countManyToday, getDistinctSets} from './set.service'; import SetForm from './SetForm'; import {useSettings} from './use-settings'; @@ -34,6 +34,7 @@ export default function StartPlan() { const [selected, setSelected] = useState(0); const {settings} = useSettings(); const [counts, setCounts] = useState(); + const [distinctSets, setDistinctSets] = useState(); const weightRef = useRef(null); const repsRef = useRef(null); const unitRef = useRef(null); @@ -55,7 +56,16 @@ export default function StartPlan() { headerRight: null, title: params.plan.days.replace(/,/g, ', '), }); - countManyToday().then(setCounts); + countManyToday().then(newCounts => { + setCounts(newCounts); + console.log(`${StartPlan.name}.focus:`, {newCounts}); + }); + getDistinctSets({limit: 100, offset: 0, search: '%'}).then( + newDistinct => { + setDistinctSets(newDistinct); + console.log(`${StartPlan.name}.focus:`, {newDistinct}); + }, + ); }, [navigation, params]), ); @@ -115,10 +125,11 @@ export default function StartPlan() { const getDescription = useCallback( (countName: string) => { const count = counts?.find(c => c.name === countName); - if (!count) return; - return `${count.total} / ${count.sets}`; + if (!distinctSets) return; + const distinct = distinctSets.find(d => d.name === countName); + return `${count?.total || 0} / ${distinct?.sets}`; }, - [counts], + [counts, distinctSets], ); return ( diff --git a/count-many.ts b/count-many.ts index 7d10775..d97c39a 100644 --- a/count-many.ts +++ b/count-many.ts @@ -1,5 +1,4 @@ export default interface CountMany { name: string; total: number; - sets: number; } diff --git a/set.service.ts b/set.service.ts index c3d80dc..2114e08 100644 --- a/set.service.ts +++ b/set.service.ts @@ -157,7 +157,7 @@ export const countToday = async (name: string): Promise => { export const countManyToday = async (): Promise => { const select = ` - SELECT COUNT(*) as total, name, sets FROM sets + SELECT COUNT(*) as total, name FROM sets WHERE created LIKE strftime('%Y-%m-%d%%', 'now', 'localtime') AND NOT hidden GROUP BY name