-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp.js
97 lines (75 loc) · 2.01 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
var database = firebase.database();
var userID = window.location.search.match(/\?id=(.*)/)[1];
$(document).ready(function () {
$('input').val('');
database.ref("users/" + userID).once("value")
.then(function (snapshot) {
var userInfo = snapshot.val();
})
database.ref("users").once("value")
.then(function (snapshot) {
snapshot.forEach(function (childSnapshot) {
var childKey = childSnapshot.key;
var childData = childSnapshot.val();
createUsers(childData.email, childKey);
});
})
$('input').keyup(getSearch);
getGifsFromAPI();
$('#red-btn').click(getSearch);
$('#green-btn').click(likedGif);
$('#favorite').click(() => window.location = "favoritos.html?id=" + userID);
});
let indexOfGif = -1;
//chamando o Hammer.js
const myElement = document.getElementById('card-image');
const mc = new Hammer(myElement);
// listen to events...
mc.on("swipeleft", getSearch);
mc.on("swiperight", likedGif);
function getSearch(event) {
let searchItem = buscaPalavra();
if (!searchItem) {
getGifsFromAPI();
}
trazBusca(searchItem);
}
function trazBusca(searchItem) {
$.ajax({
type: 'GET',
url: `http://api.giphy.com/v1/gifs/search?q=${searchItem}&api_key=${keyAPI}&limit=100`,
success: showGif,
error: erro
});
}
function buscaPalavra(){
return document.getElementById("search").value;
}
function likedGif() {
getSearch();
addToFav()
}
function getGifsFromAPI() {
$.ajax({
type: 'GET',
url: `https://api.giphy.com/v1/gifs/search?q=gif&api_key=rc0vl8oEetDnA6wEuyjXXwtGB99EYxSS&limit=100 `,
success: showGif,
error: erro
});
}
function showGif(data) {
indexOfGif += 1;
urlImg = data['data'][indexOfGif]['images']['original']['url'];
$('img').attr('src', urlImg);
gifTitle = data['data'][indexOfGif]['title'];
$('#gif-title').html(gifTitle);
}
function addToFav() {
return database.ref("favorites/" + userID).push({
url: urlImg,
title: gifTitle
});
}
function erro() {
console.log('erro');
}