Skip to content

Commit e3be258

Browse files
authored
Merge pull request #58 from pgulb/48-add-loading-animation-to-pwa
48 add loading animation to pwa
2 parents 7c96234 + bec446d commit e3be258

File tree

2 files changed

+46
-3
lines changed

2 files changed

+46
-3
lines changed

pwa/flush/browser.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,3 +120,11 @@ func ValidateFlush(flush Flush) error {
120120
}
121121
return nil
122122
}
123+
124+
func ShowLoading(id string) {
125+
app.Window().GetElementByID(id).Set("className", LoadingCss)
126+
}
127+
128+
func Hide(id string) {
129+
app.Window().GetElementByID(id).Set("className", InviCss)
130+
}

pwa/flush/components.go

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const (
1616
RegisterDivCss = "p-4 text-center text-xl shadow-lg bg-white rounded-lg mx-10"
1717
InviCss = "fixed invisible"
1818
RootContainerCss = "shadow-lg bg-white rounded-lg p-6 min-h-72 relative"
19+
LoadingCss = "flex flex-row justify-center items-center"
1920
)
2021

2122
type ErrorContainer struct {
@@ -68,6 +69,7 @@ func (r *RegisterContainer) Render() app.UI {
6869
&buttonRegister{},
6970
app.P().Text("").Class("text-red-500").ID("register-error"),
7071
).Class(InviCss).ID("register-container"),
72+
&LoadingWidget{id: "register-loading"},
7173
)
7274
}
7375

