feat: 增强目标管理功能及相关组件

- 在 GoalsListScreen 中新增目标编辑功能,支持用户编辑现有目标
- 更新 CreateGoalModal 组件,支持编辑模式下的目标更新
- 在 NutritionRecordsScreen 中新增删除营养记录功能,允许用户删除不需要的记录
- 更新 NutritionRecordCard 组件,增加操作选项,支持删除记录
- 修改 dietRecords 服务,添加删除营养记录的 API 调用
- 优化 goalsSlice,确保目标更新逻辑与 Redux 状态管理一致
This commit is contained in:
2025-08-26 22:34:03 +08:00
parent 0610f287ee
commit 0a8b20f0ec
8 changed files with 244 additions and 131 deletions

View File

@@ -1,6 +1,6 @@
import { Colors } from '@/constants/Colors';
import { useColorScheme } from '@/hooks/useColorScheme';
import { CreateGoalRequest, GoalPriority, RepeatType } from '@/types/goals';
import { CreateGoalRequest, GoalPriority, RepeatType, UpdateGoalRequest } from '@/types/goals';
import { Ionicons } from '@expo/vector-icons';
import DateTimePicker from '@react-native-community/datetimepicker';
import { LinearGradient } from 'expo-linear-gradient';
@@ -24,9 +24,11 @@ interface CreateGoalModalProps {
visible: boolean;
onClose: () => void;
onSubmit: (goalData: CreateGoalRequest) => void;
onUpdate?: (goalId: string, goalData: UpdateGoalRequest) => void;
onSuccess?: () => void;
loading?: boolean;
initialData?: Partial<CreateGoalRequest>;
editGoalId?: string;
}
const REPEAT_TYPE_OPTIONS: { value: RepeatType; label: string }[] = [
@@ -41,9 +43,11 @@ export const CreateGoalModal: React.FC<CreateGoalModalProps> = ({
visible,
onClose,
onSubmit,
onUpdate,
onSuccess,
loading = false,
initialData,
editGoalId,
}) => {
const theme = (useColorScheme() ?? 'light') as 'light' | 'dark';
const colorTokens = Colors[theme];
@@ -130,28 +134,49 @@ export const CreateGoalModal: React.FC<CreateGoalModalProps> = ({
startTime = hours * 60 + minutes;
}
// 根据是否是编辑模式决定数据结构
if (editGoalId && onUpdate) {
// 更新模式:使用 UpdateGoalRequest 结构
const updateData: UpdateGoalRequest = {
title: title.trim(),
description: description.trim() || undefined,
repeatType,
frequency,
category: category.trim() || undefined,
priority,
hasReminder,
reminderTime: hasReminder ? reminderTime : undefined,
customRepeatRule: {
weekdays: repeatType === 'weekly' ? selectedWeekdays : [1, 2, 3, 4, 5, 6, 0],
dayOfMonth: repeatType === 'monthly' ? selectedMonthDays : undefined,
},
endDate: endDate || undefined,
};
console.log('updateData', updateData);
onUpdate(editGoalId, updateData);
} else {
// 创建模式:使用 CreateGoalRequest 结构
const goalData: CreateGoalRequest = {
title: title.trim(),
description: description.trim() || undefined,
repeatType,
frequency,
category: category.trim() || undefined,
priority,
hasReminder,
reminderTime: hasReminder ? reminderTime : undefined,
customRepeatRule: {
weekdays: repeatType === 'weekly' ? selectedWeekdays : [1, 2, 3, 4, 5, 6, 0],
dayOfMonth: repeatType === 'monthly' ? selectedMonthDays : undefined,
},
startTime,
endDate: endDate || undefined,
};
const goalData: CreateGoalRequest = {
title: title.trim(),
description: description.trim() || undefined,
repeatType,
frequency,
category: category.trim() || undefined,
priority,
hasReminder,
reminderTime: hasReminder ? reminderTime : undefined,
customRepeatRule: {
weekdays: repeatType === 'weekly' ? selectedWeekdays : [1, 2, 3, 4, 5, 6, 0], // 根据重复类型设置周几
dayOfMonth: repeatType === 'monthly' ? selectedMonthDays : undefined, // 根据重复类型设置月几
},
startTime,
endDate: endDate || undefined,
};
console.log('goalData', goalData);
onSubmit(goalData);
console.log('goalData', goalData);
onSubmit(goalData);
}
// 通知父组件提交成功
if (onSuccess) {
@@ -278,7 +303,7 @@ export const CreateGoalModal: React.FC<CreateGoalModalProps> = ({
<Ionicons name="close" size={24} color={colorTokens.text} />
</TouchableOpacity>
<Text style={[styles.title, { color: colorTokens.text }]}>
{editGoalId ? '编辑目标' : '创建新目标'}
</Text>
<View style={{ width: 24 }} />
</View>
@@ -717,7 +742,7 @@ export const CreateGoalModal: React.FC<CreateGoalModalProps> = ({
disabled={loading || !title.trim()}
>
<Text style={styles.saveButtonText}>
{loading ? '保存中...' : '保存'}
{loading ? (editGoalId ? '更新中...' : '保存中...') : (editGoalId ? '更新' : '保存')}
</Text>
</TouchableOpacity>
</View>