-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscripts.js
81 lines (69 loc) · 3.12 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
const filterCategories = document.getElementById('filter-categories');
const filterCouncils = document.getElementById('filter-councils');
const results = document.getElementById('results');
fetch('./provedores.json')
.then(res => res.json())
.then(data => {
const councils = new Set();
const categories = new Set();
data.forEach(provider => {
if (provider.council) {
councils.add(provider.council);
}
provider.categories.forEach(category => categories.add(category));
});
filterCouncils.innerHTML += Array.from(councils).map(council => `<option>${council}</option>`).join('');
filterCategories.innerHTML += Array.from(categories).map(category => `<option>${category}</option>`).join('');
results.classList.add('providers');
results.innerHTML = '';
data.sort((a, b) => a.name < b.name ? -1 : 1);
data.forEach(provider => {
const element = document.createElement('article');
element.classList.add('provider');
element.innerHTML = `
<header>
<h1 class="provider-title">${
provider.web
? `<a class="provider-web" href="${provider.web}">${provider.name}</a>`
: provider.name
}</h1>
<nav>
<strong class="provider-council">${provider.council}</strong><br>
${provider.categories.map(cat => `<strong class="provider-category">${cat}</strong>`).join(', ')}
</nav>
</header>
<p class="provider-description">${provider.description || 'Sin descripción'}</p>
<p class="provider-address">${provider.address}</p>
`;
if (provider.phones) {
element.innerHTML += `
<p>Teléfono: ${
provider.phones.map(phone =>
`<a class="provider-phone" href="tel:${phone}">${phone}</a>`
).join(', ')
}</p>`;
}
if (provider.email) {
element.innerHTML += `<p>Email: <a class="provider-email" href="mailto:${provider.email}">${provider.email}</a></p>`;
}
provider.element = element;
results.append(element);
});
filterCouncils.addEventListener('change', filter)
filterCategories.addEventListener('change', filter)
function filter() {
const category = filterCategories.value;
const council = filterCouncils.value;
data.forEach(provider => {
if (council && provider.council && provider.council !== council) {
provider.element.hidden = true;
return;
}
if (category && !provider.categories.includes(category)) {
provider.element.hidden = true;
return;
}
provider.element.hidden = false;
})
}
})