|
| 1 | +import { useState, useRef, useEffect, useCallback } from 'react' |
| 2 | +import { FiMessageSquare, FiTag, FiCalendar } from 'react-icons/fi' |
| 3 | +import { TextInput } from '@/components/text-input' |
| 4 | +import { Button } from '@/components/button/button' |
| 5 | +import type { TodoPriority } from '@/context/todo.context' |
| 6 | +import type { Todo } from '@/services/hooks/todo/todo.interface' |
| 7 | +import Modal from '@/components/modal' |
| 8 | +import { PRIORITY_OPTIONS } from '@/common/constant/priority_options' |
| 9 | +import { PriorityButton } from '@/components/priority-options/priority-options' |
| 10 | +import { ClickableTooltip } from '@/components/clickableTooltip' |
| 11 | +import { DatePicker } from '@/components/date-picker/date-picker' |
| 12 | +import type jalaliMoment from 'jalali-moment' |
| 13 | +import { parseTodoDate } from './tools/parse-date' |
| 14 | +import { showToast } from '@/common/toast' |
| 15 | +import { useUpdateTodo } from '@/services/hooks/todo/update-todo.hook' |
| 16 | +import { safeAwait } from '@/services/api' |
| 17 | +import { translateError } from '@/utils/translate-error' |
| 18 | +import { useQueryClient } from '@tanstack/react-query' |
| 19 | +import Analytics from '@/analytics' |
| 20 | + |
| 21 | +interface EditTodoModalProps { |
| 22 | + todo: Todo |
| 23 | + isOpen: boolean |
| 24 | + onClose: () => void |
| 25 | +} |
| 26 | + |
| 27 | +export function EditTodoModal({ todo, isOpen, onClose }: EditTodoModalProps) { |
| 28 | + const queryClient = useQueryClient() |
| 29 | + |
| 30 | + const [isDatePickerOpen, setIsDatePickerOpen] = useState(false) |
| 31 | + const [selectedDate, setSelectedDate] = useState<jalaliMoment.Moment | undefined>( |
| 32 | + todo.date ? parseTodoDate(todo.date) : undefined |
| 33 | + ) |
| 34 | + const { mutateAsync, isPending } = useUpdateTodo() |
| 35 | + const datePickerButtonRef = useRef<HTMLButtonElement>(null) |
| 36 | + |
| 37 | + const [text, setText] = useState(todo.text) |
| 38 | + const [notes, setNotes] = useState(todo.notes || '') |
| 39 | + const [category, setCategory] = useState(todo.category || '') |
| 40 | + const [priority, setPriority] = useState<TodoPriority>(todo.priority as TodoPriority) |
| 41 | + |
| 42 | + useEffect(() => { |
| 43 | + setText(todo.text) |
| 44 | + setNotes(todo.notes || '') |
| 45 | + setCategory(todo.category || '') |
| 46 | + setPriority(todo.priority as TodoPriority) |
| 47 | + setSelectedDate(todo.date ? parseTodoDate(todo.date) : undefined) |
| 48 | + Analytics.event('todo_edit_opened') |
| 49 | + }, [todo]) |
| 50 | + |
| 51 | + const handleTextChange = useCallback((value: string) => { |
| 52 | + setText(value) |
| 53 | + }, []) |
| 54 | + |
| 55 | + const handleNotesChange = useCallback((e: React.ChangeEvent<HTMLTextAreaElement>) => { |
| 56 | + setNotes(e.target.value) |
| 57 | + }, []) |
| 58 | + |
| 59 | + const handleCategoryChange = useCallback((value: string) => { |
| 60 | + setCategory(value) |
| 61 | + }, []) |
| 62 | + |
| 63 | + const handlePriorityChange = useCallback((newPriority: TodoPriority) => { |
| 64 | + setPriority(newPriority) |
| 65 | + }, []) |
| 66 | + |
| 67 | + const handleDateSelect = useCallback((date: jalaliMoment.Moment) => { |
| 68 | + setSelectedDate(date) |
| 69 | + setIsDatePickerOpen(false) |
| 70 | + }, []) |
| 71 | + |
| 72 | + const handleSave = useCallback(async () => { |
| 73 | + if (!text.trim()) { |
| 74 | + showToast('متن وظیفه نمیتواند خالی باشد', 'error') |
| 75 | + return |
| 76 | + } |
| 77 | + |
| 78 | + const input = { |
| 79 | + text, |
| 80 | + notes, |
| 81 | + category, |
| 82 | + priority, |
| 83 | + date: selectedDate?.add(3.5, 'hours').toISOString(), |
| 84 | + } |
| 85 | + |
| 86 | + const [err, _] = await safeAwait( |
| 87 | + mutateAsync({ |
| 88 | + id: todo.onlineId || todo.id, |
| 89 | + input, |
| 90 | + }) |
| 91 | + ) |
| 92 | + |
| 93 | + if (err) { |
| 94 | + showToast(translateError(err) as any, 'error') |
| 95 | + return |
| 96 | + } |
| 97 | + showToast('وظیفه با موفقیت ویرایش شد', 'success') |
| 98 | + onClose() |
| 99 | + queryClient.invalidateQueries({ queryKey: ['getTodos'] }) |
| 100 | + }, [text, notes, category, priority, selectedDate, onClose]) |
| 101 | + |
| 102 | + return ( |
| 103 | + <Modal |
| 104 | + isOpen={isOpen} |
| 105 | + onClose={onClose} |
| 106 | + title="ویرایش وظیفه" |
| 107 | + size="md" |
| 108 | + direction="rtl" |
| 109 | + > |
| 110 | + <div className="p-1 space-y-2"> |
| 111 | + <div className="space-y-2"> |
| 112 | + <TextInput |
| 113 | + value={text} |
| 114 | + onChange={handleTextChange} |
| 115 | + placeholder="متن وظیفه را وارد کنید" |
| 116 | + className="text-sm" |
| 117 | + debounce={false} |
| 118 | + /> |
| 119 | + </div> |
| 120 | + |
| 121 | + <div className="flex items-center justify-between"> |
| 122 | + <div className="flex items-center gap-3"> |
| 123 | + <div className="relative flex-shrink-0 text-center"> |
| 124 | + <span className="absolute w-2 h-2 rounded-full -left-0.5 -bottom-0.5 bg-error animate-pulse"></span> |
| 125 | + <FiCalendar className="text-indigo-400" size={14} /> |
| 126 | + </div> |
| 127 | + <button |
| 128 | + ref={datePickerButtonRef} |
| 129 | + onClick={() => setIsDatePickerOpen(!isDatePickerOpen)} |
| 130 | + className="min-w-full text-right px-2 py-1.5 min-h-8 text-xs rounded-xl border border-base-300 hover:border-primary/50 transition-colors bg-content text-content opacity-75 cursor-pointer" |
| 131 | + > |
| 132 | + {selectedDate |
| 133 | + ? selectedDate.locale('fa').format('dddd، jD jMMMM') |
| 134 | + : 'انتخاب تاریخ'} |
| 135 | + </button> |
| 136 | + <ClickableTooltip |
| 137 | + triggerRef={datePickerButtonRef} |
| 138 | + isOpen={isDatePickerOpen} |
| 139 | + setIsOpen={setIsDatePickerOpen} |
| 140 | + content={ |
| 141 | + <DatePicker |
| 142 | + onDateSelect={handleDateSelect} |
| 143 | + selectedDate={selectedDate} |
| 144 | + /> |
| 145 | + } |
| 146 | + contentClassName="!p-0 !bg-transparent !border-none !shadow-none" |
| 147 | + /> |
| 148 | + </div> |
| 149 | + <div className="flex items-center gap-1"> |
| 150 | + {PRIORITY_OPTIONS.map((option) => ( |
| 151 | + <PriorityButton |
| 152 | + key={option.value} |
| 153 | + option={option} |
| 154 | + isSelected={priority === option.value} |
| 155 | + onClick={() => |
| 156 | + handlePriorityChange(option.value as TodoPriority) |
| 157 | + } |
| 158 | + /> |
| 159 | + ))} |
| 160 | + </div> |
| 161 | + </div> |
| 162 | + |
| 163 | + <div className="flex items-center gap-3"> |
| 164 | + <div className="flex-shrink-0 text-center"> |
| 165 | + <FiTag className="text-indigo-400" size={14} /> |
| 166 | + </div> |
| 167 | + <TextInput |
| 168 | + value={category} |
| 169 | + onChange={handleCategoryChange} |
| 170 | + placeholder="دستهبندی (مثال: شخصی، کاری)" |
| 171 | + className="text-xs placeholder:text-xs py-1.5" |
| 172 | + debounce={false} |
| 173 | + /> |
| 174 | + </div> |
| 175 | + |
| 176 | + <div className="flex items-center gap-2"> |
| 177 | + <div className="flex-shrink-0 text-center"> |
| 178 | + <FiMessageSquare className="text-indigo-400" size={14} /> |
| 179 | + </div> |
| 180 | + <textarea |
| 181 | + value={notes} |
| 182 | + onChange={handleNotesChange} |
| 183 | + placeholder="یادداشت یا لینک (اختیاری)" |
| 184 | + className="w-full px-4 py-2 mt-2 text-base font-light leading-relaxed transition-all border-none outline-none resize-none bg-content min-h-48 focus:ring-1 focus:ring-primary/30 rounded-xl text-muted " |
| 185 | + /> |
| 186 | + </div> |
| 187 | + |
| 188 | + {/* Action Buttons */} |
| 189 | + <div className="flex items-center justify-end gap-2 pt-4"> |
| 190 | + <Button |
| 191 | + onClick={onClose} |
| 192 | + size="md" |
| 193 | + disabled={isPending} |
| 194 | + className={ |
| 195 | + 'btn btn-circle !bg-base-300 hover:!bg-error/10 text-muted hover:!text-error px-10 border-none shadow-none !rounded-2xl transition-colors duration-300 ease-in-out' |
| 196 | + } |
| 197 | + > |
| 198 | + لغو |
| 199 | + </Button> |
| 200 | + <Button |
| 201 | + onClick={handleSave} |
| 202 | + disabled={!text?.trim() || isPending} |
| 203 | + size="md" |
| 204 | + isPrimary={true} |
| 205 | + loading={isPending} |
| 206 | + className={ |
| 207 | + 'btn btn-circle !w-fit px-8 border-none shadow-none text-secondary !rounded-2xl transition-colors duration-300 ease-in-out' |
| 208 | + } |
| 209 | + > |
| 210 | + ذخیره |
| 211 | + </Button> |
| 212 | + </div> |
| 213 | + </div> |
| 214 | + </Modal> |
| 215 | + ) |
| 216 | +} |
0 commit comments