feat: 完善训练

This commit is contained in:
2025-08-16 14:15:11 +08:00
parent 5a4d86ff7d
commit 4c6a0e0399
17 changed files with 3079 additions and 166 deletions

View File

@@ -2,6 +2,7 @@ import {
workoutsApi,
type AddWorkoutExerciseDto,
type CompleteWorkoutExerciseDto,
type CreateWorkoutSessionDto,
type StartWorkoutDto,
type StartWorkoutExerciseDto,
type UpdateWorkoutExerciseDto,
@@ -159,10 +160,10 @@ export const loadWorkoutStats = createAsyncThunk(
// 获取训练会话列表
export const loadWorkoutSessions = createAsyncThunk(
'workout/loadSessions',
async ({ page = 1, limit = 10 }: { page?: number; limit?: number } = {}, { rejectWithValue }) => {
async ({ page = 1, limit = 10, append = false }: { page?: number; limit?: number; append?: boolean } = {}, { rejectWithValue }) => {
try {
const result = await workoutsApi.getSessions(page, limit);
return result;
return { ...result, append };
} catch (error: any) {
return rejectWithValue(error.message || '获取训练列表失败');
}
@@ -182,6 +183,20 @@ export const addWorkoutExercise = createAsyncThunk(
}
);
// 创建训练会话
export const createWorkoutSession = createAsyncThunk(
'workout/createSession',
async (dto: CreateWorkoutSessionDto, { rejectWithValue }) => {
try {
console.log('createWorkoutSession', dto);
const session = await workoutsApi.createSession(dto);
return session;
} catch (error: any) {
return rejectWithValue(error.message || '创建训练会话失败');
}
}
);
// 删除训练会话
export const deleteWorkoutSession = createAsyncThunk(
'workout/deleteSession',
@@ -343,7 +358,13 @@ const workoutSlice = createSlice({
})
.addCase(loadWorkoutSessions.fulfilled, (state, action) => {
state.loading = false;
state.sessions = action.payload.sessions;
if (action.payload.append) {
// 追加数据(分页加载更多)
state.sessions = [...state.sessions, ...action.payload.sessions];
} else {
// 替换数据(刷新或首次加载)
state.sessions = action.payload.sessions;
}
state.sessionsPagination = action.payload.pagination;
})
.addCase(loadWorkoutSessions.rejected, (state, action) => {
@@ -366,6 +387,24 @@ const workoutSlice = createSlice({
state.error = action.payload as string;
})
// createWorkoutSession
.addCase(createWorkoutSession.pending, (state) => {
state.loading = true;
state.error = null;
})
.addCase(createWorkoutSession.fulfilled, (state, action) => {
state.loading = false;
// 将新创建的会话添加到列表开头
state.sessions.unshift(action.payload);
// 设置为当前会话
state.currentSession = action.payload;
state.exercises = action.payload.exercises || [];
})
.addCase(createWorkoutSession.rejected, (state, action) => {
state.loading = false;
state.error = action.payload as string;
})
// deleteWorkoutSession
.addCase(deleteWorkoutSession.pending, (state) => {
state.loading = true;