-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvillagers.js
132 lines (104 loc) · 5.26 KB
/
villagers.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
const apiurl = 'https://acnhapi.com/v1/villagers/';
var villagersList = [];
var selectedVillagerList = []; //list for certain villagers based on search requirements
var personality = ["Uchi", "Jock", "Normal", "Peppy", "Smug", "Snooty", "Lazy", "Cranky"];
var myVillagers; //list for villagers in local cache
//insert all villagers upon loading
for(var i = 1; i <= 391; i++) {
var api = "https://acnhapi.com/v1/villagers/".concat(i);
insertVillagers(api);
}
window.onload = function() {
//create all buttons for each personality type
for(var i = 0; i < personality.length; i++) {
var button = document.createElement("button");
button.innerHTML = personality[i];
document.getElementById('buttons').appendChild(button);
button.value = personality[i];
button.addEventListener("click", function() {
console.log(this.value);
selectedVillagerList = [];
document.getElementById('main').innerHTML = "";
for(var i = 0; i < villagersList.length; i++) {
if(villagersList[i].personality === this.value) {
console.log("pushing villager");
selectedVillagerList.push(villagersList[i].id); //push the villager ID to the selectedList
}
}
//fetch API with selectedList entries as URL query and append villagers to main
for(var i = 0; i < selectedVillagerList.length; i++) {
var api = "https://acnhapi.com/v1/villagers/".concat(selectedVillagerList[i]);
insertVillagers(api);
}
}); //end button eventListener
}//end personality for-loop
document.getElementById('all-button').addEventListener("click", function() {
document.getElementById('main').innerHTML = "";
for(var i = 1; i <= 391; i++) {
insertVillagers("https://acnhapi.com/v1/villagers/".concat(i));
}
})
//search by name
var searchBtn = document.getElementById('search_button');
var search = document.getElementById('search');
searchBtn.addEventListener("click", function(){
document.getElementById('main').innerHTML = "";
selectedVillagerList = [];
console.log(search.value);
for(var i = 0; i < villagersList.length; i++) {
if((villagersList[i].name["name-USen"]).includes(search.value)) {
selectedVillagerList.push(villagersList[i].id);
}
}
console.table(selectedVillagerList);
for(var i = 0; i < selectedVillagerList.length; i++) {
var api = "https://acnhapi.com/v1/villagers/".concat(selectedVillagerList[i]);
insertVillagers(api);
}
});
selectedVillagerList = [];
}//end window.onload
function insertVillagers(api) {
fetch(api)
.then(res => res.json())
.then(data => {
villagersList.push(data);
//create the necessary HTML objects for each villager
var main = document.getElementById('main');
var villager = document.createElement('div');
villager.classList.add("villager");
var img = document.createElement("img");
var name = document.createElement('div');
name.classList.add("name");
//set the name and image of villager
name.innerHTML = data.name["name-USen"];
img.src = data.image_uri;
//append the name and image to the villager element
villager.appendChild(img);
villager.appendChild(name);
//append the villager to the main area of the page
main.appendChild(villager);
//display infor when hovering
let hoverData = data.name["name-USen"];
img.title = hoverData;
img.onclick = function() {
console.log("image is clicked");
document.getElementById('name').innerHTML ="Name: " + data.name["name-USen"];
document.getElementById('personality').innerHTML = "Personality: " + data.personality;
console.log(data.personality);
document.getElementById('birthday').innerHTML = "Birthday: " + data["birthday-string"];
document.getElementById('gender').innerHTML = "Gender: " + data.gender;
document.getElementById("add").onclick = function() {
myVillagers = (localStorage.hasOwnProperty("myVillagers")) ? JSON.parse(localStorage.myVillagers) : [];
localStorage.setItem("myVillagers", myVillagers);
if(!localStorage.myVillagers.includes(data.id) && myVillagers.length < 10) {
myVillagers.push(data.id);
console.log(data.id);
localStorage.setItem("myVillagers", JSON.stringify(myVillagers));
} else {
localStorage.setItem("myVillagers", "[" + localStorage.myVillagers + "]");
console.log("already in or limit exceeded") }
}
};
});//end api fetch;
}