-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmain.js
More file actions
109 lines (100 loc) · 4.18 KB
/
main.js
File metadata and controls
109 lines (100 loc) · 4.18 KB
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
const MEETUP_ISSUE_ID = 14;
fetch('https://api.github.com/repos/devfsa/vagas/issues')
.then(response => {
response.json()
.then(data => {
const jobs = data.map(d => {
const splitTag = /\s+(?:n[ao]|at)\s+/i;
const tags = d.title.split(splitTag)[0].split('] ');
const body = splitTag.test(d.title) ? d.title.split(splitTag)[1] + ' - ' : '';
const title = tags[tags.length - 1];
const city = tags[0].replace('[', '');
const labels = d.labels.reduce((l, c) => l + ' - ' + c.name, '');
return {
url: d.html_url,
body,
title,
labels: d.labels,
city,
description: body + city + labels
};
});
const jobsHTML = jobs.slice(0, 4)
.reduce((html, job) => {
const { title, url, description } = job;
return [
html,
'<a href="', url, '" class="card-item" target="_blank">',
'<p class="card-item-title">', title, '</p>',
'<p class="card-item-description">', description, '</p>',
'</a>',
].join('');
}, '');
document.getElementById("jobs").insertAdjacentHTML('beforeend', jobsHTML);
});
})
.catch(err => {
console.log('Fetch Error:', err);
});
fetch(`https://api.github.com/repos/devfsa/meetups/issues/${MEETUP_ISSUE_ID}/comments`, { headers: { "Accept": "text" } })
.then(response => {
response.json()
.then(data => {
const reactionsFetchHeader = { "Accept": "application/vnd.github.squirrel-girl-preview+json" };
// will wait all fetch from comments' reactions
Promise.all(
data.map(d => {
const keynote = {
url: d.url,
html_url: d.html_url,
user: d.user,
body: d.body,
title: d.body.split('\r\n')[0].replace(/\*\*/g, '')
};
// will transform array of data into array of promises that returns data
return fetch(`${keynote.url}/reactions`, { headers: reactionsFetchHeader })
.then(res => res.json())
.then(reactions => Object.assign({}, keynote, { reactions: reactions || [] }))
.catch(err => {
console.log(`Error fetching reactions of ${keynote.title} // ${keynote.url}`);
console.error(err);
return Object.assign({}, keynote, { reactions: [] });
});
})
).then(keynotes => {
// after fetching all comments' reactions, sort it and then slice it
const notes = keynotes.sort((k1, k2) => k2.reactions.length - k1.reactions.length).slice(0, 4);
const keynotesHTML = notes.reduce((html, keynote, i) => {
const { user, html_url, body, title, reactions } = keynote;
return [
html,
'<div class="card keynote-card">',
'<div class="keynote-image">',
'<img src="', user.avatar_url, '" class="keynote-photo" />',
'</div>',
'<div class="keynote-desc">',
'<a href="', html_url, '" class="keynote-title" target="_blank">', title, '</a>',
'<div class="keynote-info">',
'<p class="keynote-author">@', user.login, '</p>',
'<a href="', html_url, '" class="keynote-vote ', i === 0 ? 'voted' : '', '" target="_blank">',
'<i class="far fa-arrow-alt-circle-up"></i>',
'<span class="keynote-vote-number"> ', reactions.length, '</span>',
'</a>',
'</div>',
'</div>',
'</div>',
].join('');
}, '');
document.getElementById("keynotes").insertAdjacentHTML('beforeend', keynotesHTML);
});
});
})
.catch(err => {
console.log('Fetch Error:', err);
});
window.onload = (ev) => {
let submissionDOM = document.getElementById('submission');
let seeallDom = document.getElementById('seeall');
submissionDOM.href=`https://github.com/devfsa/meetups/issues/${MEETUP_ISSUE_ID}`;
seeallDom.href=`https://github.com/devfsa/meetups/issues/${MEETUP_ISSUE_ID}`;
}