From 9100e32313580c02e07e650424427d8758a53356 Mon Sep 17 00:00:00 2001 From: stingl Date: Tue, 28 Sep 2021 11:07:27 +0200 Subject: [PATCH] stop redundant jokes --- js/JokeService.js | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/js/JokeService.js b/js/JokeService.js index 7b0c197..41a35f7 100644 --- a/js/JokeService.js +++ b/js/JokeService.js @@ -3,7 +3,8 @@ class JokeService { constructor(categories = ['programming', 'coding', 'development'], lang = 'en') { this.categories = Array.isArray(categories) ? categories : [categories]; 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) { @@ -17,6 +18,11 @@ class JokeService { service.apiBaseUrl + service.categories.join(',') + '?lang=' + service.lang ).then((response) => { if (response.data.error === false) { + if (service.toldJokes.includes(response.data.id)) { + service.tell(); + return false; + } + service.show(response.data); } }).catch((error) => { @@ -25,16 +31,26 @@ class JokeService { } show(joke) { + let service = this; if (joke.type === 'twopart') { setTimeout(() => { showNotification(joke.delivery) }, 4000); showNotification(joke.setup); + service.jokeTold(joke); } else { showNotification(joke.joke); } } + + jokeTold(joke) { + this.toldJokes.push(joke.id); + localStorage.setItem('toldJokes', JSON.stringify(this.toldJokes)); + } + + getToldJokes() { + } } function showNotification(text) {