diff --git a/frontend/src/components/TaskForm/TaskForm.tsx b/frontend/src/components/TaskForm/TaskForm.tsx deleted file mode 100644 index 2bc9b2d..0000000 --- a/frontend/src/components/TaskForm/TaskForm.tsx +++ /dev/null @@ -1,297 +0,0 @@ -import React, { useEffect, useRef, useState } from 'react'; -import { useForm } from 'react-hook-form'; -import { - DESCRIPTION_MAX_LENGTH, - PRIORITY_LABELS, - STATUS_LABELS, - TASK_PRIORITIES, - TASK_STATUSES, - Task, - TaskDraft, - TaskPriority, - TaskStatus, - TITLE_MAX_LENGTH, - User, -} from '../../types'; -import './TaskForm.css'; - -interface TaskFormProps { - users: User[]; - /** Task being edited, or null to create a new one. */ - task: Task | null; - onCancel: () => void; - /** Rejects with a user-safe message; the form stays open so nothing is lost. */ - onSubmit: (draft: TaskDraft) => Promise; -} - -interface FormValues { - title: string; - description: string; - assignee_id: string; - status: TaskStatus; - priority: TaskPriority; - due_date: string; -} - -const FOCUSABLE = - 'button:not([disabled]), input:not([disabled]), select:not([disabled]), textarea:not([disabled]), [href]'; - -const TaskForm: React.FC = ({ users, task, onCancel, onSubmit }) => { - const dialogRef = useRef(null); - const [submitError, setSubmitError] = useState(null); - - const { - register, - handleSubmit, - setFocus, - formState: { errors, isSubmitting }, - } = useForm({ - defaultValues: { - title: task?.title ?? '', - description: task?.description ?? '', - assignee_id: task?.assigned_to ? String(task.assigned_to.id) : '', - status: task?.status ?? 'open', - priority: task?.priority ?? 'medium', - due_date: task?.due_date ?? '', - }, - }); - - useEffect(() => { - setFocus('title'); - }, [setFocus]); - - const submit = async (values: FormValues): Promise => { - setSubmitError(null); - try { - await onSubmit({ - title: values.title, - description: values.description, - assignee_id: Number(values.assignee_id), - status: values.status, - priority: values.priority, - due_date: values.due_date, - }); - } catch (error) { - setSubmitError( - error instanceof Error - ? error.message - : 'The task could not be saved. Your details are still here, so you can try again.' - ); - } - }; - - /** Escape closes; Tab stays inside the dialog. */ - const handleKeyDown = (event: React.KeyboardEvent): void => { - if (event.key === 'Escape') { - event.stopPropagation(); - onCancel(); - return; - } - - if (event.key !== 'Tab' || !dialogRef.current) { - return; - } - - const focusable = Array.from(dialogRef.current.querySelectorAll(FOCUSABLE)); - if (focusable.length === 0) { - return; - } - - const first = focusable[0]; - const last = focusable[focusable.length - 1]; - - if (event.shiftKey && document.activeElement === first) { - event.preventDefault(); - last.focus(); - } else if (!event.shiftKey && document.activeElement === last) { - event.preventDefault(); - first.focus(); - } - }; - - const titleId = 'task-form-title'; - - return ( -
-
-
-
-

{task ? 'Edit Task' : 'Create New Task'}

- -
- - {submitError && ( -

- {submitError} -

- )} - -
- - - {errors.title ? ( - - ) : ( -

- {' '} - Required field -

- )} -
- -
- -