feat: Implement task notes functionality with CRUD operations and integrate into project tasks section
This commit is contained in:
@@ -9,12 +9,34 @@ import Badge from "./ui/Badge";
|
||||
export default function ProjectTasksSection({ projectId }) {
|
||||
const [projectTasks, setProjectTasks] = useState([]);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [taskNotes, setTaskNotes] = useState({});
|
||||
const [newNote, setNewNote] = useState({});
|
||||
const [loadingNotes, setLoadingNotes] = useState({});
|
||||
useEffect(() => {
|
||||
const fetchProjectTasks = async () => {
|
||||
try {
|
||||
const res = await fetch(`/api/project-tasks?project_id=${projectId}`);
|
||||
const tasks = await res.json();
|
||||
setProjectTasks(tasks);
|
||||
|
||||
// Fetch notes for each task
|
||||
const notesPromises = tasks.map(async (task) => {
|
||||
try {
|
||||
const notesRes = await fetch(`/api/task-notes?task_id=${task.id}`);
|
||||
const notes = await notesRes.json();
|
||||
return { taskId: task.id, notes };
|
||||
} catch (error) {
|
||||
console.error(`Failed to fetch notes for task ${task.id}:`, error);
|
||||
return { taskId: task.id, notes: [] };
|
||||
}
|
||||
});
|
||||
|
||||
const notesResults = await Promise.all(notesPromises);
|
||||
const notesMap = {};
|
||||
notesResults.forEach(({ taskId, notes }) => {
|
||||
notesMap[taskId] = notes;
|
||||
});
|
||||
setTaskNotes(notesMap);
|
||||
} catch (error) {
|
||||
console.error("Failed to fetch project tasks:", error);
|
||||
} finally {
|
||||
@@ -24,12 +46,30 @@ export default function ProjectTasksSection({ projectId }) {
|
||||
|
||||
fetchProjectTasks();
|
||||
}, [projectId]);
|
||||
|
||||
const refetchTasks = async () => {
|
||||
try {
|
||||
const res = await fetch(`/api/project-tasks?project_id=${projectId}`);
|
||||
const tasks = await res.json();
|
||||
setProjectTasks(tasks);
|
||||
|
||||
// Refresh notes for all tasks
|
||||
const notesPromises = tasks.map(async (task) => {
|
||||
try {
|
||||
const notesRes = await fetch(`/api/task-notes?task_id=${task.id}`);
|
||||
const notes = await notesRes.json();
|
||||
return { taskId: task.id, notes };
|
||||
} catch (error) {
|
||||
console.error(`Failed to fetch notes for task ${task.id}:`, error);
|
||||
return { taskId: task.id, notes: [] };
|
||||
}
|
||||
});
|
||||
|
||||
const notesResults = await Promise.all(notesPromises);
|
||||
const notesMap = {};
|
||||
notesResults.forEach(({ taskId, notes }) => {
|
||||
notesMap[taskId] = notes;
|
||||
});
|
||||
setTaskNotes(notesMap);
|
||||
} catch (error) {
|
||||
console.error("Failed to fetch project tasks:", error);
|
||||
}
|
||||
@@ -55,7 +95,6 @@ export default function ProjectTasksSection({ projectId }) {
|
||||
alert("Error updating task status");
|
||||
}
|
||||
};
|
||||
|
||||
const handleDeleteTask = async (taskId) => {
|
||||
if (!confirm("Are you sure you want to delete this task?")) return;
|
||||
try {
|
||||
@@ -72,6 +111,59 @@ export default function ProjectTasksSection({ projectId }) {
|
||||
alert("Error deleting task");
|
||||
}
|
||||
};
|
||||
|
||||
const handleAddNote = async (taskId) => {
|
||||
const noteContent = newNote[taskId]?.trim();
|
||||
if (!noteContent) return;
|
||||
|
||||
setLoadingNotes((prev) => ({ ...prev, [taskId]: true }));
|
||||
|
||||
try {
|
||||
const res = await fetch("/api/task-notes", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({
|
||||
task_id: taskId,
|
||||
note: noteContent,
|
||||
}),
|
||||
});
|
||||
|
||||
if (res.ok) {
|
||||
// Refresh notes for this task
|
||||
const notesRes = await fetch(`/api/task-notes?task_id=${taskId}`);
|
||||
const notes = await notesRes.json();
|
||||
setTaskNotes((prev) => ({ ...prev, [taskId]: notes }));
|
||||
setNewNote((prev) => ({ ...prev, [taskId]: "" }));
|
||||
} else {
|
||||
alert("Failed to add note");
|
||||
}
|
||||
} catch (error) {
|
||||
alert("Error adding note");
|
||||
} finally {
|
||||
setLoadingNotes((prev) => ({ ...prev, [taskId]: false }));
|
||||
}
|
||||
};
|
||||
|
||||
const handleDeleteNote = async (noteId, taskId) => {
|
||||
if (!confirm("Are you sure you want to delete this note?")) return;
|
||||
|
||||
try {
|
||||
const res = await fetch(`/api/task-notes?note_id=${noteId}`, {
|
||||
method: "DELETE",
|
||||
});
|
||||
|
||||
if (res.ok) {
|
||||
// Refresh notes for this task
|
||||
const notesRes = await fetch(`/api/task-notes?task_id=${taskId}`);
|
||||
const notes = await notesRes.json();
|
||||
setTaskNotes((prev) => ({ ...prev, [taskId]: notes }));
|
||||
} else {
|
||||
alert("Failed to delete note");
|
||||
}
|
||||
} catch (error) {
|
||||
alert("Error deleting note");
|
||||
}
|
||||
};
|
||||
const getPriorityVariant = (priority) => {
|
||||
switch (priority) {
|
||||
case "urgent":
|
||||
@@ -177,8 +269,15 @@ export default function ProjectTasksSection({ projectId }) {
|
||||
>
|
||||
{task.priority}
|
||||
</Badge>
|
||||
</div>
|
||||
|
||||
</div>{" "}
|
||||
{/* Task Description */}
|
||||
{task.description && (
|
||||
<div className="mb-4">
|
||||
<p className="text-sm text-gray-700 bg-gray-50 p-3 rounded-md">
|
||||
{task.description}
|
||||
</p>
|
||||
</div>
|
||||
)}
|
||||
<div className="grid grid-cols-1 sm:grid-cols-3 gap-3 text-sm text-gray-600 mb-4">
|
||||
<div>
|
||||
<span className="font-medium">Max wait days:</span>{" "}
|
||||
@@ -193,8 +292,7 @@ export default function ProjectTasksSection({ projectId }) {
|
||||
{new Date(task.date_added).toLocaleDateString()}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="flex items-center gap-3">
|
||||
<div className="flex items-center gap-3 mb-4">
|
||||
<span className="text-sm font-medium text-gray-700">
|
||||
Status:
|
||||
</span>
|
||||
@@ -217,6 +315,80 @@ export default function ProjectTasksSection({ projectId }) {
|
||||
{task.status.replace("_", " ")}
|
||||
</Badge>
|
||||
</div>
|
||||
{/* Notes Section */}
|
||||
<div className="border-t pt-4">
|
||||
<h5 className="text-sm font-medium text-gray-900 mb-3">
|
||||
Notes ({taskNotes[task.id]?.length || 0})
|
||||
</h5>
|
||||
|
||||
{/* Existing Notes */}
|
||||
{taskNotes[task.id] &&
|
||||
taskNotes[task.id].length > 0 && (
|
||||
<div className="space-y-2 mb-3">
|
||||
{taskNotes[task.id].map((note) => (
|
||||
<div
|
||||
key={note.note_id}
|
||||
className="bg-blue-50 p-3 rounded-md flex justify-between items-start"
|
||||
>
|
||||
<div className="flex-1">
|
||||
<p className="text-sm text-gray-800">
|
||||
{note.note}
|
||||
</p>
|
||||
<p className="text-xs text-gray-500 mt-1">
|
||||
{new Date(
|
||||
note.note_date
|
||||
).toLocaleDateString()}{" "}
|
||||
at{" "}
|
||||
{new Date(
|
||||
note.note_date
|
||||
).toLocaleTimeString()}
|
||||
</p>
|
||||
</div>
|
||||
<button
|
||||
onClick={() =>
|
||||
handleDeleteNote(note.note_id, task.id)
|
||||
}
|
||||
className="ml-2 text-red-500 hover:text-red-700 text-xs font-bold"
|
||||
title="Delete note"
|
||||
>
|
||||
×
|
||||
</button>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Add New Note */}
|
||||
<div className="flex gap-2">
|
||||
<input
|
||||
type="text"
|
||||
placeholder="Add a note..."
|
||||
value={newNote[task.id] || ""}
|
||||
onChange={(e) =>
|
||||
setNewNote((prev) => ({
|
||||
...prev,
|
||||
[task.id]: e.target.value,
|
||||
}))
|
||||
}
|
||||
className="flex-1 px-3 py-1.5 text-sm border border-gray-300 rounded-md focus:ring-2 focus:ring-blue-500 focus:border-blue-500"
|
||||
onKeyPress={(e) => {
|
||||
if (e.key === "Enter") {
|
||||
handleAddNote(task.id);
|
||||
}
|
||||
}}
|
||||
/>
|
||||
<Button
|
||||
size="sm"
|
||||
variant="primary"
|
||||
onClick={() => handleAddNote(task.id)}
|
||||
disabled={
|
||||
loadingNotes[task.id] || !newNote[task.id]?.trim()
|
||||
}
|
||||
>
|
||||
{loadingNotes[task.id] ? "Adding..." : "Add"}
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="ml-4 flex-shrink-0">
|
||||
|
||||
Reference in New Issue
Block a user