feat: 完善训练
This commit is contained in:
@@ -48,12 +48,43 @@ export default function SelectExerciseForScheduleScreen() {
|
||||
const planId = params.planId;
|
||||
const sessionId = params.sessionId;
|
||||
const plan = useMemo(() => plans.find(p => p.id === planId), [plans, planId]);
|
||||
const session = useMemo(() => sessionId ? currentSession : null, [sessionId, currentSession]);
|
||||
|
||||
// 会话状态管理
|
||||
const [session, setSession] = useState<any>(null);
|
||||
const [sessionLoading, setSessionLoading] = useState(false);
|
||||
|
||||
// 根据是否有sessionId来确定是训练计划模式还是训练会话模式
|
||||
const isSessionMode = !!sessionId;
|
||||
|
||||
// 加载会话详情(如果是会话模式)
|
||||
useEffect(() => {
|
||||
if (sessionId && !session) {
|
||||
const loadSession = async () => {
|
||||
try {
|
||||
setSessionLoading(true);
|
||||
// 首先尝试使用 currentSession(如果 sessionId 匹配)
|
||||
if (currentSession?.id === sessionId) {
|
||||
setSession(currentSession);
|
||||
} else {
|
||||
// 否则从 API 获取会话详情
|
||||
const { workoutsApi } = await import('@/services/workoutsApi');
|
||||
const sessionDetail = await workoutsApi.getSessionDetail(sessionId);
|
||||
setSession(sessionDetail);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('加载会话详情失败:', error);
|
||||
} finally {
|
||||
setSessionLoading(false);
|
||||
}
|
||||
};
|
||||
loadSession();
|
||||
}
|
||||
}, [sessionId, currentSession, session]);
|
||||
|
||||
const targetGoal = plan?.goal || session?.trainingPlan?.goal;
|
||||
const goalConfig = targetGoal ? (GOAL_TEXT[targetGoal] || { title: isSessionMode ? '添加动作' : '训练计划', color: palette.primary, description: isSessionMode ? '选择要添加的动作' : '开始你的训练之旅' }) : null;
|
||||
const goalConfig = targetGoal
|
||||
? (GOAL_TEXT[targetGoal] || { title: isSessionMode ? '添加动作' : '训练计划', color: palette.primary, description: isSessionMode ? '选择要添加的动作' : '开始你的训练之旅' })
|
||||
: { title: isSessionMode ? '添加动作' : '训练计划', color: palette.primary, description: isSessionMode ? '选择要添加的动作' : '开始你的训练之旅' };
|
||||
|
||||
const [keyword, setKeyword] = useState('');
|
||||
const [category, setCategory] = useState<string>('全部');
|
||||
@@ -139,7 +170,7 @@ export default function SelectExerciseForScheduleScreen() {
|
||||
if (isSessionMode && sessionId) {
|
||||
// 训练会话模式:添加到训练会话
|
||||
await dispatch(addWorkoutExercise({ sessionId, dto: newExerciseDto })).unwrap();
|
||||
} else if (plan) {
|
||||
} else if (plan && planId) {
|
||||
// 训练计划模式:添加到训练计划
|
||||
const planExerciseDto = {
|
||||
exerciseKey: selected.key,
|
||||
@@ -149,7 +180,7 @@ export default function SelectExerciseForScheduleScreen() {
|
||||
itemType: 'exercise' as const,
|
||||
note: `${selected.category}训练`,
|
||||
};
|
||||
await dispatch(addExercise({ planId: plan.id, dto: planExerciseDto })).unwrap();
|
||||
await dispatch(addExercise({ planId: planId, dto: planExerciseDto })).unwrap();
|
||||
} else {
|
||||
throw new Error('缺少必要的参数');
|
||||
}
|
||||
@@ -191,9 +222,9 @@ export default function SelectExerciseForScheduleScreen() {
|
||||
if (isSessionMode && sessionId) {
|
||||
// 训练会话模式
|
||||
await dispatch(addWorkoutExercise({ sessionId, dto: restDto })).unwrap();
|
||||
} else if (plan) {
|
||||
} else if (plan && planId) {
|
||||
// 训练计划模式
|
||||
await dispatch(addExercise({ planId: plan.id, dto: restDto })).unwrap();
|
||||
await dispatch(addExercise({ planId: planId, dto: restDto })).unwrap();
|
||||
} else {
|
||||
throw new Error('缺少必要的参数');
|
||||
}
|
||||
@@ -224,9 +255,9 @@ export default function SelectExerciseForScheduleScreen() {
|
||||
if (isSessionMode && sessionId) {
|
||||
// 训练会话模式
|
||||
await dispatch(addWorkoutExercise({ sessionId, dto: noteDto })).unwrap();
|
||||
} else if (plan) {
|
||||
} else if (plan && planId) {
|
||||
// 训练计划模式
|
||||
await dispatch(addExercise({ planId: plan.id, dto: noteDto })).unwrap();
|
||||
await dispatch(addExercise({ planId: planId, dto: noteDto })).unwrap();
|
||||
} else {
|
||||
throw new Error('缺少必要的参数');
|
||||
}
|
||||
@@ -256,14 +287,36 @@ export default function SelectExerciseForScheduleScreen() {
|
||||
setSelectedKey(key);
|
||||
};
|
||||
|
||||
if (!goalConfig || (!plan && !isSessionMode)) {
|
||||
// 加载状态
|
||||
if (sessionLoading) {
|
||||
return (
|
||||
<SafeAreaView style={styles.safeArea}>
|
||||
<HeaderBar title="选择动作" onBack={() => router.back()} />
|
||||
<View style={styles.errorContainer}>
|
||||
<ThemedText style={styles.errorText}>
|
||||
{isSessionMode ? '找不到指定的训练会话' : '找不到指定的训练计划'}
|
||||
</ThemedText>
|
||||
<ThemedText style={styles.errorText}>加载中...</ThemedText>
|
||||
</View>
|
||||
</SafeAreaView>
|
||||
);
|
||||
}
|
||||
|
||||
// 错误状态检查
|
||||
if (isSessionMode && !session) {
|
||||
return (
|
||||
<SafeAreaView style={styles.safeArea}>
|
||||
<HeaderBar title="选择动作" onBack={() => router.back()} />
|
||||
<View style={styles.errorContainer}>
|
||||
<ThemedText style={styles.errorText}>找不到指定的训练会话</ThemedText>
|
||||
</View>
|
||||
</SafeAreaView>
|
||||
);
|
||||
}
|
||||
|
||||
if (!isSessionMode && !plan) {
|
||||
return (
|
||||
<SafeAreaView style={styles.safeArea}>
|
||||
<HeaderBar title="选择动作" onBack={() => router.back()} />
|
||||
<View style={styles.errorContainer}>
|
||||
<ThemedText style={styles.errorText}>找不到指定的训练计划</ThemedText>
|
||||
</View>
|
||||
</SafeAreaView>
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user