
2 changed files with 55 additions and 3 deletions
@ -0,0 +1,46 @@ |
|||||||
|
class JokeService { |
||||||
|
|
||||||
|
constructor(category = 'programming') { |
||||||
|
this.category = category; |
||||||
|
this.apiBaseUrl = 'https://v2.jokeapi.dev/joke/' |
||||||
|
} |
||||||
|
|
||||||
|
setCategory(category) { |
||||||
|
this.category = category; |
||||||
|
} |
||||||
|
|
||||||
|
tell() { |
||||||
|
let service = this; |
||||||
|
axios.get( |
||||||
|
service.apiBaseUrl + service.category |
||||||
|
).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); |
||||||
|
} |
||||||
|
} |
Reference in new issue