/
productdetail.go
47 lines (39 loc) · 1.58 KB
/
productdetail.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
package handlers
import (
"context"
"encoding/json"
"strings"
"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"
"honnef.co/go/js/xhr"
)
func ProductDetailHandler(env *common.Env) isokit.Handler {
return isokit.HandlerFunc(func(ctx context.Context) {
routeVars := ctx.Value(isokit.RouteVarsKey("Vars")).(map[string]string)
productTitle := routeVars[`product-detail/{productTitle}`]
productChannel := make(chan *models.Product)
go FetchProductDetail(productChannel, productTitle)
product := <-productChannel
templateData := &templatedata.ProductDetail{PageTitle: product.Name, Product: product}
env.TemplateSet.Render("product_detail_content", &isokit.RenderParams{Data: templateData, Disposition: isokit.PlacementReplaceInnerContents, Element: env.PrimaryContent, PageTitle: templateData.PageTitle})
InitializeProductDetailPage(env)
})
}
func FetchProductDetail(productChannel chan *models.Product, productTitle string) {
data, err := xhr.Send("GET", "/restapi/get-product-detail"+"/"+productTitle, nil)
if err != nil {
println("Encountered error: ", err)
println(err)
}
var product *models.Product
json.NewDecoder(strings.NewReader(string(data))).Decode(&product)
productChannel <- product
}
func InitializeProductDetailPage(env *common.Env) {
buttons := env.Document.GetElementsByClassName("addToCartButton")
for _, button := range buttons {
button.AddEventListener("click", false, handleAddToCartButtonClickEvent)
}
}