refactor: data structs for template + method

enclosing rooms.Room into roomTableData, with methods to be used in
tempalte to get other template related derived data structures
This commit is contained in:
efim
2023-11-19 05:41:03 +00:00
parent 8b23103e8e
commit e4c56506be
3 changed files with 35 additions and 37 deletions

View File

@@ -72,10 +72,7 @@ func streamingRoomStates(
templFile := "templates/room.gohtml"
tableTemplates := "templates/tableTemplates.gohtml"
tmpl := template.Must(
template.New("").Funcs(template.FuncMap{
"bricksForPerson": bricksForPerson,
"personsFromRoom": personsFromRoom,
}).ParseFS(templateFs, tableTemplates, templFile))
template.New("").ParseFS(templateFs, tableTemplates, templFile))
roomStream := roomsM.Subscribe(r.Context(), roomName)
for room := range roomStream {
@@ -84,7 +81,11 @@ func streamingRoomStates(
var buffer bytes.Buffer
err := tmpl.ExecuteTemplate(&buffer, "simpleRoomShow", room)
roomTemplateData := roomTableData{
Room: &room,
currentPerson: session.PersonId,
}
err := tmpl.ExecuteTemplate(&buffer, "simpleRoomShow", &roomTemplateData)
if err != nil {
log.Printf("/rooms/subscribe/%s got error on template %s", roomName, err)
}
@@ -197,10 +198,7 @@ func roomPageRoute(
baseFile := "templates/base.gohtml"
tableTemplates := "templates/tableTemplates.gohtml"
tmpl := template.Must(
template.New("").Funcs(template.FuncMap{
"bricksForPerson": bricksForPerson,
"personsFromRoom": personsFromRoom,
}).ParseFS(templateFs, tableTemplates, templFile, baseFile))
template.New("").ParseFS(templateFs, tableTemplates, templFile, baseFile))
type GestureData struct {
Name string
@@ -215,10 +213,13 @@ func roomPageRoute(
}
contentData := struct {
Room rooms.Room
Room *roomTableData
Gestures []GestureData
}{
Room: room,
Room: &roomTableData{
Room: &room,
currentPerson: session.PersonId,
},
Gestures: gesturesData,
}
data := pageData{