|
1 | 1 | <div x-data="quote()" x-init="fetchQuote()">
|
2 |
| - <template x-if="isLoaded()"> |
| 2 | + <template x-if="!loading"> |
3 | 3 | <template x-if="success">
|
4 |
| - <a :href="link" target="_blank" |
5 |
| - class="inline-block py-2 pl-4 my-2 text-orange-300 border-l-4 border-orange-300" x-text="quote"> |
| 4 | + <a :href="quote.link" target="_blank" |
| 5 | + class="inline-block py-2 pl-4 my-2 text-orange-300 border-l-4 border-orange-300"> |
| 6 | + <p x-text="quote.text"></p> |
| 7 | + <p x-text="quote.author"></p> |
6 | 8 | </a>
|
7 | 9 | </template>
|
8 | 10 | <template x-if="!success">
|
9 |
| - <p class="py-2 my-2 text-sm text-red-300" x-text="quote"> |
| 11 | + <p class="py-2 my-2 text-sm text-red-300" x-text="errorMessage"> |
10 | 12 | </p>
|
11 | 13 | </template>
|
12 | 14 | </template>
|
13 |
| - <template x-if="!isLoaded()"> |
| 15 | + <template x-if="loading"> |
14 | 16 | <p class="py-2 my-2 text-sm text-gray-300">
|
15 | 17 | Loading quote of the day...
|
16 | 18 | </p>
|
|
20 | 22 | <script>
|
21 | 23 | const quote = () => {
|
22 | 24 | return {
|
23 |
| - quote: '', |
24 |
| - author: '', |
25 |
| - link: '', |
26 |
| - success: false, |
27 |
| - isLoaded() { |
28 |
| - return this.quote !== '' |
| 25 | + quote: { |
| 26 | + text: '', |
| 27 | + author: '', |
| 28 | + link: '', |
29 | 29 | },
|
| 30 | + errorMessage: '', |
| 31 | + loading: true, |
| 32 | + success: false, |
| 33 | + |
30 | 34 | fetchQuote() {
|
31 | 35 | const url = "/.netlify/functions/quote";
|
32 | 36 |
|
33 | 37 | fetch(url)
|
34 | 38 | .then(response => response.json())
|
35 | 39 | .then(data => {
|
36 |
| - this.success = data.success; |
37 |
| - this.quote = this.success ? `${data.quote.text} - ${data.quote.author}` : data.error |
38 |
| - this.link = this.success ? data.quote.link : '' |
| 40 | + this.loading = false |
| 41 | + this.success = data.success |
| 42 | + |
| 43 | + if (this.success) |
| 44 | + this.quote = data.quote |
| 45 | + else |
| 46 | + this.errorMessage = data.error |
39 | 47 |
|
40 | 48 | console.log('Loaded quote. Success: ' + this.success)
|
41 | 49 | });
|
|
0 commit comments