import { useEffect, useState } from "react"; import { useNavigate } from "react-router-dom"; import { getUserTasks, createTask, deleteTask } from "../api/tasks"; import { logout } from "../api/auth"; import api from "../api/api"; import Cookies from "js-cookie"; import { User } from "lucide-react"; // Define Task type interface Task { id: number; title: string; description: string; due_date: string; done: boolean; } const Tasks = () => { const [tasks, setTasks] = useState([]); const [newTask, setNewTask] = useState({ title: "", description: "", due_date: "", done: false }); const [editingTaskId, setEditingTaskId] = useState(null); const [editedTask, setEditedTask] = useState>({}); const navigate = useNavigate(); const userId = Number(Cookies.get("user_id")) useEffect(() => { if (!userId) { navigate("/login"); return; } const fetchTasks = async () => { try { const tasksData = await getUserTasks(userId); setTasks(tasksData); } catch (error) { console.error("Error during tasks fetching:", error); } }; fetchTasks(); }, [userId]); const handleLogout = async () => { await logout(); navigate("/login"); }; const handleCreateTask = async () => { try { const task = await createTask(newTask); setTasks([...tasks, task]); // List update setNewTask({ title: "", description: "", due_date: "", done: false }); // Form reset } catch (error) { console.error("Error during task creation:", error); } }; const handleDeleteTask = async (taskId: number) => { try { await deleteTask(taskId); setTasks(tasks.filter((task) => task.id !== taskId)); } catch (error) { console.error("Error during task deletion:", error); } }; // Change task status const handleToggleTaskStatus = async (taskId: number) => { try { const updatedTasks = tasks.map(task => { if (task.id === taskId) { const newStatus = !task.done; // Send PATCH request to backend api.patch(`/tasks/${task.id}`, { done: newStatus }); // Change local state return { ...task, done: newStatus }; } return task; }); setTasks(updatedTasks); // refresh UI } catch (err) { console.error("Error during task update:", err); } }; const handleEditTask = (task: Task) => { setEditingTaskId(task.id); setEditedTask({ title: task.title, description: task.description, due_date: task.due_date }); }; const handleCancelEdit = () => { setEditingTaskId(null); setEditedTask({}); }; const handleSaveEdit = async (taskId: number) => { try { const response = await api.patch(`/tasks/${taskId}`, editedTask); setTasks(tasks.map((t) => (t.id === taskId ? response.data : t))); setEditingTaskId(null); setEditedTask({}); } catch (error) { console.error("Błąd podczas edycji zadania:", error); } }; return (

Twoje zadania

{/* Form to add a task */}
setNewTask({ ...newTask, title: e.target.value })} className="border p-2 mr-2" /> setNewTask({ ...newTask, description: e.target.value })} className="border p-2 mr-2" /> setNewTask({ ...newTask, due_date: e.target.value })} className="border p-2 mr-2" />
{/* 🔹 Task list */} {tasks.length === 0 ? (

Brak zadań.

) : (
    {tasks.map((task) => (
  • {editingTaskId === task.id ? ( <> setEditedTask({ ...editedTask, title: e.target.value })} className="border p-1 mb-1 w-full" />