This repository has been archived by the owner on Jun 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 583
/
app.js
65 lines (57 loc) · 1.89 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*
Instructions:
(1) Rewrite get with the Fetch API: https://davidwalsh.name/fetch
(2) Finish the getJSON method. getJSON should take a URL and return the parsed JSON response.
(a) getJSON needs to return a Promise!
(3) Test by console.logging the response and by passing the query string from getJSON to addSearchHeader.
(4) Handle errors by passing "unknown" to addSearchHeader.
*/
// Inline configuration for jshint below. Prevents `gulp jshint` from failing with quiz starter code.
/* jshint unused: false */
(function(document) {
'use strict';
var home = null;
/**
* Helper function to show the search query.
* @param {String} query - The search query.
*/
function addSearchHeader(query) {
home.innerHTML = '<h2 class="page-title">query: ' + query + '</h2>';
}
/**
* XHR wrapped in a Promise.
* @param {String} url - The URL to fetch.
* @return {Promise} - A Promise that resolves when the XHR succeeds and fails otherwise.
*/
function get(url) {
return fetch(url, {
method: 'get'
});
}
/**
* Performs an XHR for a JSON and returns a parsed JSON response.
* @param {String} url - The JSON URL to fetch.
* @return {Promise} - A promise that passes the parsed JSON response.
*/
function getJSON(url) {
return get(url).then(function(response) {
// Handle network errors
if (!response.ok) {
throw Error(response.statusText ? response.statusText : 'Unknown network error')
}
return response.json();
});
}
window.addEventListener('WebComponentsReady', function() {
home = document.querySelector('section[data-route="home"]');
getJSON('../data/earth-like-results.json')
.then(function(response) {
addSearchHeader(response.query);
console.log(response);
})
.catch(function(error) {
addSearchHeader('unknown');
console.log(error);
});
});
})(document);