-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscripts.js
116 lines (93 loc) · 2.88 KB
/
scripts.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
$(function($) {
// var apiUrl = "http://pokeapi.co/api/v2/pokemon/"
// var apiUrl = "http://localhost:8080/types?name=pikachu";
// var apiUrl = "http://localhost:8080/addFavorite?name=pikachu?user=8"
var apiUrl = "http://localhost:8080";
var $pokemonIdInput = $("#pokemonId");
var $pokemonsIdsInput = $("#pokemonsIds");
var $userIdInput = $("#userId");
var $getPokemonTypeByIdButton = $("#getPokemonTypeById");
var $addPokemonToFavButton = $("#addPokemonToFav");
var sourceFav = $("#pokemonFavTemplate").html();
var templateFav = Handlebars.compile(sourceFav);
var sourceType = $("#pokemonTypeTemplate").html();
var templateType = Handlebars.compile(sourceType);
var $pokemonInfo = $("#pokemonInfo");
function getPokemonTypes(id) {
$.ajax({
type: "GET",
url: apiUrl + "/types?name=" + id,
success: function(resp) {
$pokemonInfo.html(templateType({resp}));
},
error: function(e) {
console.log(e);
}
});
};
function getPokemonTypesJson(id) {
$.ajax({
dataType: 'json',
url: "http://localhost:8080/types?name=" + id, type: 'get', contentType: 'application/json',
data: JSON.stringify({}), success: function () {
$pokemonInfo.html(templateType({resp}));
},
error: function (e) {
console.log(e);
}
});
};
function userIdsToArray(args) {
return args.split(",");
};
$getPokemonTypeByIdButton.click(function() {
var id = parseInt($pokemonIdInput.val(), 10);
if (!id) {
alert("por favor, pon un numero de id en el campo");
return;
}
getPokemonTypes(id);
});
function postPokemonFavorites(pokemonsIds, userIdInput) {
var url =
apiUrl +
"/addFavorite?name=" + pokemonsIds + "&user=" + userIdInput;
$.ajax({
type: "POST",
url: url,
success: function(resp) {
$pokemonInfo.html(
templateFav({resp})
);
},
error: function(e) {
console.log(e);
}
});
};
function postPokemonFavoritesJson(pokemonsIds, userIdInput) {
$.post({
dataType: 'json',
url: "http://localhost:8080/addFavorite?name=" + pokemonsIds + "&user=" + userIdInput, type: 'post', contentType: 'application/json',
data: JSON.stringify( { }), success: function() {
$pokemonInfo.html(templateFav({resp}));
},
error: function(e) {
console.log(e);
}
});
};
$addPokemonToFavButton.click(function() {
var pokemonsIds = parseInt($pokemonsIdsInput.val(), 10);
var userIdInput = parseInt($userIdInput.val(), 10);
if (!pokemonsIds) {
alert("por favor, pon un numero de id en el campo pokemons Ids");
return;
}
if (!userIdInput) {
alert("por favor, pon un numero de id en el campo user Id");
return;
}
postPokemonFavorites(pokemonsIds, userIdInput);
});
});