@@ -25,7 +25,15 @@ window.addEventListener('click', (e) => {
2525 }
2626} ) ;
2727
28- // Fetch all notes from the API
28+ /**
29+ * Fetch all notes from the API and display them in the UI
30+ * @async
31+ * @returns {Promise<void> }
32+ * @throws {Error } When API request fails or response is not ok
33+ * @example
34+ * // Automatically called on page load
35+ * await fetchNotes();
36+ */
2937async function fetchNotes ( ) {
3038 try {
3139 const response = await fetch ( API_URL ) ;
@@ -40,7 +48,19 @@ async function fetchNotes() {
4048 }
4149}
4250
43- // Display notes in the UI
51+ /**
52+ * Display notes in the UI container, handling empty state
53+ * @param {Object[] } notes - Array of note objects to display
54+ * @param {string } notes[].id - Unique identifier of the note
55+ * @param {string } notes[].title - Title of the note
56+ * @param {string } notes[].content - Content of the note
57+ * @returns {void }
58+ * @example
59+ * const notes = [
60+ * { id: '1', title: 'Sample Note', content: 'Sample content' }
61+ * ];
62+ * displayNotes(notes);
63+ */
4464function displayNotes ( notes ) {
4565 if ( notes . length === 0 ) {
4666 notesContainer . innerHTML = '<div class="no-notes">No notes found. Create your first note!</div>' ;
@@ -62,7 +82,13 @@ function displayNotes(notes) {
6282 notesContainer . addEventListener ( 'click' , handleNoteActions ) ;
6383}
6484
65- // Open modal for creating a new note
85+ /**
86+ * Open the modal dialog for creating a new note
87+ * @returns {void }
88+ * @example
89+ * // Called when user clicks "Create New Note" button
90+ * openCreateNoteModal();
91+ */
6692function openCreateNoteModal ( ) {
6793 modalTitle . textContent = 'Create New Note' ;
6894 noteIdInput . value = '' ;
@@ -71,7 +97,16 @@ function openCreateNoteModal() {
7197 noteModal . classList . add ( 'modal-visible' ) ;
7298}
7399
74- // Open modal for editing an existing note
100+ /**
101+ * Open the modal dialog for editing an existing note
102+ * @async
103+ * @param {string } id - The unique identifier of the note to edit
104+ * @returns {Promise<void> }
105+ * @throws {Error } When note fetch fails or note is not found
106+ * @example
107+ * // Called when user clicks "Edit" button on a note
108+ * await openEditNoteModal('note_123');
109+ */
75110async function openEditNoteModal ( id ) {
76111 try {
77112 const response = await fetch ( `${ API_URL } /${ id } ` ) ;
@@ -91,12 +126,27 @@ async function openEditNoteModal(id) {
91126 }
92127}
93128
94- // Close the modal
129+ /**
130+ * Close the modal dialog and hide it from view
131+ * @returns {void }
132+ * @example
133+ * // Called when user clicks close button or cancel
134+ * closeModal();
135+ */
95136function closeModal ( ) {
96137 noteModal . classList . remove ( 'modal-visible' ) ;
97138}
98139
99- // Handle form submission (create or update note)
140+ /**
141+ * Handle form submission for creating or updating a note
142+ * @async
143+ * @param {Event } e - The form submit event
144+ * @returns {Promise<void> }
145+ * @throws {Error } When note creation/update fails
146+ * @example
147+ * // Automatically called when form is submitted
148+ * noteForm.addEventListener('submit', handleFormSubmit);
149+ */
100150async function handleFormSubmit ( e ) {
101151 e . preventDefault ( ) ;
102152
@@ -137,7 +187,16 @@ async function handleFormSubmit(e) {
137187 }
138188}
139189
140- // Delete a note
190+ /**
191+ * Delete a note after user confirmation
192+ * @async
193+ * @param {string } id - The unique identifier of the note to delete
194+ * @returns {Promise<void> }
195+ * @throws {Error } When note deletion fails
196+ * @example
197+ * // Called when user clicks "Delete" button on a note
198+ * await deleteNote('note_123');
199+ */
141200async function deleteNote ( id ) {
142201 if ( ! confirm ( 'Are you sure you want to delete this note?' ) ) {
143202 return ;
@@ -159,7 +218,14 @@ async function deleteNote(id) {
159218 }
160219}
161220
162- // Handle clicks on note action buttons using event delegation
221+ /**
222+ * Handle clicks on note action buttons using event delegation
223+ * @param {Event } e - The click event on the notes container
224+ * @returns {void }
225+ * @example
226+ * // Automatically handles edit and delete button clicks
227+ * notesContainer.addEventListener('click', handleNoteActions);
228+ */
163229function handleNoteActions ( e ) {
164230 const target = e . target ;
165231
@@ -175,7 +241,14 @@ function handleNoteActions(e) {
175241 }
176242}
177243
178- // Helper function to escape HTML to prevent XSS
244+ /**
245+ * Escape HTML characters to prevent XSS attacks
246+ * @param {string } unsafe - The unsafe string that may contain HTML characters
247+ * @returns {string } The escaped string safe for HTML insertion
248+ * @example
249+ * const safe = escapeHtml('<script>alert("xss")</script>');
250+ * // Returns: "<script>alert("xss")</script>"
251+ */
179252function escapeHtml ( unsafe ) {
180253 return unsafe
181254 . replace ( / & / g, "&" )
0 commit comments