import React, { useEffect, useState } from 'react'; import { useParams, Link, useNavigate } from 'react-router-dom'; import { PencilSquareIcon, TrashIcon, TagIcon, DocumentTextIcon } from '@heroicons/react/24/solid'; import ConfirmDialog from '../Shared/ConfirmDialog'; import NoteModal from './NoteModal'; import { Note } from '../../entities/Note'; import { fetchNotes, deleteNote as apiDeleteNote, updateNote as apiUpdateNote } from '../../utils/notesService'; const NoteDetails: React.FC = () => { const { id } = useParams<{ id: string }>(); const [note, setNote] = useState(null); const [isNoteModalOpen, setIsNoteModalOpen] = useState(false); const [isConfirmDialogOpen, setIsConfirmDialogOpen] = useState(false); const [noteToDelete, setNoteToDelete] = useState(null); const [isLoading, setIsLoading] = useState(true); const [isError, setIsError] = useState(false); const navigate = useNavigate(); useEffect(() => { const fetchNote = async () => { try { setIsLoading(true); const notes = await fetchNotes(); const foundNote = notes.find((n: Note) => n.id === Number(id)); setNote(foundNote || null); if (!foundNote) { setIsError(true); } } catch (err) { setIsError(true); console.error('Error fetching note:', err); } finally { setIsLoading(false); } }; fetchNote(); }, [id]); const handleDeleteNote = async () => { if (!noteToDelete) return; try { await apiDeleteNote(noteToDelete.id!); navigate('/notes'); } catch (err) { console.error('Error deleting note:', err); } }; const handleSaveNote = async (updatedNote: Note) => { try { if (updatedNote.id !== undefined) { await apiUpdateNote(updatedNote.id, updatedNote); setNote(updatedNote); } else { console.error("Error: Note ID is undefined."); } } catch (err) { console.error('Error saving note:', err); } setIsNoteModalOpen(false); }; const handleEditNote = () => { setIsNoteModalOpen(true); }; const handleOpenConfirmDialog = (note: Note) => { setNoteToDelete(note); setIsConfirmDialogOpen(true); }; if (isLoading) { return (
Loading note details...
); } if (isError || !note) { return (
{isError ? 'Error loading note details.' : 'Note not found.'}
); } return (
{/* Header Section with Title and Action Buttons */}

{note.title}

{/* Action Buttons */}
{/* Card with Tags and Metadata */}
{/* Note Tags */} {note.tags && note.tags.length > 0 && (
{note.tags.map((tag) => ( ))}
)} {/* Note Metadata */}

Created on: {new Date(note.created_at || '').toLocaleDateString()}

Last updated: {new Date(note.updated_at || '').toLocaleDateString()}

{/* Note Project */} {note.project && (

Project

{note.project.name}
)}
{/* Note Content */}

{note.content}

{/* NoteModal for editing */} {isNoteModalOpen && ( setIsNoteModalOpen(false)} onSave={handleSaveNote} note={note} /> )} {/* ConfirmDialog */} {isConfirmDialogOpen && noteToDelete && ( { setIsConfirmDialogOpen(false); setNoteToDelete(null); }} /> )}
); }; export default NoteDetails;