-
Notifications
You must be signed in to change notification settings - Fork 2
/
events.js
82 lines (76 loc) · 2.85 KB
/
events.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
function run() {
const scripts = document.querySelectorAll('script')
const script = scripts[scripts.length - 1]
const filter = script.getAttribute('data-filter')
const titleNext = script.getAttribute('data-title-next')
const also = script.getAttribute('data-also')
const titlePast = script.getAttribute('data-title-past')
const more = script.getAttribute('data-more')
const meetup = script.getAttribute('data-meetup')
showNext(meetup, titleNext, also, filter)
showPast(meetup, titlePast, more, filter)
}
function showNext(meetup, title, alsoText, filter) {
const limit = 3
fetchEvents(meetup, 'upcoming').then(eventsUnfiltered => {
const events = eventsUnfiltered.filter(event => event.name.includes(filter))
if (events.length === 0) return
const eventsShown = events.slice(1, limit + 1)
const dates = eventsShown.map((event, i) => {
const after = (i === eventsShown.length - 2) ? ' & '
: (i === eventsShown.length - 1) ? '.'
: ', '
return [
crel('a', { href: event.link }, toDate(event.time)),
after
]
})
const also = events.length <= 1 ? '' : crel('p', [
alsoText + ' ',
dates
])
crel(document.querySelector('.next-event-block'), [
crel('h3', title),
toBox(events[0]),
also
])
})
}
function showPast(meetup, title, moreText, filter) {
const limit = 4
fetchEvents(meetup, 'past').then(eventsUnfiltered => {
const events = eventsUnfiltered.filter(event => event.name.includes(filter))
if (events.length === 0) return
const items = events.reverse().slice(0, limit).map(event => {
return crel('li', toBox(event))
})
const also = events.length === 0 ? '' : crel('p', [
crel('a', { href: 'https://www.meetup.com/' + meetup + '/events/past' }, moreText)
])
crel(document.querySelector('.past-events-block'), [
crel('h3', title),
crel('ol', { 'class': 'past-events' }, items),
also
])
})
}
function fetchEvents(meetup, status) {
const location = 'https://api.meetup.com/' + meetup + '/events?status=' + status
return fetchJsonp(location)
.then(response => response.json())
.then(response => response.data)
}
function toBox(event) {
return crel('a', { 'class': 'event', href: event.link }, [
crel('time', toDate(event.time, true)),
crel('h4', event.name)
])
}
function toDate(timestamp, useLongFormat) {
const format = useLongFormat ? 'dddd Do MMMM YYYY h:mma' : 'Do MMMM YYYY'
const locale = document.querySelector('html').lang
moment.locale(locale)
const date = moment(timestamp)
return date.format(format).replace(':00', '')
}
run()