-
Notifications
You must be signed in to change notification settings - Fork 0
/
pensador-api.js
52 lines (40 loc) · 1.48 KB
/
pensador-api.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
const authors = ['Elon Musk', 'Jeff Bezos', 'Warren Buffet', 'Bill Gates'];
class API {
constructor() {
this.author = authors[Math.floor(Math.random() * authors.length)];
this.phrase = '';
}
async getPhrase(cb) {
const result = await axios.get(`https://minhastack.herokuapp.com/pensador?term=${this.author}&max=${30}`);
const tam = result.data.total;
const local = localStorage.getItem('phrases');
if(local) {
const saved_phrases = JSON.parse(local);
let new_phrase = result.data.phrases[Math.floor(Math.random() * tam)];
let cnt = 0
while((saved_phrases.find(p => p.text === new_phrase.text) !== undefined || new_phrase.author !== this.author) && cnt < 1000) {
new_phrase = result.data.phrases[Math.floor(Math.random() * tam)];
cnt++;
}
if(cnt >= 1000) {
this.phrase = saved_phrases[0].text;
this.author = saved_phrases[0].author;
saved_phrases.push(saved_phrases[0]);
saved_phrases.shift();
localStorage.setItem('phrases', JSON.stringify(saved_phrases));
} else {
saved_phrases.push(new_phrase);
this.phrase = new_phrase.text;
if(saved_phrases.length > 30) saved_phrases.shift();
localStorage.setItem('phrases', JSON.stringify(saved_phrases));
}
} else {
let new_phrase = result.data.phrases[Math.floor(Math.random() * tam)];
const saved_phrases = []
this.phrase = new_phrase.text;
saved_phrases.push(new_phrase);
localStorage.setItem('phrases', JSON.stringify(saved_phrases));
}
if(cb) cb();
}
}