import React, { useState } from 'react'; import { RecurrenceType } from '../../entities/Task'; import { useTranslation } from 'react-i18next'; interface RecurrenceInputProps { recurrenceType: RecurrenceType; recurrenceInterval: number; recurrenceEndDate?: string; recurrenceWeekday?: number; recurrenceMonthDay?: number; recurrenceWeekOfMonth?: number; completionBased: boolean; onChange: (field: string, value: any) => void; disabled?: boolean; isChildTask?: boolean; parentTaskLoading?: boolean; onEditParent?: () => void; onParentRecurrenceChange?: (field: string, value: any) => void; } const RecurrenceInput: React.FC = ({ recurrenceType, recurrenceInterval, recurrenceEndDate, recurrenceWeekday, recurrenceMonthDay, recurrenceWeekOfMonth, completionBased, onChange, disabled = false, isChildTask = false, parentTaskLoading = false, onEditParent, onParentRecurrenceChange, }) => { const { t } = useTranslation(); const [editingParentRecurrence, setEditingParentRecurrence] = useState(false); const weekdays = [ { value: 0, label: t('weekdays.sunday', 'Sunday') }, { value: 1, label: t('weekdays.monday', 'Monday') }, { value: 2, label: t('weekdays.tuesday', 'Tuesday') }, { value: 3, label: t('weekdays.wednesday', 'Wednesday') }, { value: 4, label: t('weekdays.thursday', 'Thursday') }, { value: 5, label: t('weekdays.friday', 'Friday') }, { value: 6, label: t('weekdays.saturday', 'Saturday') }, ]; const weekOfMonthOptions = [ { value: 1, label: t('recurrence.firstWeek', 'First') }, { value: 2, label: t('recurrence.secondWeek', 'Second') }, { value: 3, label: t('recurrence.thirdWeek', 'Third') }, { value: 4, label: t('recurrence.fourthWeek', 'Fourth') }, { value: 5, label: t('recurrence.lastWeek', 'Last') }, ]; const renderRecurrenceTypeSelect = (customOnChange?: (field: string, value: any) => void, isDisabled?: boolean) => (
); const renderIntervalInput = (customOnChange?: (field: string, value: any) => void, isDisabled?: boolean) => (
(customOnChange || onChange)('recurrence_interval', parseInt(e.target.value))} className="block w-20 border border-gray-300 dark:border-gray-900 rounded-md focus:outline-none shadow-sm px-2 py-2 text-sm bg-white dark:bg-gray-900 text-gray-900 dark:text-gray-100" disabled={isDisabled} /> {recurrenceType === 'daily' && t('recurrence.days', 'days')} {recurrenceType === 'weekly' && t('recurrence.weeks', 'weeks')} {(recurrenceType === 'monthly' || recurrenceType === 'monthly_weekday' || recurrenceType === 'monthly_last_day') && t('recurrence.months', 'months')}
); const renderWeekdaySelect = (customOnChange?: (field: string, value: any) => void, isDisabled?: boolean) => (
); const renderMonthDayInput = (customOnChange?: (field: string, value: any) => void, isDisabled?: boolean) => (
(customOnChange || onChange)('recurrence_month_day', e.target.value ? parseInt(e.target.value) : null)} placeholder={t('recurrence.monthDayPlaceholder', 'Leave empty for current day')} className="block w-full border border-gray-300 dark:border-gray-900 rounded-md focus:outline-none shadow-sm px-2 py-2 text-sm bg-white dark:bg-gray-900 text-gray-900 dark:text-gray-100" disabled={isDisabled} />
); const renderMonthlyWeekdayInputs = () => (
); const renderEndDateInput = (customOnChange?: (field: string, value: any) => void, isDisabled?: boolean) => (
(customOnChange || onChange)('recurrence_end_date', e.target.value || null)} className="block w-full border border-gray-300 dark:border-gray-900 rounded-md focus:outline-none shadow-sm px-2 py-2 text-sm bg-white dark:bg-gray-900 text-gray-900 dark:text-gray-100" disabled={isDisabled} />
); const renderCompletionBasedToggle = () => (

{t('forms.task.completionBasedHelp', 'If checked, the next task will be created based on completion date instead of due date')}

); // Show message for child tasks if (isChildTask && parentTaskLoading) { return (

{t('forms.task.recurrenceSettings', 'Recurrence Settings')}

Loading parent task recurrence settings...
); } if (isChildTask) { return (

{t('forms.task.recurrenceSettings', 'Recurrence Settings')}

Recurring Task Instance

This task was generated from a recurring task. The recurrence settings shown below are inherited from the original task and cannot be edited here.

{onParentRecurrenceChange && ( )}
{editingParentRecurrence && (
⚠️ You are editing the parent task's recurrence settings. Changes will affect all future instances of this recurring task.
)} {recurrenceType === 'none' ? renderRecurrenceTypeSelect(editingParentRecurrence ? onParentRecurrenceChange : undefined, !editingParentRecurrence) : ( <> {renderRecurrenceTypeSelect(editingParentRecurrence ? onParentRecurrenceChange : undefined, !editingParentRecurrence)} {renderIntervalInput(editingParentRecurrence ? onParentRecurrenceChange : undefined, !editingParentRecurrence)} {(recurrenceType === 'weekly' || recurrenceType === 'monthly_weekday') && renderWeekdaySelect(editingParentRecurrence ? onParentRecurrenceChange : undefined, !editingParentRecurrence)} {recurrenceType === 'monthly' && renderMonthDayInput(editingParentRecurrence ? onParentRecurrenceChange : undefined, !editingParentRecurrence)} {recurrenceType === 'monthly_weekday' && renderMonthlyWeekdayInputs()} {renderEndDateInput(editingParentRecurrence ? onParentRecurrenceChange : undefined, !editingParentRecurrence)} {renderCompletionBasedToggle()} )}
); } if (recurrenceType === 'none') { return (
{renderRecurrenceTypeSelect()}
); } return (

{t('forms.task.recurrenceSettings', 'Recurrence Settings')}

{/* Main recurrence settings in one row */}
{(recurrenceType === 'daily' || recurrenceType === 'weekly' || recurrenceType === 'monthly' || recurrenceType === 'monthly_weekday' || recurrenceType === 'monthly_last_day') && (
onChange('recurrence_interval', parseInt(e.target.value))} className="block w-20 border border-gray-300 dark:border-gray-900 rounded-md focus:outline-none shadow-sm px-2 py-2 text-sm bg-white dark:bg-gray-900 text-gray-900 dark:text-gray-100" disabled={disabled} /> {recurrenceType === 'daily' && t('recurrence.days', 'days')} {recurrenceType === 'weekly' && t('recurrence.weeks', 'weeks')} {(recurrenceType === 'monthly' || recurrenceType === 'monthly_weekday' || recurrenceType === 'monthly_last_day') && t('recurrence.months', 'months')}
)}
onChange('recurrence_end_date', e.target.value || null)} className="block w-full border border-gray-300 dark:border-gray-900 rounded-md focus:outline-none shadow-sm px-2 py-2 text-sm bg-white dark:bg-gray-900 text-gray-900 dark:text-gray-100" disabled={disabled} />
{/* Additional settings for specific recurrence types */} {recurrenceType === 'weekly' && renderWeekdaySelect()} {recurrenceType === 'monthly' && renderMonthDayInput()} {recurrenceType === 'monthly_weekday' && renderMonthlyWeekdayInputs()} {renderCompletionBasedToggle()}
); }; export default RecurrenceInput;