forked from LaunchCode-Education-Archived/the-ajaxson-5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequest-gif.js
68 lines (53 loc) · 2.59 KB
/
request-gif.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
66
67
68
$(document).ready(function() {
// register our function as the "callback" to be triggered by the form's submission event
$("#form-gif-request").submit(fetchAndDisplayGif); // in other words, when the form is submitted, fetchAndDisplayGif() will be executed
});
/**
* sends an asynchronous request to Giphy.com aksing for a random GIF using the
* user's search term (along with "jackson 5")
*
* upon receiving a response from Giphy, updates the DOM to display the new GIF
*/
function fetchAndDisplayGif(event) {
// This prevents the form submission from doing what it normally does: send a request (which would cause our page to refresh).
// Because we will be making our own AJAX request, we dont need to send a normal request and we definitely don't want the page to refresh.
event.preventDefault();
// get the user's input text from the DOM
var searchQuery = ""; // TODO should be e.g. "dance"
// configure a few parameters to attach to our request
var params = {
api_key: "dc6zaTOxFJmzC",
tag : "" // TODO should be e.g. "jackson 5 dance"
};
// make an ajax request for a random GIF
$.ajax({
url: "", // TODO where should this request be sent?
data: params, // attach those extra parameters onto the request
success: function(response) {
// if the response comes back successfully, the code in here will execute.
// jQuery passes us the `response` variable, a regular javascript object created from the JSON the server gave us
console.log("we received a response!");
console.log(response);
// TODO
// 1. set the source attribute of our image to the image_url of the GIF
// 2. hide the feedback message and display the image
},
error: function() {
// if something went wrong, the code in here will execute instead of the success function
// give the user an error message
$("#feedback").text("Sorry, could not load GIF. Try again!");
setGifLoadedStatus(false);
}
});
// TODO
// give the user a "Loading..." message while they wait
}
/**
* toggles the visibility of UI elements based on whether a GIF is currently loaded.
* if the GIF is loaded: displays the image and hides the feedback label
* otherwise: hides the image and displays the feedback label
*/
function setGifLoadedStatus(isCurrentlyLoaded) {
$("#gif").attr("hidden", !isCurrentlyLoaded);
$("#feedback").attr("hidden", isCurrentlyLoaded);
}