You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.
|
|
|
|
class JokeService {
|
|
|
|
|
|
|
|
|
|
constructor(category = ['programming', 'coding', 'development'], lang = 'en') {
|
|
|
|
|
this.category = Array.isArray(category) ? category : [category];
|
|
|
|
|
this.lang = lang;
|
|
|
|
|
this.apiBaseUrl = 'https://v2.jokeapi.dev/joke/'
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setCategory(category) {
|
|
|
|
|
this.category = Array.isArray(category) ? category : [category];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tell() {
|
|
|
|
|
let service = this;
|
|
|
|
|
|
|
|
|
|
axios.get(
|
|
|
|
|
service.apiBaseUrl + getRandomItem(service.category) + '?lang=' + service.lang
|
|
|
|
|
).then((response) => {
|
|
|
|
|
if (response.data.error === false) {
|
|
|
|
|
service.show(response.data);
|
|
|
|
|
}
|
|
|
|
|
}).catch((error) => {
|
|
|
|
|
console.error(error);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
show(joke) {
|
|
|
|
|
if (joke.type === 'twopart') {
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
showNotification(joke.delivery)
|
|
|
|
|
}, 4000);
|
|
|
|
|
|
|
|
|
|
showNotification(joke.setup);
|
|
|
|
|
} else {
|
|
|
|
|
showNotification(joke.joke);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function showNotification(text) {
|
|
|
|
|
if (Notification.permission === 'granted') {
|
|
|
|
|
new Notification('', {
|
|
|
|
|
body: text
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
// alert(text);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getRandomItem(items) {
|
|
|
|
|
return items[Math.floor(Math.random()*items.length)];
|
|
|
|
|
}
|