Skip to content

Commit 775ae4a

Browse files
author
Piotr Gulbinowicz
committed
split stats and flushes loading
1 parent 8d77e34 commit 775ae4a

File tree

1 file changed

+24
-14
lines changed

1 file changed

+24
-14
lines changed

pwa/flush/components.go

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ func (r *RegisterContainer) Render() app.UI {
8888
type RootContainer struct {
8989
app.Compo
9090
buttonUpdate
91+
Stats app.UI
9192
FlushList app.UI
9293
}
9394

@@ -104,15 +105,21 @@ func (b *RootContainer) OnMount(ctx app.Context) {
104105
app.Window().GetElementByID("about-container").Set("className", "invisible fixed")
105106
ShowLoading("flushes-loading")
106107
ctx.Async(func() {
108+
stats, err := StatsDiv(ctx)
107109
result := GetFlushesFromOID(ctx)
108110
ctx.Dispatch(func(ctx app.Context) {
111+
if err != nil {
112+
ShowErrorDiv(ctx, err, 2)
113+
return
114+
}
109115
defer Hide("flushes-loading")
110116
if result == nil {
111117
ShowErrorDiv(ctx, errors.New("Error while fetching flushes"), 2)
112118
return
113119
}
114120
app.Window().GetElementByID("hidden-hello").Set("innerHTML", "hello!")
115121
b.SetList(result)
122+
b.Stats = stats
116123
var isEmpty string
117124
ctx.GetState("no-flushes", &isEmpty)
118125
log.Println("no-flushes: ", isEmpty)
@@ -151,6 +158,7 @@ func (b *RootContainer) Render() app.UI {
151158
).ID("root-buttons-container").Class(InviCss),
152159
app.P().Text("Tracked flushes:").Class("py-2"),
153160
&LoadingWidget{id: "flushes-loading"},
161+
b.Stats,
154162
b.FlushList,
155163
app.Div().Body(
156164
&b.buttonUpdate,
@@ -559,20 +567,6 @@ func FlushTable(flushes []Flush, ctx app.Context) app.UI {
559567
).Class("flex flex-col p-4 border-1 shadow-lg rounded-lg").ID("div-"+flush.ID),
560568
)
561569
}
562-
stats, err := GetStats(ctx)
563-
if err != nil {
564-
ShowErrorDiv(ctx, err, 3)
565-
return app.Div()
566-
}
567-
statsDiv := app.Div().Body(
568-
app.P().Text("Total flushes: "+strconv.Itoa(stats.FlushCount)),
569-
app.P().Text("Total time: "+strconv.Itoa(stats.TotalTime)+" minutes"),
570-
app.P().Text("Mean time: "+strconv.Itoa(stats.MeanTime)+" minutes"),
571-
app.P().Text("Mean rating: "+strconv.Itoa(stats.MeanRating)),
572-
app.P().Text("Times with phone used: "+strconv.Itoa(stats.PhoneUsedCount)),
573-
app.P().Text("Percent with phone used: "+strconv.Itoa(stats.PercentPhoneUsed)+"%"),
574-
).Class("flex flex-col p-4 border-1 shadow-lg rounded-lg font-bold")
575-
divs = append([]app.UI{statsDiv}, divs...)
576570
return app.Div().Body(divs...)
577571
}
578572

@@ -987,3 +981,19 @@ func (b *BurgerMenuButton) OnClick(ctx app.Context, e app.Event) {
987981
app.Window().GetElementByID("root-buttons-container").Set("className", RootButtonsCss)
988982
}
989983
}
984+
985+
func StatsDiv(ctx app.Context) (app.UI, error) {
986+
stats, err := GetStats(ctx)
987+
if err != nil {
988+
return nil, err
989+
}
990+
return app.Div().Body(
991+
app.P().Text("Total flushes: "+strconv.Itoa(stats.FlushCount)),
992+
app.P().Text("Total time: "+strconv.Itoa(stats.TotalTime)+" minutes"),
993+
app.P().Text("Mean time: "+strconv.Itoa(stats.MeanTime)+" minutes"),
994+
app.P().Text("Mean rating: "+strconv.Itoa(stats.MeanRating)),
995+
app.P().Text("Times with phone used: "+strconv.Itoa(stats.PhoneUsedCount)),
996+
app.P().Text("Percent with phone used: "+strconv.Itoa(stats.PercentPhoneUsed)+"%"),
997+
).Class("flex flex-col p-4 border-1 shadow-lg rounded-lg font-bold"),
998+
nil
999+
}

0 commit comments

Comments
 (0)