import React, { useState } from "react"; import { View, Text, StyleSheet, Modal, TextInput, TouchableOpacity, ScrollView, Platform, Alert, } from "react-native"; import { Ionicons } from "@expo/vector-icons"; import DateTimePicker from "@react-native-community/datetimepicker"; import type { CreateGoalData } from "../services/fitnessGoals"; import { useTheme } from "../contexts/ThemeContext"; import { MinimalButton } from "./MinimalButton"; interface GoalCreationModalProps { visible: boolean; onClose: () => void; onSubmit: (goalData: CreateGoalData) => Promise; } const GOAL_TYPES = [ { value: "weight_target", label: "Weight Target" }, { value: "strength_milestone", label: "Strength Milestone" }, { value: "endurance_target", label: "Endurance Target" }, { value: "flexibility_goal", label: "Flexibility Goal" }, { value: "habit_building", label: "Habit Building" }, { value: "custom", label: "Custom Goal" }, ] as const; const PRIORITIES = [ { value: "low", label: "Low" }, { value: "medium", label: "Medium" }, { value: "high", label: "High" }, ] as const; export function GoalCreationModal({ visible, onClose, onSubmit, }: GoalCreationModalProps) { const { colors, typography } = useTheme(); const [goalType, setGoalType] = useState("weight_target"); const [title, setTitle] = useState(""); const [description, setDescription] = useState(""); const [targetValue, setTargetValue] = useState(""); const [currentValue, setCurrentValue] = useState(""); const [unit, setUnit] = useState(""); const [priority, setPriority] = useState<"low" | "medium" | "high">("medium"); const [targetDate, setTargetDate] = useState(); const [showDatePicker, setShowDatePicker] = useState(false); const [submitting, setSubmitting] = useState(false); const resetForm = () => { setGoalType("weight_target"); setTitle(""); setDescription(""); setTargetValue(""); setCurrentValue(""); setUnit(""); setPriority("medium"); setTargetDate(undefined); }; const handleSubmit = async () => { if (!title.trim()) { Alert.alert("Error", "Please enter a goal title"); return; } setSubmitting(true); try { const goalData: CreateGoalData = { goalType, title: title.trim(), description: description.trim() || undefined, targetValue: targetValue ? parseFloat(targetValue) : undefined, currentValue: currentValue ? parseFloat(currentValue) : undefined, unit: unit.trim() || undefined, targetDate: targetDate?.toISOString(), priority, }; await onSubmit(goalData); resetForm(); onClose(); } catch (error) { console.error("Error creating goal:", error); Alert.alert("Error", "Failed to create goal. Please try again."); } finally { setSubmitting(false); } }; const handleClose = () => { resetForm(); onClose(); }; const getPriorityColor = (p: "low" | "medium" | "high") => { switch (p) { case "high": return colors.danger; case "medium": return colors.warning; case "low": return colors.success; } }; return ( Create Fitness Goal {/* Goal Type */} Goal Type * {GOAL_TYPES.map((type) => ( setGoalType(type.value)} > {type.label} ))} {/* Title */} Title * {/* Description */} Description {/* Target Value & Unit */} Target Value Unit {/* Current Value */} Current Value {/* Target Date */} Target Date setShowDatePicker(!showDatePicker)} > {targetDate ? targetDate.toLocaleDateString() : "Select target date"} {showDatePicker && ( { setShowDatePicker(Platform.OS === "ios"); if (selectedDate) { setTargetDate(selectedDate); } }} minimumDate={new Date()} themeVariant="light" /> )} {/* Priority */} Priority {PRIORITIES.map((p) => ( setPriority(p.value)} > {p.label} ))} ); } const styles = StyleSheet.create({ container: { flex: 1, }, header: { flexDirection: "row", justifyContent: "space-between", alignItems: "center", padding: 20, paddingTop: Platform.OS === "ios" ? 60 : 20, borderBottomWidth: 1, }, closeButton: { padding: 4, }, content: { flex: 1, padding: 20, }, field: { marginBottom: 20, }, input: { borderWidth: 1, borderRadius: 8, padding: 12, fontSize: 16, marginTop: 8, }, textArea: { height: 80, textAlignVertical: "top", }, row: { flexDirection: "row", gap: 12, }, flex1: { flex: 1, }, typeScroll: { flexGrow: 0, marginTop: 8, }, typeButton: { paddingHorizontal: 16, paddingVertical: 8, borderRadius: 8, borderWidth: 1, marginRight: 8, }, dateButton: { flexDirection: "row", justifyContent: "space-between", alignItems: "center", borderWidth: 1, borderRadius: 8, padding: 12, marginTop: 8, }, priorityContainer: { flexDirection: "row", gap: 12, marginTop: 8, }, priorityButton: { flex: 1, paddingVertical: 12, borderRadius: 8, borderWidth: 1, alignItems: "center", }, footer: { padding: 20, paddingBottom: Platform.OS === "ios" ? 40 : 20, borderTopWidth: 1, }, submitButton: { width: "100%", }, datePickerContainer: { borderRadius: 8, borderWidth: 1, marginTop: 8, overflow: "hidden", }, });