import { useState } from 'react' import { repetitionManagement } from '../lib/supabase' import type { Experiment, ExperimentRepetition } from '../lib/supabase' interface RepetitionScheduleModalProps { experiment: Experiment repetition: ExperimentRepetition onClose: () => void onScheduleUpdated: (updatedRepetition: ExperimentRepetition) => void } export function RepetitionScheduleModal({ experiment, repetition, onClose, onScheduleUpdated }: RepetitionScheduleModalProps) { const [loading, setLoading] = useState(false) const [error, setError] = useState(null) // Initialize with existing scheduled date or current date/time const getInitialDateTime = () => { if (repetition.scheduled_date) { const date = new Date(repetition.scheduled_date) return { date: date.toISOString().split('T')[0], time: date.toTimeString().slice(0, 5) } } const now = new Date() // Set to next hour by default now.setHours(now.getHours() + 1, 0, 0, 0) return { date: now.toISOString().split('T')[0], time: now.toTimeString().slice(0, 5) } } const [dateTime, setDateTime] = useState(getInitialDateTime()) const isScheduled = repetition.scheduled_date && repetition.schedule_status === 'scheduled' const handleSubmit = async (e: React.FormEvent) => { e.preventDefault() setError(null) setLoading(true) try { // Validate date/time const selectedDateTime = new Date(`${dateTime.date}T${dateTime.time}`) const now = new Date() if (selectedDateTime <= now) { setError('Scheduled date and time must be in the future') setLoading(false) return } // Schedule the repetition const updatedRepetition = await repetitionManagement.scheduleRepetition( repetition.id, selectedDateTime.toISOString() ) onScheduleUpdated(updatedRepetition) onClose() } catch (err: any) { setError(err.message || 'Failed to schedule repetition') console.error('Schedule repetition error:', err) } finally { setLoading(false) } } const handleRemoveSchedule = async () => { if (!confirm('Are you sure you want to remove the schedule for this repetition?')) { return } setError(null) setLoading(true) try { const updatedRepetition = await repetitionManagement.removeRepetitionSchedule(repetition.id) onScheduleUpdated(updatedRepetition) onClose() } catch (err: any) { setError(err.message || 'Failed to remove schedule') console.error('Remove schedule error:', err) } finally { setLoading(false) } } const handleCancel = () => { onClose() } return (
e.stopPropagation()}> {/* Close Button */} {/* Header */}

Schedule Repetition

{/* Experiment and Repetition Info */}

Experiment #{experiment.experiment_number} - Repetition #{repetition.repetition_number}

{experiment.reps_required} reps required • {experiment.soaking_duration_hr}h soaking

{/* Error Message */} {error && (
{error}
)} {/* Current Schedule (if exists) */} {isScheduled && (
Currently Scheduled

{new Date(repetition.scheduled_date!).toLocaleString()}

)} {/* Schedule Form */}
setDateTime({ ...dateTime, date: e.target.value })} className="max-w-xs px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500 transition-colors text-sm" required />
setDateTime({ ...dateTime, time: e.target.value })} className="max-w-xs px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500 transition-colors text-sm" required />
{/* Action Buttons */}
{isScheduled && ( )}
) }