-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
145 lines (122 loc) · 4.66 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
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
140
141
142
143
144
145
let countrylist = [];
let localcountries;
let filteredcountrylist = [];
let enabledSettings = [];
let AfricaList = [];
let AmericasList = [];
let AsiaList = [];
let EuropeList = [];
let OceaniaList = [];
const apiURL ='http://api.countrylayer.com/v2/all?access_key=4e65e880be2cb3ca68403e03fb82c7f0'; //logan's API key
// const apiURL ='http://api.countrylayer.com/v2/all?access_key=578f857d4210a11edcda5243a2d175ed'; //Amber and Emerald's API key
const answerCover = document.getElementById('answer-cover');
const settings = document.getElementById('btn-settings');
const modal = document.getElementById('modal');
const closeButton = document.getElementById('btn-close');
const checkboxElements = document.querySelectorAll("input[type=checkbox]");
const nextButton = document.getElementById('next');
// EVENT LISTENERS //
//onclick - Get next country
nextButton.addEventListener('click', () => {
newCountry();
})
//onclick - Reveal the answer
answerCover.addEventListener('click', () => {
answerCover.classList.add('d-none');
})
//onclick - Show the settings modal
settings.addEventListener('click', () => {
modal.classList.toggle('d-block');
})
//onclick - Hide the settings modal
closeButton.addEventListener('click', () => {
modal.classList.toggle('d-block');
})
//onlcick - Update the enabled settings checkboxes for filtering countries by region
for (var i = 0; i < checkboxElements.length; i++) {
checkboxElements[i].addEventListener('change', (e) => {
getEnabledSettings();
})
}
function newCountry() {
answerCover.classList.remove('d-none');
const country = filteredcountrylist[Math.floor(Math.random() * filteredcountrylist.length)]
countryname = document.getElementById('countryname');
countryname.innerHTML = "";
countryname.innerHTML = country.name;
answer = document.getElementById('answer');
answer.innerHTML = "";
answer.innerHTML = country.capital;
}
// Removed this function because the free tier of api wont accept https requests and chrome wont process http requests
// async function getCountries(apiURL) {
// //Check if a list of counrties is already stored in Local storage. If not, fetch list via API
// if (localStorage.getItem("localcountries") === null) {
// try {
// const response = await fetch(apiURL)
// const rawlist = await response.json();
// localcountries = await rawlist.filter(country => country.capital != "" )
// localStorage.setItem("localcountries", JSON.stringify(localcountries));
// countrylist = [...localcountries];
// } catch (error) {
// console.log("Sorry there was an error: ", error)
// }
// } else {
// localcountries = JSON.parse(localStorage.getItem("localcountries"))
// countrylist = [...localcountries];
// }
// }
async function getCountries() {
const response = await fetch('data.json')
const rawlist = await response.json();
localcountries = await rawlist.filter(country => country.capital != "" );
countrylist = [...localcountries];
}
function getEnabledSettings() {
getRegions = [...document.getElementsByClassName('region')];
enabledSettings = [];
getRegions.forEach(element => {
if (element.checked) {
enabledSettings.push(element.name);
}
});
if (enabledSettings.length == 0) {
swal({
icon: '',
title: 'WAKANDA FOREVER!',
text: 'At least one region must be selected.',
button: {
className: "btn-swal",
text: "",
background: "none"
}
})
let wakanda = document.getElementById('Africa');
wakanda.checked = true;
enabledSettings = ['Africa'];
filteredcountrylist = AfricaList;
}
else {
filteredcountrylist = []
enabledSettings.forEach(element => {
ref = eval(element+"List");
filteredcountrylist.push(ref);
});
flattenedArray = filteredcountrylist.flat(2);
filteredcountrylist = [...flattenedArray];
}
}
function generateRegionalLists() {
AfricaList = countrylist.filter(country => country.region == "Africa" );
AmericasList = countrylist.filter(country => country.region == "Americas" );
AsiaList = countrylist.filter(country => country.region == "Asia" );
EuropeList = countrylist.filter(country => country.region == "Europe" );
OceaniaList = countrylist.filter(country => country.region == "Oceania" );
}
async function caller () {
await getCountries();
generateRegionalLists();
getEnabledSettings();
newCountry();
}
caller(apiURL)