- 在 GoalsListScreen 中新增目标编辑功能,支持用户编辑现有目标 - 更新 CreateGoalModal 组件,支持编辑模式下的目标更新 - 在 NutritionRecordsScreen 中新增删除营养记录功能,允许用户删除不需要的记录 - 更新 NutritionRecordCard 组件,增加操作选项,支持删除记录 - 修改 dietRecords 服务,添加删除营养记录的 API 调用 - 优化 goalsSlice,确保目标更新逻辑与 Redux 状态管理一致
123 lines
3.1 KiB
TypeScript
123 lines
3.1 KiB
TypeScript
import {
|
|
ApiResponse,
|
|
BatchGoalOperationRequest,
|
|
BatchGoalOperationResult,
|
|
CompleteGoalRequest,
|
|
CreateGoalRequest,
|
|
GetGoalCompletionsQuery,
|
|
GetGoalsQuery,
|
|
Goal,
|
|
GoalCompletion,
|
|
GoalDetailResponse,
|
|
GoalListItem,
|
|
GoalStats,
|
|
PaginatedResponse,
|
|
UpdateGoalRequest,
|
|
} from '@/types/goals';
|
|
import { api } from './api';
|
|
|
|
// 目标管理API服务
|
|
|
|
/**
|
|
* 创建目标
|
|
*/
|
|
export const createGoal = async (goalData: CreateGoalRequest): Promise<Goal> => {
|
|
return api.post<Goal>('/goals', goalData);
|
|
};
|
|
|
|
/**
|
|
* 获取目标列表
|
|
*/
|
|
export const getGoals = async (query: GetGoalsQuery = {}): Promise<PaginatedResponse<GoalListItem>> => {
|
|
const searchParams = new URLSearchParams();
|
|
|
|
Object.entries(query).forEach(([key, value]) => {
|
|
if (value !== undefined && value !== null) {
|
|
searchParams.append(key, String(value));
|
|
}
|
|
});
|
|
|
|
const queryString = searchParams.toString();
|
|
const path = queryString ? `/goals?${queryString}` : '/goals';
|
|
|
|
return api.get<PaginatedResponse<GoalListItem>>(path);
|
|
};
|
|
|
|
/**
|
|
* 获取目标详情
|
|
*/
|
|
export const getGoalById = async (goalId: string): Promise<ApiResponse<GoalDetailResponse>> => {
|
|
return api.get<ApiResponse<GoalDetailResponse>>(`/goals/${goalId}`);
|
|
};
|
|
|
|
/**
|
|
* 更新目标
|
|
*/
|
|
export const updateGoal = async (goalId: string, goalData: UpdateGoalRequest): Promise<Goal> => {
|
|
return api.put<Goal>(`/goals/${goalId}`, goalData);
|
|
};
|
|
|
|
/**
|
|
* 删除目标
|
|
*/
|
|
export const deleteGoal = async (goalId: string): Promise<ApiResponse<boolean>> => {
|
|
return api.delete<ApiResponse<boolean>>(`/goals/${goalId}`);
|
|
};
|
|
|
|
/**
|
|
* 记录目标完成
|
|
*/
|
|
export const completeGoal = async (goalId: string, completionData: CompleteGoalRequest = {}): Promise<ApiResponse<GoalCompletion>> => {
|
|
return api.post<ApiResponse<GoalCompletion>>(`/goals/${goalId}/complete`, completionData);
|
|
};
|
|
|
|
/**
|
|
* 获取目标完成记录
|
|
*/
|
|
export const getGoalCompletions = async (
|
|
goalId: string,
|
|
query: GetGoalCompletionsQuery = {}
|
|
): Promise<ApiResponse<PaginatedResponse<GoalCompletion>>> => {
|
|
const searchParams = new URLSearchParams();
|
|
|
|
Object.entries(query).forEach(([key, value]) => {
|
|
if (value !== undefined && value !== null) {
|
|
searchParams.append(key, String(value));
|
|
}
|
|
});
|
|
|
|
const queryString = searchParams.toString();
|
|
const path = queryString ? `/goals/${goalId}/completions?${queryString}` : `/goals/${goalId}/completions`;
|
|
|
|
return api.get<ApiResponse<PaginatedResponse<GoalCompletion>>>(path);
|
|
};
|
|
|
|
/**
|
|
* 获取目标统计信息
|
|
*/
|
|
export const getGoalStats = async (): Promise<ApiResponse<GoalStats>> => {
|
|
return api.get<ApiResponse<GoalStats>>('/goals/stats/overview');
|
|
};
|
|
|
|
/**
|
|
* 批量操作目标
|
|
*/
|
|
export const batchOperateGoals = async (operationData: BatchGoalOperationRequest): Promise<ApiResponse<BatchGoalOperationResult[]>> => {
|
|
return api.post<ApiResponse<BatchGoalOperationResult[]>>('/goals/batch', operationData);
|
|
};
|
|
|
|
|
|
// 导出所有API方法
|
|
export const goalsApi = {
|
|
createGoal,
|
|
getGoals,
|
|
getGoalById,
|
|
updateGoal,
|
|
deleteGoal,
|
|
completeGoal,
|
|
getGoalCompletions,
|
|
getGoalStats,
|
|
batchOperateGoals,
|
|
};
|
|
|
|
export default goalsApi; |