-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroot.vugu
116 lines (97 loc) · 2.39 KB
/
root.vugu
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<div class="gadget">
<div class ="body">
<div class="spinner spinner--1" vg-if='data.isLoading'></div>
<div class="display" vg-if='data.dadJoke.Joke != ""'>
<span vg-html='data.dadJoke.Joke'></span>
</div>
</div>
<button class="button" @click="data.HandleClick(event)">Get dad joke</button>
</div>
<style>
.container--box .box .spinner--1 {
border-top: 2px solid #fff;
animation: spinner1 600ms linear infinite;
}
@keyframes spinner1 {
to {
transform: rotate(360deg);
}
}
.body {
width: 150px;
height: 250px;
border: 5px solid #404041;
background: #e9df72;
box-sizing: border-box;
padding: 20px;
}
body {
background: #9e97c6;
}
.gadget {
width: 150px;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -60%);
}
.button {
border-radius: 5px;
border: 4px solid #404041;
margin-top: 20px;
background: $red;
font-family: "Share Tech Mono";
text-align: center;
color: #404041;
padding: 7px;
cursor: pointer;
}
.display {
margin-top: 10px;
height: 80px;
padding: 5px;
font-family: "VT323";
color: #404041;
text-align: center;
}
</style>
<script type="application/x-go">
import "encoding/json"
import "net/http"
import "log"
type RootData struct {
dadJoke DadJoke
isLoading bool
}
type DadJoke struct {
Joke string `json:"joke"`
}
func (data *RootData) HandleClick(event *vugu.DOMEvent) {
data.dadJoke = DadJoke{}
ee := event.EventEnv()
go func() {
ee.Lock()
data.isLoading = true
ee.UnlockRender()
client := &http.Client{}
req, _ := http.NewRequest("GET", "https://icanhazdadjoke.com", nil)
req.Header.Set("Accept", "application/json")
res, err := client.Do(req)
if err != nil {
log.Printf("Error fetch()ing: %v", err)
return
}
defer res.Body.Close()
var newb DadJoke
err = json.NewDecoder(res.Body).Decode(&newb)
if err != nil {
log.Printf("Error JSON decoding: %v", err)
return
}
ee.Lock()
defer ee.UnlockRender()
data.dadJoke = newb
data.isLoading = false
}()
}
</script>