import { View, Text, StyleSheet, KeyboardAvoidingView, ScrollView, TouchableOpacity, } from "react-native"; import { useState } from "react"; import { router } from "expo-router"; import { Card } from "@/components/ui/Cards"; import { Input } from "@/components/ui/Input"; import { Button } from "@/components/ui/Button"; import { Section, SectionHeader } from "@/components/ui/Sections"; import { Spacing, Typography, Colors, BorderRadius, Shadows } from "@/constants/theme"; import { TimePicker } from "@/components/ui/TimePicker"; import { useCreateCareAction } from "@/hooks"; import { MedicinePeriodicity } from "@/lib/api"; const PERIODICITY_OPTIONS: { label: string; value: MedicinePeriodicity }[] = [ { label: 'Uma vez', value: 'ONCE' }, { label: 'Diário', value: 'DAILY' }, { label: '2x ao dia', value: 'TWICE_DAILY' }, { label: '3x ao dia', value: 'THREE_TIMES_DAILY' }, { label: 'Semanal', value: 'WEEKLY' }, { label: 'Quinzenal', value: 'BIWEEKLY' }, { label: 'Mensal', value: 'MONTHLY' }, ]; function getTimePickerCount(p: MedicinePeriodicity): number { switch (p) { case 'ONCE': return 0; case 'DAILY': return 1; case 'TWICE_DAILY': return 2; case 'THREE_TIMES_DAILY': return 3; case 'WEEKLY': case 'BIWEEKLY': case 'MONTHLY': return 1; } } export default function MedicineNewCareAction() { const [medicineName, setMedicineName] = useState(''); const [medicineDosage, setMedicineDosage] = useState(''); const [medicinePeriodicity, setMedicinePeriodicity] = useState('ONCE'); const [scheduledTimes, setScheduledTimes] = useState([]); const createCareAction = useCreateCareAction(); const handleSave = async () => { const count = getTimePickerCount(medicinePeriodicity); const scheduledAt = scheduledTimes.slice(0, count).map( d => `${d.getHours().toString().padStart(2,'0')}:${d.getMinutes().toString().padStart(2,'0')}` ); await createCareAction.mutateAsync({ type: 'MEDICINE', moment: new Date(), medicine: { name: medicineName, dosage: medicineDosage, periodicity: medicinePeriodicity, scheduledAt, }, }); router.replace('/(tabs)/actions'); }; return ( Novo medicamento Informe os detalhes do medicamento
{PERIODICITY_OPTIONS.map((opt) => ( { setMedicinePeriodicity(opt.value); const count = getTimePickerCount(opt.value); setScheduledTimes(prev => count === 0 ? [] : count > prev.length ? [...prev, ...Array(count - prev.length).fill(new Date())] : prev.slice(0, count) ); }} activeOpacity={0.7} > {opt.label} ))}
{scheduledTimes.length > 0 && (
{scheduledTimes.map((t, i) => ( setScheduledTimes(prev => prev.map((v, idx) => idx === i ? date : v)) } /> ))}
)}