refactor: toggling state by same button

This commit is contained in:
efim 2023-11-24 05:13:39 +00:00
parent 4680d96a97
commit 1b91b9f083
3 changed files with 39 additions and 67 deletions

View File

@ -107,22 +107,20 @@ func raiseGestureHandRoute(
) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
gestureInd, err := strconv.Atoi(r.URL.Path)
gesture, found := rooms.GestureFromInt(gestureInd)
selectedGesture, found := rooms.GestureFromInt(gestureInd)
if err != nil || !found {
log.Printf("/rooms/raiseGesture error %s gettin hand symbol index from path %s\n", err, r.URL.Path)
return
}
log.Printf("/rooms/raiseGesture successfully got gesture %d : %s", gesture, gesture.String())
log.Printf("/rooms/raiseGesture successfully got gesture %d : %s", selectedGesture, selectedGesture.String())
session, found := getContextSession(r.Context())
if !found {
log.Printf("/rooms/raiseGesture session not found, should be impossible")
// TODO return error i guess
return
}
var outerClosureRoom rooms.Room
err = roomsM.Update(r.Context(), session.RoomId, func(fromRoom rooms.Room) (toRoom rooms.Room) {
toRoom = fromRoom.RaiseHand(session.PersonId, gesture)
outerClosureRoom = toRoom
toRoom = fromRoom.RaiseHand(session.PersonId, selectedGesture)
return toRoom
})
if err != nil {
@ -130,16 +128,19 @@ func raiseGestureHandRoute(
return
// TODO return error i guess
}
log.Printf(">> i attempt to get room with closure: %+v\n", outerClosureRoom)
tableTemplates := "templates/room.gohtml"
tmpl := template.Must(
template.New("").ParseFS(templateFs, tableTemplates))
gestureData := GestureData{
var gesturesData []GestureData
for _, gesture := range rooms.GesturesHighToLow {
gesturesData = append(gesturesData, GestureData{
Url: fmt.Sprintf("%s%d", raiseHandPath, gesture),
Gesture: gesture,
IsSelected: true,
IsSelected: selectedGesture == gesture,
})
}
err = tmpl.ExecuteTemplate(w, "activeButton", &gestureData)
err = tmpl.ExecuteTemplate(w, "controls", &gesturesData)
if err != nil {
log.Printf("/rooms/releaseHand error saving hand: %s\n", err)
return
@ -171,39 +172,23 @@ func releaseHandRoute(
// TODO return error i guess
}
r.ParseForm() // to return either 'inactive button" or 'no need to change html'
gestureString := r.FormValue("gesture")
if gestureString != "" {
gestureIndex, err := strconv.Atoi(gestureString)
if err != nil {
log.Printf("/rooms/releaseHand error getting gesture index: %s %s", gestureString, err)
return
// TODO return error i guess
}
gesture, ok := rooms.GestureFromInt(gestureIndex)
if !ok {
log.Printf("/rooms/releaseHand error getting gesture: %s %s", gestureString, err)
return
// TODO return error i guess
}
tableTemplates := "templates/room.gohtml"
tmpl := template.Must(
template.New("").ParseFS(templateFs, tableTemplates))
gestureData := GestureData{
var gesturesData []GestureData
for _, gesture := range rooms.GesturesHighToLow {
gesturesData = append(gesturesData, GestureData{
Url: fmt.Sprintf("%s%d", raiseHandPath, gesture),
Gesture: gesture,
IsSelected: false,
})
}
err = tmpl.ExecuteTemplate(w, "inactiveButton", &gestureData)
err = tmpl.ExecuteTemplate(w, "controls", &gesturesData)
if err != nil {
log.Printf("/rooms/releaseHand error saving hand: %s\n", err)
return
// TODO return error i guess
}
} else {
w.WriteHeader(http.StatusNoContent)
}
}
}

View File

@ -697,10 +697,6 @@ video {
border-width: 4px;
}
.border-\[hsl\(var\(--border-color\)\)\] {
border-color: hsl(var(--border-color));
}
.border-\[hsl\(var\(--brick-color\)\)\] {
border-color: hsl(var(--brick-color));
}
@ -709,6 +705,10 @@ video {
border-color: hsl(var(--brick-color) / 0.25);
}
.border-\[hsl\(var\(--color\)\)\] {
border-color: hsl(var(--color));
}
.border-\[hsl\(var\(--color-dark\)\)\] {
border-color: hsl(var(--color-dark));
}

View File

@ -48,31 +48,19 @@
</div>
{{/* This is personal hand controls */}}
<div id="controls" class="bg-green-300 flex flex-col p-10 gap-y-5">
{{ range .Gestures }}
{{ block "controls" .Gestures }}
{{ range . }}
{{/* expects routes.GestureData */}}
<button
{{ if not .IsSelected }}
{{/* expects routes.GestureData */}}
{{ block "inactiveButton" . }}
<button
hx-get="{{ .Url }}"
hx-swap="outerHTML"
class="bg-white rounded border-[hsl(var(--border-color))] border-2 text-[hsl(var(--text-color))] font-bold h-16 rounded-l-full flex flex-row items-center"
style="--border-color: var({{.Gesture.GetGestureInfo.Color}});
--text-color: var({{.Gesture.GetGestureInfo.ColorDark}})
">
<img src="{{.Gesture.GetGestureInfo.IconUrl}}" alt=""
class="h-full"
/>
<p class="px-5 text-l">{{ .Gesture.String }} </p>
</button>
{{ end }}
class="bg-white rounded border-[hsl(var(--color))] border-2 text-[hsl(var(--color-dark))] font-bold h-16 rounded-l-full flex flex-row items-center"
{{ else }}
{{/* expects routes.GestureData */}}
{{ block "activeButton" . }}
<button
hx-get="/rooms/releaseHand?gesture={{ printf "%d" .Gesture}}"
hx-swap="outerHTML"
class="bg-[hsl(var(--color))]/50 rounded border-[hsl(var(--color-dark))] border-2 text-[hsl(var(--color-dark))] font-bold h-16 rounded-l-full flex flex-row items-center"
hx-get="/rooms/releaseHand"
class="bg-[hsl(var(--color))]/50 rounded border-[hsl(var(--color-dark))] border-2 text-[hsl(var(--text-color))] font-bold h-16 rounded-l-full flex flex-row items-center"
{{ end }}
hx-target="#controls"
style="--color: var({{.Gesture.GetGestureInfo.Color}});
--color-dark: var({{.Gesture.GetGestureInfo.ColorDark}})
">
@ -81,10 +69,9 @@
/>
<p class="px-5 text-l">{{ .Gesture.String }} </p>
</button>
{{ end }}
{{ end }}
{{ end }}
{{ end }}
</div>
</div>
</main>