Brandon Presley
ebe9a392ca
Running this code results in the following error: TypeError: subQueryBuilder.getParameters is not a function (it is undefined) at createFromAlias (http://localhost:8081/index.bundle//&platform=android&dev=t y=false&app=com.massive&modulesOnly=false&runModule=true:205802:59) at from (http://localhost:8081/index.bundle//&platform=android&dev=true&minify= =com.massive&modulesOnly=false&runModule=true:201576:45) at ?anon_0_ (http://localhost:8081/index.bundle//&platform=android&dev=true&min &app=com.massive&modulesOnly=false&runModule=true:316375:209) at next (native) at asyncGeneratorStep (http://localhost:8081/index.bundle//&platform=android&de nify=false&app=com.massive&modulesOnly=false&runModule=true:28380:26) at _next (http://localhost:8081/index.bundle//&platform=android&dev=true&minify p=com.massive&modulesOnly=false&runModule=true:28399:29) at anonymous (http://localhost:8081/index.bundle//&platform=android&dev=true&mi e&app=com.massive&modulesOnly=false&runModule=true:28404:14) at tryCallTwo (/root/react-native/packages/react-native/ReactAndroid/hermes-eng Release/4i495j47/arm64-v8a/lib/InternalBytecode/InternalBytecode.js:61:9) at doResolve (/root/react-native/packages/react-native/ReactAndroid/hermes-engi elease/4i495j47/arm64-v8a/lib/InternalBytecode/InternalBytecode.js:216:25) at Promise (/root/react-native/packages/react-native/ReactAndroid/hermes-engine ease/4i495j47/arm64-v8a/lib/InternalBytecode/InternalBytecode.js:82:14) at anonymous (http://localhost:8081/index.bundle//&platform=android&dev=true&mi e&app=com.massive&modulesOnly=false&runModule=true:28396:25) at anonymous (http://localhost:8081/index.bundle//&platform=android&dev=true&mi e&app=com.massive&modulesOnly=false&runModule=true:316420:14) at callback (http://localhost:8081/index.bundle//&platform=android&dev=true&min &app=com.massive&modulesOnly=false&runModule=true:145400:29) at anonymous (http://localhost:8081/index.bundle//&platform=android&dev=true&mi e&app=com.massive&modulesOnly=false&runModule=true:145419:27) at commitHookEffectListMount (http://localhost:8081/index.bundle//&platform=and true&minify=false&app=com.massive&modulesOnly=false&runModule=true:94929:38) at commitPassiveMountOnFiber (http://localhost:8081/index.bundle//&platform=and true&minify=false&app=com.massive&modulesOnly=false&runModule=true:96130:44) at commitPassiveMountEffects_complete (http://localhost:8081/index.bundle//&pla roid&dev=true&minify=false&app=com.massive&modulesOnly=false&runModule=true:96102:4 at commitPassiveMountEffects_begin (http://localhost:8081/index.bundle//&platfo d&dev=true&minify=false&app=com.massive&modulesOnly=false&runModule=true:96092:47) at commitPassiveMountEffects (http://localhost:8081/index.bundle//&platform=and true&minify=false&app=com.massive&modulesOnly=false&runModule=true:96082:40) at flushPassiveEffectsImpl (http://localhost:8081/index.bundle//&platform=andro ue&minify=false&app=com.massive&modulesOnly=false&runModule=true:97758:34) at flushPassiveEffects (http://localhost:8081/index.bundle//&platform=android&d inify=false&app=com.massive&modulesOnly=false&runModule=true:97713:43) at performSyncWorkOnRoot (http://localhost:8081/index.bundle//&platform=android &minify=false&app=com.massive&modulesOnly=false&runModule=true:97003:28) at flushSyncCallbacks (http://localhost:8081/index.bundle//&platform=android&de nify=false&app=com.massive&modulesOnly=false&runModule=true:86202:36) at flushSyncCallbacksOnlyInLegacyMode (http://localhost:8081/index.bundle//&pla
205 lines
6.5 KiB
TypeScript
205 lines
6.5 KiB
TypeScript
import {
|
|
NavigationProp,
|
|
RouteProp,
|
|
useFocusEffect,
|
|
useNavigation,
|
|
useRoute,
|
|
} from "@react-navigation/native";
|
|
import { useCallback, useMemo, useRef, useState } from "react";
|
|
import { FlatList, NativeModules, TextInput, View } from "react-native";
|
|
import { Button, IconButton, ProgressBar } from "react-native-paper";
|
|
import AppInput from "./AppInput";
|
|
import { getBestSet } from "./best.service";
|
|
import { PADDING } from "./constants";
|
|
import CountMany from "./count-many";
|
|
import { AppDataSource } from "./data-source";
|
|
import { getNow, setRepo, settingsRepo } from "./db";
|
|
import { fixNumeric } from "./fix-numeric";
|
|
import GymSet from "./gym-set";
|
|
import { PlanPageParams } from "./plan-page-params";
|
|
import Settings from "./settings";
|
|
import StackHeader from "./StackHeader";
|
|
import StartPlanItem from "./StartPlanItem";
|
|
import { toast } from "./toast";
|
|
|
|
export default function StartPlan() {
|
|
const { params } = useRoute<RouteProp<PlanPageParams, "StartPlan">>();
|
|
const [reps, setReps] = useState(params.first?.reps.toString() || "0");
|
|
const [weight, setWeight] = useState(params.first?.weight.toString() || "0");
|
|
const [unit, setUnit] = useState<string>(params.first?.unit || "kg");
|
|
const [selected, setSelected] = useState(0);
|
|
const [settings, setSettings] = useState<Settings>();
|
|
const [counts, setCounts] = useState<CountMany[]>();
|
|
const weightRef = useRef<TextInput>(null);
|
|
const repsRef = useRef<TextInput>(null);
|
|
const unitRef = useRef<TextInput>(null);
|
|
const workouts = useMemo(() => params.plan.workouts.split(","), [params]);
|
|
const navigation = useNavigation<NavigationProp<PlanPageParams>>();
|
|
|
|
const [selection, setSelection] = useState({
|
|
start: 0,
|
|
end: 0,
|
|
});
|
|
|
|
const refresh = useCallback(async () => {
|
|
const questions = workouts
|
|
.map((workout, index) => `('${workout}',${index})`)
|
|
.join(",");
|
|
const newCounts = await AppDataSource.manager
|
|
.createQueryBuilder()
|
|
.select("workouts.name")
|
|
.addSelect("COUNT(sets.id)", "total")
|
|
.addSelect("sets.sets")
|
|
.from((qb) => {
|
|
const subQuery = qb
|
|
.subQuery()
|
|
.select("0", "name")
|
|
.addSelect("0", "sequence")
|
|
.from("workouts", "workouts")
|
|
.getQuery();
|
|
return `(${subQuery} UNION ALL values ${questions})`;
|
|
}, "workouts")
|
|
.leftJoin(
|
|
"sets",
|
|
"sets",
|
|
"sets.name = workouts.name AND sets.created LIKE STRFTIME('%Y-%m-%d%%', 'now', 'localtime') AND NOT sets.hidden"
|
|
)
|
|
.groupBy("workouts.name")
|
|
.orderBy("workouts.sequence")
|
|
.limit(-1)
|
|
.offset(1)
|
|
.getRawMany();
|
|
console.log(`${StartPlan.name}.focus:`, { newCounts });
|
|
setCounts(newCounts);
|
|
}, [workouts]);
|
|
|
|
const select = useCallback(
|
|
async (index: number, newCounts?: CountMany[]) => {
|
|
setSelected(index);
|
|
if (!counts && !newCounts) return;
|
|
const workout = counts ? counts[index] : newCounts[index];
|
|
console.log(`${StartPlan.name}.next:`, { workout });
|
|
const last = await setRepo.findOne({
|
|
where: { name: workout.name },
|
|
order: { created: "desc" },
|
|
});
|
|
console.log({ last });
|
|
if (!last) return;
|
|
delete last.id;
|
|
console.log(`${StartPlan.name}.select:`, { last });
|
|
setReps(last.reps.toString());
|
|
setWeight(last.weight.toString());
|
|
setUnit(last.unit);
|
|
},
|
|
[counts]
|
|
);
|
|
|
|
useFocusEffect(
|
|
useCallback(() => {
|
|
settingsRepo.findOne({ where: {} }).then(setSettings);
|
|
refresh();
|
|
}, [refresh])
|
|
);
|
|
|
|
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,
|
|
reps: +reps,
|
|
unit,
|
|
created: now,
|
|
hidden: false,
|
|
};
|
|
await setRepo.save(newSet);
|
|
await refresh();
|
|
if (
|
|
settings.notify &&
|
|
(+weight > best.weight || (+reps > best.reps && +weight === best.weight))
|
|
) {
|
|
toast("Great work King! That's a new record.");
|
|
}
|
|
if (!settings.alarm) return;
|
|
const milliseconds =
|
|
Number(best.minutes) * 60 * 1000 + Number(best.seconds) * 1000;
|
|
NativeModules.AlarmModule.timer(milliseconds);
|
|
};
|
|
|
|
return (
|
|
<>
|
|
<StackHeader title={params.plan.days.replace(/,/g, ", ")}>
|
|
<IconButton
|
|
onPress={() => navigation.navigate("EditPlan", { plan: params.plan })}
|
|
icon="edit"
|
|
/>
|
|
</StackHeader>
|
|
<View style={{ padding: PADDING, flex: 1, flexDirection: "column" }}>
|
|
<View style={{ flex: 1 }}>
|
|
<AppInput
|
|
label="Reps"
|
|
keyboardType="numeric"
|
|
value={reps}
|
|
onChangeText={(newReps) => {
|
|
const fixed = fixNumeric(newReps);
|
|
setReps(fixed);
|
|
if (fixed.length !== newReps.length)
|
|
toast("Reps must be a number");
|
|
}}
|
|
onSubmitEditing={() => weightRef.current?.focus()}
|
|
selection={selection}
|
|
onSelectionChange={(e) => setSelection(e.nativeEvent.selection)}
|
|
innerRef={repsRef}
|
|
/>
|
|
<AppInput
|
|
label="Weight"
|
|
keyboardType="numeric"
|
|
value={weight}
|
|
onChangeText={(newWeight) => {
|
|
const fixed = fixNumeric(newWeight);
|
|
setWeight(fixed);
|
|
if (fixed.length !== newWeight.length)
|
|
toast("Weight must be a number");
|
|
}}
|
|
onSubmitEditing={handleSubmit}
|
|
innerRef={weightRef}
|
|
blurOnSubmit
|
|
/>
|
|
{settings?.showUnit && (
|
|
<AppInput
|
|
autoCapitalize="none"
|
|
label="Unit"
|
|
value={unit}
|
|
onChangeText={setUnit}
|
|
innerRef={unitRef}
|
|
/>
|
|
)}
|
|
{counts && (
|
|
<FlatList
|
|
data={counts}
|
|
renderItem={(props) => (
|
|
<View>
|
|
<StartPlanItem
|
|
{...props}
|
|
onUndo={refresh}
|
|
onSelect={select}
|
|
selected={selected}
|
|
/>
|
|
<ProgressBar
|
|
progress={(props.item.total || 0) / (props.item.sets || 3)}
|
|
/>
|
|
</View>
|
|
)}
|
|
/>
|
|
)}
|
|
</View>
|
|
<Button mode="outlined" icon="save" onPress={handleSubmit}>
|
|
Save
|
|
</Button>
|
|
</View>
|
|
</>
|
|
);
|
|
}
|