feat: toggling Create/Join room button

This commit is contained in:
efim 2023-10-30 03:49:48 +00:00
parent bd99eaa54d
commit 201b3760da
4 changed files with 52 additions and 13 deletions

View File

@ -40,7 +40,7 @@ func (r *Room) UnmarshalBinary(data []byte) error {
var ctx = context.Background()
type RoomManager interface {
Get(roomName string) (Room, error) // but we need Get by name
Get(roomName string) (Room, bool, error)
Save(room Room) error
}
const roomRedisPrefix = "room"
@ -53,15 +53,18 @@ type RedisRM struct {
Rdb *redis.Client
}
func (redisRM RedisRM) Get(roomName string) (Room, error) {
func (redisRM RedisRM) Get(roomName string) (Room, bool, error) {
var readRoom Room
err := redisRM.Rdb.Get(ctx, roomNameToRedisId(roomName)).Scan(&readRoom)
if err == redis.Nil {
return Room{}, false, nil
}
if err != nil {
log.Printf("error reading room with id %s : %s", roomName, err)
return Room{}, err
return Room{}, false, err
}
return readRoom, nil
return readRoom, true, nil
}
func (redisRM RedisRM) Save(room Room) error {

View File

@ -26,8 +26,8 @@ func registerLoginRoutes(
}
})
// submitting the login info : room name & pwd, personal name & pwd
http.HandleFunc("/login/submit", func(w http.ResponseWriter, r *http.Request) {
// when the room exists - attempt to join
http.HandleFunc("/login/join", func(w http.ResponseWriter, r *http.Request) {
err := r.ParseForm()
if err != nil {
w.WriteHeader(http.StatusBadRequest)
@ -38,7 +38,7 @@ func registerLoginRoutes(
pn := r.PostFormValue("personalName")
pp := r.PostFormValue("personalPassword")
room, err := rooms.Get(rn)
room, _, err := rooms.Get(rn)
if err != nil {
log.Printf("/login/submit error getting room %s", rn)
// return i guess
@ -63,6 +63,30 @@ func registerLoginRoutes(
// d) how should i monitor sessions?
})
// checking whether the room name already exists - change button between Join or Create
// when room does not exist
http.HandleFunc("/login/create", func(w http.ResponseWriter, r *http.Request) {
err := r.ParseForm()
if err != nil {
w.WriteHeader(http.StatusBadRequest)
}
fmt.Fprintf(w, "got submitted form %+v", r.PostForm)
})
// checking whether the room name already exists
// toggle button between Join or Create
http.HandleFunc("/login/room-name-check", func(w http.ResponseWriter, r *http.Request) {
err := r.ParseForm()
if err != nil {
w.WriteHeader(http.StatusBadRequest)
}
roomName := r.PostFormValue("roomName")
_, isFound, err := rooms.Get(roomName)
if err != nil {
log.Printf("/login/room-name-check error finding room %s\n", err)
}
var templFile = "templates/login.gohtml"
tmpl := template.Must(template.ParseFS(templateFs, templFile))
err = tmpl.ExecuteTemplate(w, "formButton", isFound)
})
}

View File

@ -538,6 +538,10 @@ video {
grid-column: 1 / -1;
}
.block {
display: block;
}
.flex {
display: flex;
}

View File

@ -3,7 +3,7 @@
<head>
<meta charset="utf-8" />
<meta http-equiv="x-ua-compatible" content="ie=edge" />
<title>Untitled</title>
<title>Untitled!!!</title>
<meta name="description" content="" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<script
@ -43,7 +43,11 @@
type="text"
name="roomName"
value=""
placeholder="room name"
placeholder="room name!!!"
hx-trigger="keyup changed delay:500ms"
hx-post="/login/room-name-check"
hx-target="#formButton"
hx-swap="outerHTML"
/>
<input
id="roomPassword"
@ -70,9 +74,13 @@
placeholder="personal password"
/>
</div>
<button class="col-span-full"
hx-post="/login/submit"
>Join / Create</button>
{{ block "formButton" .IsRoomExisting }}
{{ if not . }}
<button id="formButton" class="col-span-full" hx-post="/login/create">Create Room</button>
{{ else }}
<button id="formButton" class="col-span-full" hx-post="/login/join">Join Room</button>
{{ end }}
{{ end }}
</form>
</section>
</main>