Browse Source

stop redundant jokes

modals-to-spa
stingl 4 years ago
parent
commit
9100e32313
  1. 18
      js/JokeService.js

18
js/JokeService.js

@ -3,7 +3,8 @@ class JokeService {
constructor(categories = ['programming', 'coding', 'development'], lang = 'en') { constructor(categories = ['programming', 'coding', 'development'], lang = 'en') {
this.categories = Array.isArray(categories) ? categories : [categories]; this.categories = Array.isArray(categories) ? categories : [categories];
this.lang = lang; this.lang = lang;
this.apiBaseUrl = 'https://v2.jokeapi.dev/joke/' this.apiBaseUrl = 'https://v2.jokeapi.dev/joke/';
this.toldJokes = localStorage.getItem('toldJokes') !== null ? JSON.parse(localStorage.getItem('toldJokes')) : [];
} }
setCategory(category) { setCategory(category) {
@ -17,6 +18,11 @@ class JokeService {
service.apiBaseUrl + service.categories.join(',') + '?lang=' + service.lang service.apiBaseUrl + service.categories.join(',') + '?lang=' + service.lang
).then((response) => { ).then((response) => {
if (response.data.error === false) { if (response.data.error === false) {
if (service.toldJokes.includes(response.data.id)) {
service.tell();
return false;
}
service.show(response.data); service.show(response.data);
} }
}).catch((error) => { }).catch((error) => {
@ -25,16 +31,26 @@ class JokeService {
} }
show(joke) { show(joke) {
let service = this;
if (joke.type === 'twopart') { if (joke.type === 'twopart') {
setTimeout(() => { setTimeout(() => {
showNotification(joke.delivery) showNotification(joke.delivery)
}, 4000); }, 4000);
showNotification(joke.setup); showNotification(joke.setup);
service.jokeTold(joke);
} else { } else {
showNotification(joke.joke); showNotification(joke.joke);
} }
} }
jokeTold(joke) {
this.toldJokes.push(joke.id);
localStorage.setItem('toldJokes', JSON.stringify(this.toldJokes));
}
getToldJokes() {
}
} }
function showNotification(text) { function showNotification(text) {