@@ -22,16 +22,32 @@ function createNote(note){
22
22
showAnnouncer ( 'Note created successfully!' ) ;
23
23
}
24
24
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
+ }
28
36
}
29
37
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
+ }
35
51
}
36
52
function changeArchiveState ( note ) {
37
53
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){
40
56
}
41
57
42
58
//announcer for users activity
43
- function showAnnouncer ( text ) {
59
+ function showAnnouncer ( text , error ) {
44
60
let announcer = document . getElementById ( 'announcer' )
45
61
announcer . style . opacity = '1' ;
46
62
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 ) ;
48
70
}
49
71
50
72
function buildForm ( note ) {
0 commit comments