-
Notifications
You must be signed in to change notification settings - Fork 6
/
about.go
59 lines (52 loc) · 1.85 KB
/
about.go
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
package handlers
import (
"context"
"encoding/json"
"log"
"strconv"
"strings"
"time"
"honnef.co/go/js/xhr"
"github.com/EngineerKamesh/igb/igweb/client/common"
"github.com/EngineerKamesh/igb/igweb/shared/cogs/timeago"
"github.com/EngineerKamesh/igb/igweb/shared/models"
"github.com/EngineerKamesh/igb/igweb/shared/templatedata"
"go.isomorphicgo.org/go/isokit"
)
func AboutHandler(env *common.Env) isokit.Handler {
return isokit.HandlerFunc(func(ctx context.Context) {
gopherTeamChannel := make(chan []*models.Gopher)
go FetchGopherTeam(gopherTeamChannel)
gophers := <-gopherTeamChannel
templateData := templatedata.About{PageTitle: "About", Gophers: gophers}
env.TemplateSet.Render("about_content", &isokit.RenderParams{Data: templateData, Disposition: isokit.PlacementReplaceInnerContents, Element: env.PrimaryContent, PageTitle: templateData.PageTitle})
InitializeAboutPage(env)
})
}
func FetchGopherTeam(gopherTeamChannel chan []*models.Gopher) {
data, err := xhr.Send("GET", "/restapi/get-gopher-team", nil)
if err != nil {
println("Encountered error: ", err)
}
var gophers []*models.Gopher
json.NewDecoder(strings.NewReader(string(data))).Decode(&gophers)
gopherTeamChannel <- gophers
}
func InitializeAboutPage(env *common.Env) {
humanReadableDivs := env.Document.GetElementsByClassName("humanReadableDate")
for _, div := range humanReadableDivs {
unixTimestamp, err := strconv.ParseInt(div.GetAttribute("data-starttimeunix"), 10, 64)
if err != nil {
log.Println("Encountered error when attempting to parse int64 from string:", err)
}
t := time.Unix(unixTimestamp, 0)
humanTime := timeago.NewTimeAgo()
humanTime.CogInit(env.TemplateSet)
humanTime.SetID(div.ID())
humanTime.SetTime(t)
err = humanTime.Start()
if err != nil {
println("Encountered the following error when attempting to start the timeago cog: ", err)
}
}
}