7
7
</ head >
8
8
9
9
< body class ="section ">
10
- < h1 > Notely</ h1 >
10
+ < h1 > Welcome to Notely</ h1 >
11
11
12
12
< div id ="userCreationContainer " class ="section ">
13
13
< input id ="nameField " type ="text " placeholder ="Enter your name ">
@@ -37,7 +37,7 @@ <h2>Your Notes</h2>
37
37
return ;
38
38
}
39
39
const noteContent = document . getElementById ( 'newNoteContent' ) . value ;
40
- const response = await fetch ( `${ API_BASE } /notes` ,
40
+ const response = await fetchWithAlert ( `${ API_BASE } /notes` ,
41
41
{
42
42
method : 'POST' , headers : { 'Content-Type' : 'application/json' , 'Authorization' : `ApiKey ${ currentUserAPIKey } ` } ,
43
43
body : JSON . stringify ( { note : noteContent } )
@@ -47,15 +47,15 @@ <h2>Your Notes</h2>
47
47
}
48
48
49
49
async function getUser ( ) {
50
- const response = await fetch ( `${ API_BASE } /users` , { headers : { 'Authorization' : `ApiKey ${ currentUserAPIKey } ` } } ) ;
50
+ const response = await fetchWithAlert ( `${ API_BASE } /users` , { headers : { 'Authorization' : `ApiKey ${ currentUserAPIKey } ` } } ) ;
51
51
return await response . json ( ) ;
52
52
}
53
53
54
54
async function loadNotes ( ) {
55
55
if ( ! currentUser ) {
56
56
return ;
57
57
}
58
- const response = await fetch ( `${ API_BASE } /notes` , { headers : { 'Authorization' : `ApiKey ${ currentUserAPIKey } ` } } ) ;
58
+ const response = await fetchWithAlert ( `${ API_BASE } /notes` , { headers : { 'Authorization' : `ApiKey ${ currentUserAPIKey } ` } } ) ;
59
59
const notes = await response . json ( ) ;
60
60
const notesContainer = document . getElementById ( 'notes' ) ;
61
61
notesContainer . innerHTML = '' ;
@@ -71,7 +71,7 @@ <h2>Your Notes</h2>
71
71
72
72
async function createUser ( ) {
73
73
const nameField = document . getElementById ( 'nameField' ) ;
74
- const response = await fetch ( `${ API_BASE } /users` , {
74
+ const response = await fetchWithAlert ( `${ API_BASE } /users` , {
75
75
method : 'POST' ,
76
76
headers : { 'Content-Type' : 'application/json' } ,
77
77
body : JSON . stringify ( { name : nameField . value } ) // using the value from nameField
@@ -113,6 +113,15 @@ <h2>Your Notes</h2>
113
113
document . getElementById ( 'greetingMessage' ) . textContent = `Hello ${ user . name } !` ;
114
114
}
115
115
116
+ async function fetchWithAlert ( url , options ) {
117
+ const response = await fetch ( url , options ) ;
118
+ if ( response . status > 299 ) {
119
+ alert ( `Error: ${ response . status } ` ) ;
120
+ return ;
121
+ }
122
+ return response ;
123
+ }
124
+
116
125
login ( ) ;
117
126
</ script >
118
127
0 commit comments