@@ -11,11 +11,11 @@ var xhrRequest = function(url, callback) {
11
11
if ( xhr . readyState === 4 ) {
12
12
loader . classList . remove ( "visible" ) ;
13
13
loader . classList . add ( "hidden" ) ;
14
- if ( xhr . status === 200 ) {
14
+ if ( xhr . status === 200 ) {
15
15
var results = JSON . parse ( xhr . responseText ) ;
16
16
callback ( null , results ) ;
17
17
} else {
18
- callback ( ' error' ) ;
18
+ callback ( " error" ) ;
19
19
}
20
20
}
21
21
} ;
@@ -29,8 +29,10 @@ xhrRequest("/latest", displayResults);
29
29
//XHR request to display news matching user input
30
30
button . addEventListener ( "click" , function ( e ) {
31
31
e . preventDefault ( ) ;
32
+ loader . classList . remove ( "hidden" ) ;
33
+ loader . classList . add ( "visible" ) ;
32
34
var q = input . value . toLowerCase ( ) . trim ( ) ;
33
- if ( q === '' ) {
35
+ if ( q === "" ) {
34
36
input . style . boxShadow = "0 0 10px red" ;
35
37
input . placeholder = "Please type some text" ;
36
38
} else {
@@ -47,38 +49,43 @@ function displayResults(error, articles) {
47
49
if ( error ) {
48
50
console . log ( error , "something went wrong" ) ;
49
51
sectionResults . style . display = "block" ;
50
- sectionResults . innerHTML = "<h3>Something went wrong from our part. <br> Please try again later</h3>" ;
52
+ sectionResults . innerHTML =
53
+ "<h3>Something went wrong from our part. <br> Please try again later</h3>" ;
51
54
return ;
52
55
} else {
53
- if ( articles . length === 0 ) {
54
- sectionResults . style . display = "block" ;
55
- sectionResults . innerHTML = '<h3>No results found! <br> Please try another search query</h3>' ;
56
- } else {
57
- sectionResults . style . display = "grid" ;
58
- for ( var i = 0 ; i < articles . length ; i ++ ) {
59
- if ( articles [ i ] . urlToImage && articles [ i ] . urlToImage . startsWith ( "http" ) ) {
60
- var newsArticle = document . createElement ( "article" ) ;
61
- newsArticle . className = "article" ;
62
- var newsTitle = document . createElement ( "h2" ) ;
63
- newsTitle . className = "article__title" ;
64
- var newsImage = document . createElement ( "img" ) ;
65
- newsImage . className = "article__img" ;
66
- var newsDesc = document . createElement ( "p" ) ;
67
- newsDesc . className = "article__p" ;
68
- var link = document . createElement ( "a" ) ;
56
+ if ( articles . length === 0 ) {
57
+ sectionResults . style . display = "block" ;
58
+ sectionResults . innerHTML =
59
+ "<h3>No results found! <br> Please try another search query</h3>" ;
60
+ } else {
61
+ sectionResults . style . display = "grid" ;
62
+ for ( var i = 0 ; i < articles . length ; i ++ ) {
63
+ if (
64
+ articles [ i ] . urlToImage &&
65
+ articles [ i ] . urlToImage . startsWith ( "http" )
66
+ ) {
67
+ var newsArticle = document . createElement ( "article" ) ;
68
+ newsArticle . className = "article" ;
69
+ var newsTitle = document . createElement ( "h2" ) ;
70
+ newsTitle . className = "article__title" ;
71
+ var newsImage = document . createElement ( "img" ) ;
72
+ newsImage . className = "article__img" ;
73
+ var newsDesc = document . createElement ( "p" ) ;
74
+ newsDesc . className = "article__p" ;
75
+ var link = document . createElement ( "a" ) ;
69
76
70
- newsTitle . innerText = articles [ i ] . title ;
71
- newsImage . src = articles [ i ] . urlToImage ;
72
- newsImage . alt = articles [ i ] . title ;
73
- link . href = articles [ i ] . url ;
74
- link . title = "Click to go to the source" ;
75
- link . target = "_blank" ;
76
- newsDesc . innerText = articles [ i ] . description ;
77
- newsArticle . appendChild ( newsImage ) ;
78
- newsArticle . appendChild ( newsTitle ) ;
79
- newsArticle . appendChild ( newsDesc ) ;
80
- link . appendChild ( newsArticle ) ;
81
- sectionResults . appendChild ( link ) ;
77
+ newsTitle . innerText = articles [ i ] . title ;
78
+ newsImage . src = articles [ i ] . urlToImage ;
79
+ newsImage . alt = articles [ i ] . title ;
80
+ link . href = articles [ i ] . url ;
81
+ link . title = "Click to go to the source" ;
82
+ link . target = "_blank" ;
83
+ newsDesc . innerText = articles [ i ] . description ;
84
+ newsArticle . appendChild ( newsImage ) ;
85
+ newsArticle . appendChild ( newsTitle ) ;
86
+ newsArticle . appendChild ( newsDesc ) ;
87
+ link . appendChild ( newsArticle ) ;
88
+ sectionResults . appendChild ( link ) ;
82
89
}
83
90
}
84
91
}
0 commit comments