diff --git a/EditWorkout.tsx b/EditWorkout.tsx index 9276103..e71a196 100644 --- a/EditWorkout.tsx +++ b/EditWorkout.tsx @@ -12,7 +12,7 @@ import ConfirmDialog from './ConfirmDialog'; import {MARGIN, PADDING} from './constants'; import MassiveInput from './MassiveInput'; import {updatePlanWorkouts} from './plan.service'; -import {addSet, getSets, updateManySet, updateSetImage} from './set.service'; +import {addSet, getSet, updateManySet, updateSetImage} from './set.service'; import {WorkoutsPageParams} from './WorkoutsPage'; export default function EditWorkout() { @@ -34,20 +34,19 @@ export default function EditWorkout() { navigation.goBack()} /> ), headerRight: null, - title: params.value.name ? params.value.name : 'New workout', + title: params.value.name || 'New workout', }); - if (!params.value.name) return; - getSets({search: params.value.name, limit: 1, offset: 0}).then( - ([set]) => { - if (!set) return; - setUri(set.image); - setMinutes(set.minutes?.toString() ?? '3'); - setSeconds(set.seconds?.toString() ?? '30'); - setSets(set.sets?.toString() ?? '3'); - setSteps(set.steps ?? ''); - }, - ); - }, [navigation, params]), + if (!name) return; + getSet(name).then(set => { + console.log(`${EditWorkout.name}.focus`, {set, name}); + if (!set) return; + setUri(set.image); + setMinutes(set.minutes?.toString() ?? '3'); + setSeconds(set.seconds?.toString() ?? '30'); + setSets(set.sets?.toString() ?? '3'); + setSteps(set.steps ?? ''); + }); + }, [navigation, name, params.value.name]), ); const update = async () => { diff --git a/set.service.ts b/set.service.ts index 33fecb4..0c59043 100644 --- a/set.service.ts +++ b/set.service.ts @@ -61,6 +61,16 @@ interface PageParams { offset: number; } +export const getSet = async (name: string): Promise => { + const select = ` + SELECT * from sets + WHERE name = ? + LIMIT 1 + `; + const [result] = await db.executeSql(select, [name]); + return result.rows.item(0); +}; + export const getSets = async ({ search, limit,