Skip to content

Commit 66a211e

Browse files
committed
added exception handler in case there is no such notes in list
1 parent 3bb45cf commit 66a211e

File tree

1 file changed

+32
-10
lines changed

1 file changed

+32
-10
lines changed

scripts/HTMLBuilder.js

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,32 @@ function createNote(note){
2222
showAnnouncer('Note created successfully!');
2323
}
2424
function updateNote(note){
25-
notes.splice(notes.findIndex(n => n.id === note.id),1, note);
26-
refreshTables();
27-
showAnnouncer('Note updated successfully!');
25+
try{
26+
let index = notes.findIndex(n => n.id === note.id);
27+
if (index < 0)
28+
throw "There is no such note!";
29+
notes.splice(index,1, note);
30+
refreshTables();
31+
showAnnouncer('Note updated successfully!');
32+
} catch (e){
33+
console.error(e);
34+
showAnnouncer("Note wasn't updated!", true);
35+
}
2836
}
2937
function deleteNote(noteID){
30-
notes.splice(notes.indexOf(notes.find(note => note.id === noteID)),1);
31-
document.getElementById(noteID).remove();
32-
clearInnerHTML(statisticsTable);
33-
buildStatisticTable();
34-
showAnnouncer('Note deleted successfully!');
38+
try {
39+
let index = notes.findIndex(n => n.id === noteID);
40+
if (index < 0)
41+
throw "There is no such note!";
42+
notes.splice(index,1);
43+
document.getElementById(noteID).remove();
44+
clearInnerHTML(statisticsTable);
45+
buildStatisticTable();
46+
showAnnouncer('Note deleted successfully!');
47+
} catch (e){
48+
console.error(e);
49+
showAnnouncer("Note wasn't updated!", true);
50+
}
3551
}
3652
function changeArchiveState(note){
3753
notes[notes.findIndex(n => n.id === note.id)].archived = !notes[notes.findIndex(n => n.id === note.id)].archived;
@@ -40,11 +56,17 @@ function changeArchiveState(note){
4056
}
4157

4258
//announcer for users activity
43-
function showAnnouncer(text){
59+
function showAnnouncer(text, error){
4460
let announcer = document.getElementById('announcer')
4561
announcer.style.opacity = '1';
4662
announcer.innerText = text;
47-
setTimeout(()=>{ announcer.style.opacity = '0' }, 1500);
63+
if (error)
64+
announcer.classList.add('invalid-input');
65+
setTimeout(()=>{
66+
announcer.style.opacity = '0';
67+
if (error)
68+
announcer.classList.remove('invalid-input');
69+
}, 1500);
4870
}
4971

5072
function buildForm(note){

0 commit comments

Comments
 (0)