From 8964a9b29b63fdd08b097a49547b155b1b1bf43f Mon Sep 17 00:00:00 2001 From: RKWojs Date: Thu, 18 Sep 2025 11:11:22 +0200 Subject: [PATCH] feat: Enhance note creation and project cancellation with user information and translations --- src/app/api/notes/route.js | 16 +++++++++++++++- src/app/api/projects/[id]/route.js | 4 +++- src/app/projects/[id]/page.js | 7 ++++++- src/components/NoteForm.js | 18 ++++++++++++++++-- src/lib/queries/tasks.js | 5 ++++- src/lib/serverTranslations.js | 8 ++++++++ 6 files changed, 52 insertions(+), 6 deletions(-) diff --git a/src/app/api/notes/route.js b/src/app/api/notes/route.js index cd75f37..0a0b6ae 100644 --- a/src/app/api/notes/route.js +++ b/src/app/api/notes/route.js @@ -73,6 +73,20 @@ async function createNoteHandler(req) { ) .run(project_id || null, task_id || null, note, req.user?.id || null); + // Get the created note with user info + const createdNote = db + .prepare( + ` + SELECT n.*, + u.name as created_by_name, + u.username as created_by_username + FROM notes n + LEFT JOIN users u ON n.created_by = u.id + WHERE n.note_id = ? + ` + ) + .get(result.lastInsertRowid); + // Log note creation await logApiActionSafe( req, @@ -85,7 +99,7 @@ async function createNoteHandler(req) { } ); - return NextResponse.json({ success: true }); + return NextResponse.json(createdNote); } catch (error) { console.error("Error creating note:", error); return NextResponse.json( diff --git a/src/app/api/projects/[id]/route.js b/src/app/api/projects/[id]/route.js index ba23e47..cbe85e6 100644 --- a/src/app/api/projects/[id]/route.js +++ b/src/app/api/projects/[id]/route.js @@ -17,6 +17,7 @@ import { AUDIT_ACTIONS, RESOURCE_TYPES, } from "@/lib/auditLogSafe.js"; +import { getUserLanguage, serverT } from "@/lib/serverTranslations"; // Make sure the DB is initialized before queries run initializeDatabase(); @@ -86,7 +87,8 @@ async function updateProjectHandler(req, { params }) { minute: '2-digit' }); - const cancellationNote = `Projekt został wycofany w dniu ${cancellationDate}`; + const language = getUserLanguage(); + const cancellationNote = `${serverT("Project cancelled on", language)} ${cancellationDate}`; try { addNoteToProject(parseInt(id), cancellationNote, userId, true); // true for is_system diff --git a/src/app/projects/[id]/page.js b/src/app/projects/[id]/page.js index 30c68e3..1211c3a 100644 --- a/src/app/projects/[id]/page.js +++ b/src/app/projects/[id]/page.js @@ -24,6 +24,11 @@ export default function ProjectViewPage() { const [notes, setNotes] = useState([]); const [loading, setLoading] = useState(true); + // Helper function to add a new note to the list + const addNote = (newNote) => { + setNotes(prevNotes => [newNote, ...prevNotes]); + }; + // Helper function to check if user can delete a note const canDeleteNote = (note) => { if (!session?.user) return false; @@ -597,7 +602,7 @@ export default function ProjectViewPage() {
- +
{notes.length === 0 ? (
diff --git a/src/components/NoteForm.js b/src/components/NoteForm.js index fc613ad..b3db5ae 100644 --- a/src/components/NoteForm.js +++ b/src/components/NoteForm.js @@ -3,7 +3,7 @@ import React, { useState } from "react"; import { useTranslation } from "@/lib/i18n"; -export default function NoteForm({ projectId }) { +export default function NoteForm({ projectId, onNoteAdded }) { const { t } = useTranslation(); const [note, setNote] = useState(""); const [status, setStatus] = useState(null); @@ -18,19 +18,33 @@ export default function NoteForm({ projectId }) { }); if (res.ok) { + const newNote = await res.json(); setNote(""); setStatus(t("common.addNoteSuccess")); - window.location.reload(); + // Call the callback to add the new note to the parent component's state + if (onNoteAdded) { + onNoteAdded(newNote); + } + // Clear status message after 3 seconds + setTimeout(() => setStatus(null), 3000); } else { setStatus(t("common.addNoteError")); } } + function handleKeyDown(e) { + if (e.ctrlKey && e.key === 'Enter') { + e.preventDefault(); + handleSubmit(e); + } + } + return (