-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmyinfo.js
139 lines (114 loc) · 4.87 KB
/
myinfo.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
133
134
135
136
137
138
139
//console.log(JSON.parse(local));
var villagerAPI = 'https://acnhapi.com/v1/villagers/';
var bugsAPI = 'https://acnhapi.com/v1/bugs/';
var fishAPI = 'https://acnhapi.com/v1/fish/';
var seaAPI = 'https://acnhapi.com/v1/sea/';
window.onload = function() {
var villagers_serialized = JSON.parse(localStorage.myVillagers); //return an array (instead of string) of myVillager array in localStorage
var bugs_serialzied = JSON.parse(localStorage.myBugs);
var fish_serialized = JSON.parse(localStorage.myFish);
var sea_serialized = JSON.parse(localStorage.mySea);
for(var i = 0; i < villagers_serialized.length; i++) {
insertVillagers(villagerAPI.concat(villagers_serialized[i]));
}
for(var i = 0; i < bugs_serialzied.length; i++) {
insertBugs(bugsAPI.concat(bugs_serialzied[i]));
}
for(var i = 0; i < fish_serialized.length; i++) {
insertFish(fishAPI.concat(fish_serialized[i]));
}
for(var i = 0; i < sea_serialized.length; i++) {
insertSea(seaAPI.concat(sea_serialized[i]));
}
document.getElementById('reset-villagers').addEventListener("click", function() {
console.log('resetting villagers');
var emptyArray = [];
localStorage.setItem("myVillagers", JSON.stringify(emptyArray));
})
}
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('villagers');
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);
})
}
function insertBugs(api){
fetch(api)
.then(res => res.json())
.then(data => {
console.log(data.name);
var main = document.getElementById('bugs');
var bug = document.createElement('div');
bug.classList.add("bug");
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.icon_uri;
//append the name and image to the villager element
bug.appendChild(img);
// bug.appendChild(name);
//append the villager to the main area of the page
document.getElementById("bugs-box").appendChild(bug);
})
}
function insertFish(api){
fetch(api)
.then(res => res.json())
.then(data => {
console.log(data.name);
var main = document.getElementById('fish');
var fish = document.createElement('div');
fish.classList.add("bug");
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.icon_uri;
//append the name and image to the villager element
fish.appendChild(img);
//fish.appendChild(name);
//append the villager to the main area of the page
document.getElementById("fish-box").appendChild(fish);
})
}
function insertSea(api) {
fetch(api)
.then(res => res.json())
.then(data => {
//villagersList.push(data);
//create the necessary HTML objects for each villager
var main = document.getElementById('sea');
var sea = document.createElement('div');
sea.classList.add("sea");
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.icon_uri;
//append the name and image to the villager element
sea.appendChild(img);
//sea.appendChild(name);
//append the villager to the main area of the page
document.getElementById("sea-box").appendChild(sea);
})
}