Skip to content

Commit 9883f16

Browse files
author
Alejandro Figueroa
committed
Component update
1 parent a47fb0b commit 9883f16

File tree

1 file changed

+22
-14
lines changed

1 file changed

+22
-14
lines changed

_includes/components/quote.html

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
<div x-data="quote()" x-init="fetchQuote()">
2-
<template x-if="isLoaded()">
2+
<template x-if="!loading">
33
<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>
68
</a>
79
</template>
810
<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">
1012
</p>
1113
</template>
1214
</template>
13-
<template x-if="!isLoaded()">
15+
<template x-if="loading">
1416
<p class="py-2 my-2 text-sm text-gray-300">
1517
Loading quote of the day...
1618
</p>
@@ -20,22 +22,28 @@
2022
<script>
2123
const quote = () => {
2224
return {
23-
quote: '',
24-
author: '',
25-
link: '',
26-
success: false,
27-
isLoaded() {
28-
return this.quote !== ''
25+
quote: {
26+
text: '',
27+
author: '',
28+
link: '',
2929
},
30+
errorMessage: '',
31+
loading: true,
32+
success: false,
33+
3034
fetchQuote() {
3135
const url = "/.netlify/functions/quote";
3236

3337
fetch(url)
3438
.then(response => response.json())
3539
.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
3947

4048
console.log('Loaded quote. Success: ' + this.success)
4149
});

0 commit comments

Comments
 (0)