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

View File

@ -33,32 +33,29 @@ var brickColors = map[rooms.HandGesture]template.CSS{
rooms.ChangeTopic: "--change-topic-color", rooms.ChangeTopic: "--change-topic-color",
} }
// data to be passed to "roomPeople" template
type roomTableData struct { type roomTableData struct {
Persons []personData *rooms.Room
Total int currentPerson rooms.PersonId
Tangens float64
} }
func personsFromRoom(room rooms.Room) roomTableData { func (room *roomTableData)Persons() []personData {
// TODO start from the 'logged in person' persons := make([]personData, 0, room.Total())
total := len(room.Paricipants)
persons := make([]personData, 0, total)
tangens := math.Tan(math.Pi / float64(total)) // Math.tan(Math.PI/m);
// tangens = math.Round(tangens*100) / 100
for i, pId := range room.Paricipants { for i, pId := range room.Paricipants {
personData := personDataFromRoom(room, pId) personData := personDataFromRoom(room, pId)
personData.Index = i personData.Index = i
// personData.Tan = math.Round(tangens*100)/100
persons = append(persons, personData) persons = append(persons, personData)
} }
return roomTableData{ return persons
Persons: persons,
Total: total,
Tangens: tangens,
} }
func (r *roomTableData)Total() int {
return len(r.Paricipants)
}
func (r *roomTableData)Tangens() float64 {
return math.Tan(math.Pi / float64(r.Total())) // Math.tan(Math.PI/m);
} }
func personDataFromRoom(room rooms.Room, pId rooms.PersonId) personData { func personDataFromRoom(room *roomTableData, pId rooms.PersonId) personData {
person, exists := room.AllKnownPeople[pId] person, exists := room.AllKnownPeople[pId]
if !exists || !slices.Contains(room.Paricipants, pId) { if !exists || !slices.Contains(room.Paricipants, pId) {
return personData{} return personData{}
@ -85,7 +82,7 @@ func personDataFromRoom(room rooms.Room, pId rooms.PersonId) personData {
} }
} }
func bricksForPerson(pData personData) []brickState { func (pData personData)BricksForPerson() []brickState {
var result = make([]brickState, 5) var result = make([]brickState, 5)
for gesture := rooms.ChangeTopic; gesture <= rooms.Meta; gesture++ { for gesture := rooms.ChangeTopic; gesture <= rooms.Meta; gesture++ {
// for index := rooms.Meta; index >= rooms.ChangeTopic; index-- { // for index := rooms.Meta; index >= rooms.ChangeTopic; index-- {
@ -168,7 +165,7 @@ func roomTemplatesPreview(
Bricks []SingleBrickData Bricks []SingleBrickData
ABrick brickState ABrick brickState
TestPerson personData TestPerson personData
ARoom rooms.Room ARoom *roomTableData
}{ }{
DefaultColor: "--expand-color", DefaultColor: "--expand-color",
ABrick: brickState{ ABrick: brickState{
@ -198,7 +195,10 @@ func roomTemplatesPreview(
}, },
}, },
TestPerson: testPersonData, TestPerson: testPersonData,
ARoom: aRoom, ARoom: &roomTableData{
Room: &aRoom,
currentPerson: aRoom.Paricipants[0],
},
} }
pageData := pageData{ pageData := pageData{
@ -207,10 +207,7 @@ func roomTemplatesPreview(
} }
baseFile := "templates/base.gohtml" baseFile := "templates/base.gohtml"
tmpl := template.Must(template.New("").Funcs(template.FuncMap{ tmpl := template.Must(template.New("").ParseFS(templateFs, baseFile,
"bricksForPerson": bricksForPerson,
"personsFromRoom": personsFromRoom,
}).ParseFS(templateFs, baseFile,
"templates/tableTemplates.gohtml", "templates/tableTemplates.gohtml",
"templates/tableTemplatesPreview.gohtml", "templates/tableTemplatesPreview.gohtml",
)) ))

View File

@ -65,13 +65,14 @@
{{end}} {{end}}
<h2>Now for a person</h2> <h2>Now for a person</h2>
<p>expected to be called with personData</p>
{{ define "personBlocks" }} {{ define "personBlocks" }}
<div class="person-bricks " <div class="person-bricks "
id="person-{{.Index}}" id="person-{{.Index}}"
style="--i: {{ .Index }}" style="--i: {{ .Index }}"
> >
{{ range (bricksForPerson .) }} {{ range .BricksForPerson }}
{{ template "brick" . }} {{ template "brick" . }}
{{ end }} {{ end }}
<p>{{ .Name }}</p> <p>{{ .Name }}</p>
@ -81,8 +82,8 @@
<h2>And now i'll want to get all persons for a room</h2> <h2>And now i'll want to get all persons for a room</h2>
<p>expected be called with room *roomTableData</p>
{{ define "roomPeople" }} {{ define "roomPeople" }}
{{ with (personsFromRoom .)}}
<div <div
class="circle-container relative" class="circle-container relative"
style="--tan: {{ .Tangens }}; --m: {{ .Total }};" style="--tan: {{ .Tangens }}; --m: {{ .Total }};"
@ -92,4 +93,3 @@
{{ end }} {{ end }}
</div> </div>
{{end}} {{end}}
{{end}}