@@ -92,6 +94,8 @@ func (b *RootContainer) OnMount(ctx app.Context) {
9294
ShowErrorDiv(ctx, err, 1)
9395
return
9496
}
97+
ShowLoading("flushes-loading")
98+
defer Hide("flushes-loading")
9599
flushes, err := GetFlushes(ctx)
96100
if err != nil {
97101
ShowErrorDiv(ctx, err, 1)
@@ -108,6 +112,7 @@ func (b *RootContainer) Render() app.UI {
108112
app.H1().Text("Flush Log").Class("text-2xl"),
109113
&buttonLogout{},
110114
app.P().Text("Tracked flushes:").Class("py-2"),
115+
&LoadingWidget{id: "flushes-loading"},
111116
b.FlushList,
112117
app.Div().Body(
113118
b.buttonUpdate.Render(),
@@ -144,6 +149,8 @@ func (b *buttonUpdate) onClick(ctx app.Context, e app.Event) {
144149
ShowErrorDiv(ctx, err, 1)
145150
return
146151
}
152+
ShowLoading("flushes-loading")
153+
defer Hide("flushes-loading")
147154
flushes, err := GetFlushes(ctx)
148155
if err != nil {
149156
ShowErrorDiv(ctx, err, 1)
@@ -180,6 +187,7 @@ func (l *LoginContainer) Render() app.UI {
180187
&buttonLogin{},
181188
&buttonShowRegister{},
182189
),
190+
&LoadingWidget{id: "login-loading"},
183191
),
184192
).Class("p-4 text-center text-xl shadow-lg bg-white rounded-lg").ID("login-container"),
185193
&RegisterContainer{},
@@ -198,6 +206,7 @@ func (b *buttonLogin) Render() app.UI {
198206
func (b *buttonLogin) onClick(ctx app.Context, e app.Event) {
199207
loginSeconds := 60
200208
log.Println("Trying to log in...")
209+
ShowLoading("login-loading")
201210
if app.Window().GetElementByID("remember-me").Get("checked").Bool() {
202211
log.Println("remember-me checked")
203212
loginSeconds = 604800 // week
@@ -207,10 +216,12 @@ func (b *buttonLogin) onClick(ctx app.Context, e app.Event) {
207216
user, pass := GetLoginCreds()
208217
err := ValidateLoginCreds(user, pass, lastCreds)
209218
if err != nil {
219+
Hide("login-loading")
210220
ShowErrorDiv(ctx, err, 1)
211221
return
212222
}
213223
ctx.Async(func() {
224+
defer Hide("login-loading")
214225
status, basic_auth, err := TryLogin(user, pass)
215226
if err != nil {
216227
ShowErrorDiv(ctx, err, 1)
@@ -248,15 +259,18 @@ func (b *buttonRegister) Render() app.UI {
248259
}
249260
func (b *buttonRegister) onClick(ctx app.Context, e app.Event) {
250261
log.Println("Trying to register...")
262+
ShowLoading("register-loading")
251263
lastCreds := LastTriedCreds{}
252264
ctx.GetState("lastUsedCredsRegister", &lastCreds)
253265
user, pass, repeatPass := GetRegisterCreds()
254266
err := ValidateRegistryCreds(user, pass, repeatPass, lastCreds)
255267
if err != nil {
268+
Hide("register-loading")
256269
ShowErrorDiv(ctx, err, 1)
257270
return
258271
}
259272
ctx.Async(func() {
273+
defer Hide("register-loading")
260274
status, basic_auth, err := TryRegister(user, pass)
261275
log.Println("register status code: ", status)
262276
if err != nil {
@@ -334,6 +348,7 @@ func (c *NewFlushContainer) Render() app.UI {
334348
"new-flush-note").MaxLength(100),
335349
app.Br(),
336350
&SubmitFlushButton{},
351+
&LoadingWidget{id: "new-flush-loading"},
337352
).Class("p-4 text-center text-xl shadow-lg bg-white rounded-lg"),
338353
app.Br(),
339354
&LinkButton{Text: "Back to Home Screen", Location: "."},
@@ -384,9 +399,11 @@ func (b *SubmitFlushButton) Render() app.UI {
384399
}
385400
func (b *SubmitFlushButton) onClick(ctx app.Context, e app.Event) {
386401
var creds Creds
402+
ShowLoading("new-flush-loading")
387403
ctx.GetState("creds", &creds)
388404
log.Println("Logged in: ", creds.LoggedIn)
389405
if !creds.LoggedIn {
406+
Hide("new-flush-loading")
390407
app.Window().Set("location", "login")
391408
return
392409
}
@@ -397,15 +414,18 @@ func (b *SubmitFlushButton) onClick(ctx app.Context, e app.Event) {
397414
app.Window().GetElementByID("new-flush-phone-used").Get("checked").Bool(),
398415
app.Window().GetElementByID("new-flush-note").Get("value").String())
399416
if err != nil {
417+
Hide("new-flush-loading")
400418
ShowErrorDiv(ctx, err, 2)
401419
return
402420
}
403421
err = ValidateFlush(flush)
404422
if err != nil {
423+
Hide("new-flush-loading")
405424
ShowErrorDiv(ctx, err, 1)
406425
return
407426
}
408427
ctx.Async(func() {
428+
defer Hide("new-flush-loading")
409429
statusCode, err := TryAddFlush(creds, flush)
410430
log.Println("Flush add statusCode: ", statusCode)
411431
if err != nil {
@@ -444,9 +464,8 @@ func (a *AboutContainer) Render() app.UI {
444464
Location: "login",
445465
AdditionalCss: "hover:bg-yellow-700",
446466
},
447-
).
448-
ID("about-container").Class("flex flex-col p-4 shadow-lg rounded-lg"),
449-
).Class(CenteringDivCss)
467+
).Class("flex flex-col p-4 shadow-lg rounded-lg"),
468+
).Class(CenteringDivCss).ID("about-container")
450469
}
451470

452471
func FLushTable(flushes []Flush) app.UI {
@@ -513,3 +532,19 @@ func timeDiv(flush Flush) app.UI {
513532
)
514533
}
515534
}
535+
536+
type LoadingWidget struct {
537+
app.Compo
538+
id string
539+
}
540+
541+
func (l *LoadingWidget) Render() app.UI {
542+
return app.Div().Body(
543+
app.Div().Body(
544+
app.Span().
545+
Text("Loading...").Class("font-bold text-black").
546+
Class("!absolute !-m-px !h-px !w-px !overflow-hidden !whitespace-nowrap !border-0 !p-0 ![clip:rect(0,0,0,0)]"),
547+
).
548+
Class("inline-block h-8 w-8 animate-spin rounded-full border-4 border-solid border-current border-e-transparent align-[-0.125em] text-surface motion-reduce:animate-[spin_1.5s_linear_infinite] text-yellow-500"),
549+
).Class(InviCss).ID(l.id)
550+
}

0 commit comments

Comments
 (0)