- 新增创建目标弹窗,支持用户输入目标信息并提交 - 实现目标数据的转换,支持将目标转换为待办事项和时间轴事件 - 优化目标页面,集成Redux状态管理,处理目标的创建、完成和错误提示 - 更新时间轴组件,支持动态显示目标安排 - 编写目标管理功能实现文档,详细描述功能和组件架构
154 lines
3.8 KiB
TypeScript
154 lines
3.8 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<ApiResponse<Goal>> => {
|
|
return api.put<ApiResponse<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);
|
|
};
|
|
|
|
/**
|
|
* 暂停目标
|
|
*/
|
|
export const pauseGoal = async (goalId: string): Promise<ApiResponse<Goal>> => {
|
|
return updateGoal(goalId, { status: 'paused' });
|
|
};
|
|
|
|
/**
|
|
* 恢复目标
|
|
*/
|
|
export const resumeGoal = async (goalId: string): Promise<ApiResponse<Goal>> => {
|
|
return updateGoal(goalId, { status: 'active' });
|
|
};
|
|
|
|
/**
|
|
* 完成目标
|
|
*/
|
|
export const markGoalCompleted = async (goalId: string): Promise<ApiResponse<Goal>> => {
|
|
return updateGoal(goalId, { status: 'completed' });
|
|
};
|
|
|
|
/**
|
|
* 取消目标
|
|
*/
|
|
export const cancelGoal = async (goalId: string): Promise<ApiResponse<Goal>> => {
|
|
return updateGoal(goalId, { status: 'cancelled' });
|
|
};
|
|
|
|
// 导出所有API方法
|
|
export const goalsApi = {
|
|
createGoal,
|
|
getGoals,
|
|
getGoalById,
|
|
updateGoal,
|
|
deleteGoal,
|
|
completeGoal,
|
|
getGoalCompletions,
|
|
getGoalStats,
|
|
batchOperateGoals,
|
|
pauseGoal,
|
|
resumeGoal,
|
|
markGoalCompleted,
|
|
cancelGoal,
|
|
};
|
|
|
|
export default goalsApi; |