Browse Source

extend for lang

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

18
js/JokeService.js

@ -1,19 +1,21 @@ @@ -1,19 +1,21 @@
class JokeService {
constructor(category = 'programming') {
this.category = category;
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 = category;
this.category = Array.isArray(category) ? category : [category];
}
tell() {
let service = this;
axios.get(
service.apiBaseUrl + service.category
).then((response) => {
axios.get(service.apiBaseUrl + getRandomItem(service.category), {
lang: service.lang
}).then((response) => {
if (response.data.error === false) {
service.show(response.data);
}
@ -44,3 +46,7 @@ function showNotification(text) { @@ -44,3 +46,7 @@ function showNotification(text) {
// alert(text);
}
}
function getRandomItem(items) {
return items[Math.floor(Math.random()*items.length)];
}