import React, { useEffect, useState } from "react"; import { useNavigate, useParams, Link } from "react-router-dom"; import { tasksApi, Task } from "@/services/tasksApi"; import { TaskForm } from "@/components/tasks/TaskForm"; import TaskTimeLogCard from "@/components/tasks/TaskTimeLogCard"; import Layout from "@/components/layout/layout"; import { toast } from "sonner"; import { ChevronRight, Home } from "lucide-react"; import { issuesApi } from "@/lib/api"; // Extend Task interface to include projectId interface ExtendedTask extends Task { projectId?: number | null; } const EditTaskPage = () => { const navigate = useNavigate(); const { id } = useParams(); const [task, setTask] = useState(); const [isLoading, setIsLoading] = useState(false); useEffect(() => { if (id) { fetchTask(); } }, [id]); const fetchTask = async () => { try { setIsLoading(true); const taskData = await tasksApi.getById(Number(id)); if (taskData) { // Fetch the issue to get its project ID if (taskData.issuesId) { try { const issueData = await issuesApi.getById(taskData.issuesId); if (issueData) { // Create extended task with project ID from the issue const extendedTask: ExtendedTask = { ...taskData, projectId: issueData.projectId }; setTask(extendedTask); return; } } catch (error) { console.error("Error fetching issue data:", error); } } // If we can't get the project ID, just use the task data setTask(taskData); } } catch (error) { console.error("Error fetching task:", error); toast.error("Failed to load task"); } finally { setIsLoading(false); } }; const handleSubmit = async (data: ExtendedTask) => { try { setIsLoading(true); // Remove projectId from the data before submitting to the API const { projectId, ...taskData } = data; await tasksApi.update(Number(id), taskData); toast.success("Task updated successfully"); navigate("/tasks"); } catch (error) { console.error("Error updating task:", error); toast.error("Failed to update task"); } finally { setIsLoading(false); } }; const handleCancel = () => { navigate("/tasks"); }; return (
{/* Breadcrumb Navigation */} {/* Header Section */}

Edit Task: {task?.title || ''}

Update the task details below

{/* Form Section */}
{/* Time Log Section - Only show when we have a valid task ID */} {task?.id && ( )}
); }; export default EditTaskPage;