/
products.go
49 lines (38 loc) · 1.41 KB
/
products.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
package handlers
import (
"context"
"encoding/json"
"strings"
"honnef.co/go/js/xhr"
"github.com/EngineerKamesh/igb/igweb/client/common"
"github.com/EngineerKamesh/igb/igweb/shared/models"
"github.com/EngineerKamesh/igb/igweb/shared/templatedata"
"go.isomorphicgo.org/go/isokit"
)
func ProductsHandler(env *common.Env) isokit.Handler {
return isokit.HandlerFunc(func(ctx context.Context) {
productsChannel := make(chan []*models.Product)
go FetchProducts(productsChannel)
products := <-productsChannel
templateData := &templatedata.Products{PageTitle: "Products", Products: products}
env.TemplateSet.Render("products_content", &isokit.RenderParams{Data: templateData, Disposition: isokit.PlacementReplaceInnerContents, Element: env.PrimaryContent, PageTitle: templateData.PageTitle})
InitializeProductsPage(env)
env.Router.RegisterLinks("#primaryContent a")
})
}
func FetchProducts(productsChannel chan []*models.Product) {
data, err := xhr.Send("GET", "/restapi/get-products", nil)
if err != nil {
println("Encountered error: ", err)
return
}
var products []*models.Product
json.NewDecoder(strings.NewReader(string(data))).Decode(&products)
productsChannel <- products
}
func InitializeProductsPage(env *common.Env) {
buttons := env.Document.GetElementsByClassName("addToCartButton")
for _, button := range buttons {
button.AddEventListener("click", false, handleAddToCartButtonClickEvent)
}
}