-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
161 lines (138 loc) · 5.24 KB
/
script.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
document.addEventListener('DOMContentLoaded', () => {
const container = document.querySelector('.container');
const box = document.querySelector('.box');
const startButton = document.getElementById('startButton');
startButton.addEventListener('click', () => {
fadeOut(box, () => {
showActivityTypes();
});
});
function showActivityTypes() {
const activityTypes = [
'recreational', 'social', 'charity', 'cooking', 'education', 'relaxation', 'busywork'
];
const description = document.createElement('p');
description.classList.add('description');
description.textContent = 'Choose your type of activity';
box.appendChild(description);
const buttonsContainer = document.createElement('div');
buttonsContainer.classList.add('buttons-container');
box.appendChild(buttonsContainer);
activityTypes.forEach(type => {
const button = document.createElement('button');
button.textContent = type.charAt(0).toUpperCase() + type.slice(1);
button.addEventListener('click', () => {
fadeOut(box, () => {
showParticipantOptions(type);
});
});
buttonsContainer.appendChild(button);
});
fadeIn(box);
}
function showParticipantOptions(type) {
const participantNumbers = [1, 2, 3, 4, 5, 6, 8];
const description = document.createElement('p');
description.classList.add('description');
description.textContent = 'How many people will take part in the activity?';
box.appendChild(description);
const buttonsContainer = document.createElement('div');
buttonsContainer.classList.add('buttons-container');
box.appendChild(buttonsContainer);
participantNumbers.forEach(number => {
const button = document.createElement('button');
button.textContent = number;
button.addEventListener('click', () => {
fadeOut(box, () => {
fetchActivity(type, number);
});
});
buttonsContainer.appendChild(button);
});
fadeIn(box);
}
function fetchActivity(type, participants, retries = 3) {
const endpoint = `https://bored-api.appbrewery.com/filter?participants=${participants}&type=${type}`;
const proxyUrl = 'https://api.allorigins.win/get?url=' + encodeURIComponent(endpoint);
fetch(proxyUrl)
.then(response => response.json())
.then(data => {
try {
const result = JSON.parse(data.contents);
const randomActivity = result[Math.floor(Math.random() * result.length)];
showActivity(randomActivity);
} catch (error) {
console.error('Error parsing JSON:', error);
console.log('Response from API:', data.contents);
}
})
.catch(error => {
console.error('Error fetching data:', error);
if (retries > 0) {
console.log(`Retrying after ${3 - retries + 1} seconds...`);
setTimeout(() => {
fetchActivity(type, participants, retries - 1);
}, (3 - retries + 1) * 1000); // Exponential backoff: wait 1 second for each retry
} else {
console.error('Max retries exceeded. Unable to fetch data.');
}
});
}
function showActivity(activity) {
const description = document.createElement('p');
description.classList.add('description');
description.textContent = 'Your activity is...';
box.appendChild(description);
const activityDescription = document.createElement('p');
activityDescription.classList.add('activity-description');
activityDescription.textContent = activity.activity;
box.appendChild(activityDescription);
const details = document.createElement('div');
details.classList.add('activity-details');
box.appendChild(details);
addDetail(details, 'Type', capitalizeFirstLetter(activity.type));
addDetail(details, 'Participants', activity.participants);
addDetail(details, 'Availability', convertToRating(activity.availability));
addDetail(details, 'Accessibility', activity.accessibility);
addDetail(details, 'Duration', `A few ${activity.duration}`);
addDetail(details, 'Kid Friendly', activity.kidFriendly ? 'Yes' : 'No');
addLink(details, 'Link', activity.link);
fadeIn(box);
}
function addDetail(container, label, value) {
if (value) {
const detail = document.createElement('p');
detail.innerHTML = `<strong>${label}:</strong> ${value}`;
container.appendChild(detail);
}
}
function addLink(container, label, url) {
if (url) {
const detail = document.createElement('p');
detail.innerHTML = `<strong>${label}:</strong> <a href="${url}" target="_blank">${url}</a>`;
container.appendChild(detail);
}
}
function convertToRating(value) {
return Math.round(value * 5);
}
function capitalizeFirstLetter(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
function fadeOut(element, callback) {
element.classList.add('fade-out');
setTimeout(() => {
element.classList.add('hidden');
element.classList.remove('fade-out');
element.innerHTML = '<h1>Are You Bored?</h1>';
if (callback) callback();
}, 500);
}
function fadeIn(element) {
element.classList.remove('hidden');
element.classList.add('fade-in');
setTimeout(() => {
element.classList.remove('fade-in');
}, 500);
}
});