"use client"; import { useState, useEffect } from "react"; import Button from "./ui/Button"; import Badge from "./ui/Badge"; import { useTranslation } from "@/lib/i18n"; export default function ProjectTaskForm({ projectId, onTaskAdded }) { const { t } = useTranslation(); const [taskTemplates, setTaskTemplates] = useState([]); const [users, setUsers] = useState([]); const [taskType, setTaskType] = useState("template"); // "template" or "custom" const [selectedTemplate, setSelectedTemplate] = useState(""); const [customTaskName, setCustomTaskName] = useState(""); const [customMaxWaitDays, setCustomMaxWaitDays] = useState(""); const [customDescription, setCustomDescription] = useState(""); const [priority, setPriority] = useState("normal"); const [assignedTo, setAssignedTo] = useState(""); const [isSubmitting, setIsSubmitting] = useState(false); useEffect(() => { // Fetch available task templates fetch("/api/tasks/templates") .then((res) => res.json()) .then(setTaskTemplates); // Fetch users for assignment fetch("/api/project-tasks/users") .then((res) => res.json()) .then(setUsers); }, []); async function handleSubmit(e) { e.preventDefault(); // Validate based on task type if (taskType === "template" && !selectedTemplate) return; if (taskType === "custom" && !customTaskName.trim()) return; setIsSubmitting(true); try { const requestData = { project_id: parseInt(projectId), priority, assigned_to: assignedTo || null, }; if (taskType === "template") { requestData.task_template_id = parseInt(selectedTemplate); } else { requestData.custom_task_name = customTaskName.trim(); requestData.custom_max_wait_days = parseInt(customMaxWaitDays) || 0; requestData.custom_description = customDescription.trim(); } const res = await fetch("/api/project-tasks", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(requestData), }); if (res.ok) { // Reset form setSelectedTemplate(""); setCustomTaskName(""); setCustomMaxWaitDays(""); setCustomDescription(""); setPriority("normal"); setAssignedTo(""); if (onTaskAdded) onTaskAdded(); } else { alert(t("tasks.addTaskError")); } } catch (error) { alert(t("tasks.addTaskError")); } finally { setIsSubmitting(false); } } return (
{taskType === "template" ? (
{" "}
) : (
setCustomTaskName(e.target.value)} className="w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:ring-2 focus:ring-blue-500 focus:border-blue-500" placeholder={t("tasks.enterTaskName")} required />
setCustomMaxWaitDays(e.target.value)} className="w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:ring-2 focus:ring-blue-500 focus:border-blue-500" placeholder="0" min="0" />