aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRunxi Yu <me@runxiyu.org>2024-10-01 19:32:50 +0800
committerRunxi Yu <me@runxiyu.org>2024-10-01 19:32:50 +0800
commit296274ac60f89352d3e7e1e40e1f88a74a128a61 (patch)
treee101381dbc2d8d661e8f14c69ea3394aa009be28
parentstyle.css: Switch the header to a grey theme (diff)
downloadtest-master.tar.gz
test-master.zip
wsm.go, main.js: Display the user's selected courses on first runHEADv0.1.0master
It's done in the WebSocket routines rather than in the template maker because it's relatively easier to write the logic this way.
-rw-r--r--static/main.js4
-rw-r--r--wsm.go20
2 files changed, 22 insertions, 2 deletions
diff --git a/static/main.js b/static/main.js
index c5c4cbb..3fc1cb0 100644
--- a/static/main.js
+++ b/static/main.js
@@ -50,6 +50,10 @@ socket.addEventListener("open", function() {
document.querySelectorAll(".before-connection").forEach(c => {
c.style.display = "none"
})
+ let courseIDs = mar[1].split(",")
+ for (let i = 0; i < courseIDs.length; i++) {
+ document.getElementById(`tick${ courseIDs[i] }`).checked = true
+ }
break
case "U": /* unauthenticated */
/* TODO: replace this with a box on screen */
diff --git a/wsm.go b/wsm.go
index c00ff32..a0525b3 100644
--- a/wsm.go
+++ b/wsm.go
@@ -25,6 +25,7 @@ import (
"errors"
"fmt"
"strconv"
+ "strings"
"time"
"github.com/coder/websocket"
@@ -33,11 +34,26 @@ import (
)
func messageHello(ctx context.Context, c *websocket.Conn, mar []string, userID string, session string) error {
- _, _, _ = mar, userID, session
- err := writeText(ctx, c, "HI")
+ _, _ = mar, session
+
+ rows, err := db.Query(
+ ctx,
+ "SELECT courseid FROM choices WHERE userid = $1",
+ userID,
+ )
+ if err != nil {
+ return protocolError(ctx, c, "error fetching choices")
+ }
+ courseIDs, err := pgx.CollectRows(rows, pgx.RowTo[string])
+ if err != nil {
+ return protocolError(ctx, c, "error collecting choices")
+ }
+
+ err = writeText(ctx, c, "HI :"+strings.Join(courseIDs, ","))
if err != nil {
return fmt.Errorf("error replying to HELLO: %w", err)
}
+
return nil
}