diff --git a/js/JokeService.js b/js/JokeService.js new file mode 100644 index 0000000..f82d861 --- /dev/null +++ b/js/JokeService.js @@ -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); + } +} diff --git a/js/app.js b/js/app.js index 110fa62..9207e68 100644 --- a/js/app.js +++ b/js/app.js @@ -81,17 +81,23 @@ const TimeTrack = { }, 5000); } - Notification.requestPermission(); setInterval(() => { vue.checkTimeBoxes(); }, 10000); + + if (this.fun) { + let jokeService = new JokeService(); + setInterval(() => { + jokeService.tell(); + }, 1_800_000) + } }, methods: { loadTooltips() { let tooltipTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle="tooltip"]')) tooltipTriggerList.map(function (tooltipTriggerEl) { return new bootstrap.Tooltip(tooltipTriggerEl) - }) + }); }, loadStorage() { let storedTickets = JSON.parse(localStorage.getItem('tickets')); @@ -122,7 +128,7 @@ const TimeTrack = { this.portals = storedPortals == null ? [] : storedPortals; let storedFun = localStorage.getItem('fun'); - this.fun = storedFun == null ? false : storedFun; + this.fun = storedFun == null || storedFun === 'false' ? false : storedFun; // let storedSnippets = JSON.parse(localStorage.getItem('snippets')); // this.snippets = storedSnippets == null ? [] : storedSnippets;