-
Notifications
You must be signed in to change notification settings - Fork 5
/
topanswers.js
84 lines (77 loc) · 3.27 KB
/
topanswers.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
window.addEventListener('load', async () => {
const htmlFromString = (html) => {
const el = document.createElement('template');
el.innerHTML = html;
return el.content.childNodes;
};
const updateList = (data, id) => {
let communityList = [
...document.querySelectorAll('.community-list .widget'),
].map((v) => v.getAttribute('data-name'));
data.forEach((site) => {
let template = [
`<div class="widget--body topanswers-site">
<img src="${site.logo_url}" height="32" width="32" alt />
<a href="${site.canonical_url}" target="_blank" rel="noopener noreferrer">${site.name}</a>
</div>`,
`<div class="grid--cell is-4 is-6-md is-12-sm">
<div class="widget">
<div class="widget--header is-complex">
<div class="has-text-align-center has-font-weight-bold has-font-size-display">
<a href="${site.canonical_url}"
style="display: flex; align-items: center; justify-content: center;">
<img src="${site.logo_url}" alt="Meta" style="height: 1.5em">
</a>
</div>
</div>
<div class="widget--body">
<p>${site.description}</p>
</div>
<div class="widget--footer">
<a href="${site.canonical_url}" class="button is-filled">Visit the site</a>
</div>
</div>
</div>`,
];
const elements = htmlFromString(template[id]);
elements.forEach((el) => {
id
? !communityList.includes(site.url_slug) &&
codidactList.prepend(el)
: topAnswersList.appendChild(el);
});
});
};
const topAnswersUrl = 'https://topanswers.xyz/communities.json';
const codidactUrl = '/communities.json';
const codidactList = document.querySelector('.community-list');
const topAnswersList = document.querySelector('.js-topanswers-list');
const codidactRespond = await fetch(codidactUrl, {
headers: {
Accept: 'application/json',
},
});
if (codidactRespond.status === 200) {
const data = await codidactRespond.json();
updateList(data, 1);
}
try {
const topAnsRespond = await fetch(topAnswersUrl, {
headers: {
Accept: 'application/json',
},
});
if (topAnsRespond.status === 200) {
const data = await topAnsRespond.json();
updateList(data, 0);
}
} catch (e) {
const error = `
<div class="notice is-danger">
<p>Couldn't load TopAnswers communities right now. Visit the <a href="https://topanswers.xyz/" target="_blank" rel="noopener noreferrer">TopAnswers homepage</a>
to see the communities available there.</p>
</div>`;
const el = htmlFromString(error);
topAnswersList.appendChild(el[0]);
}
});