From 3a2f8d9268fa998e47347ca370fa598a7a3e0ac3 Mon Sep 17 00:00:00 2001 From: Scott McKendry <39483124+scottmckendry@users.noreply.github.com> Date: Thu, 22 Aug 2024 15:38:08 +1200 Subject: [PATCH] refactor(keymaps): establish mapping type --- data/data.go | 34 +++++++++++++++++----------------- handlers/root.go | 3 ++- handlers/settings.go | 15 ++++++++++++++- main.go | 1 + public/js/input.js | 14 +++++++++++++- views/home.templ | 3 ++- views/home_templ.go | 6 +++++- views/page.templ | 1 + views/page_templ.go | 2 +- views/settings.templ | 29 ----------------------------- views/settings_templ.go | 4 ++-- 11 files changed, 58 insertions(+), 54 deletions(-) diff --git a/data/data.go b/data/data.go index cfe9f75..45a4b42 100644 --- a/data/data.go +++ b/data/data.go @@ -3,6 +3,7 @@ package data import ( "database/sql" "fmt" + "log" "github.com/markbates/goth" ) @@ -19,8 +20,13 @@ type User struct { GithubID string } +type Mapping struct { + ID int + Keymap string + MapsTo string +} + type UserSettings struct { - Mappings map[string]string SearchEngine string LeaderKey string } @@ -114,17 +120,10 @@ func buildUserFromGothUser(gothUser goth.User) *User { return user } -func (s *Storage) GetUserSettings(email string) *UserSettings { - settings := &UserSettings{} - settings.Mappings = getMappings(s.db, email) - settings = s.getGenericSettings(email, settings) - - return settings -} - -func getMappings(db *sql.DB, email string) map[string]string { - rows, err := db.Query( - `SELECT keymap, maps_to +func (s *Storage) GetMappings(email string) []Mapping { + mappings := []Mapping{} + rows, err := s.db.Query( + `SELECT mappings.id, keymap, maps_to FROM mappings INNER JOIN users ON mappings.user_id = users.id @@ -132,25 +131,26 @@ func getMappings(db *sql.DB, email string) map[string]string { email, ) if err != nil { + log.Println(err) return nil } defer rows.Close() - mappings := make(map[string]string) for rows.Next() { - var keymap, mapsTo string - err = rows.Scan(&keymap, &mapsTo) + mapping := Mapping{} + err = rows.Scan(&mapping.ID, &mapping.Keymap, &mapping.MapsTo) if err != nil { return nil } - mappings[keymap] = mapsTo + mappings = append(mappings, mapping) } return mappings } -func (s *Storage) getGenericSettings(email string, settings *UserSettings) *UserSettings { +func (s *Storage) GetUserSettings(email string) *UserSettings { + settings := &UserSettings{} rows, err := s.db.Query( `SELECT setting_key, setting_value FROM user_settings diff --git a/handlers/root.go b/handlers/root.go index 2f860ee..3456e82 100644 --- a/handlers/root.go +++ b/handlers/root.go @@ -15,6 +15,7 @@ func (h *Handler) HandleRoot(w http.ResponseWriter, r *http.Request) { } userSettings := h.store.GetUserSettings(user.Email) + mappings := h.store.GetMappings(user.Email) - views.Home(user, userSettings).Render(r.Context(), w) + views.Home(user, userSettings, mappings).Render(r.Context(), w) } diff --git a/handlers/settings.go b/handlers/settings.go index 3e7e35f..ce23b80 100644 --- a/handlers/settings.go +++ b/handlers/settings.go @@ -18,6 +18,17 @@ func (h *Handler) HandleSettings(w http.ResponseWriter, r *http.Request) { views.Settings(user, userSettings).Render(r.Context(), w) } +func (h *Handler) HandleMappings(w http.ResponseWriter, r *http.Request) { + user, err := h.auth.GetSessionUser(r) + if err != nil { + log.Println(err) + return + } + + mappings := h.store.GetMappings(user.Email) + views.Mappings(user, mappings).Render(r.Context(), w) +} + func (h *Handler) HandleSettingsUpdate(w http.ResponseWriter, r *http.Request) { user, err := h.auth.GetSessionUser(r) if err != nil { @@ -36,6 +47,8 @@ func (h *Handler) HandleSettingsUpdate(w http.ResponseWriter, r *http.Request) { userSettings.SearchEngine = r.FormValue("searchEngine") h.store.UpdateUserSettings(user.Email, userSettings) + mappings := h.store.GetMappings(user.Email) + w.Header().Set("HX-Refresh", "true") - views.Home(user, userSettings).Render(r.Context(), w) + views.Home(user, userSettings, mappings).Render(r.Context(), w) } diff --git a/main.go b/main.go index 35d6710..f34a698 100644 --- a/main.go +++ b/main.go @@ -41,6 +41,7 @@ func main() { // app routes r.HandleFunc("/", auth.RequireAuth(handler.HandleRoot, authService)) r.HandleFunc("/settings", auth.RequireAuth(handler.HandleSettings, authService)) + r.HandleFunc("/mappings", auth.RequireAuth(handler.HandleMappings, authService)) r.HandleFunc("/update-settings", auth.RequireAuth(handler.HandleSettingsUpdate, authService)) // auth diff --git a/public/js/input.js b/public/js/input.js index eb9a678..758c547 100644 --- a/public/js/input.js +++ b/public/js/input.js @@ -1,11 +1,14 @@ let inputSequence = []; let leaderMode = false; +const mappingsArray = JSON.parse( + document.getElementById("mappings").textContent, +); const userSettings = JSON.parse( document.getElementById("userSettings").textContent, ); -const keymaps = userSettings.Mappings; +const keymaps = parseMappings(mappingsArray); const leaderKey = userSettings.LeaderKey; document.addEventListener("keydown", (event) => { @@ -86,3 +89,12 @@ document.addEventListener("keydown", (event) => { }); } }); + +// Parses an array of mappings objects into a dictionary +function parseMappings(mappingsArray) { + const mappings = {}; + mappingsArray.forEach((mapping) => { + mappings[mapping["Keymap"]] = mapping["MapsTo"]; + }); + return mappings; +} diff --git a/views/home.templ b/views/home.templ index 9bc22dc..9b94df8 100644 --- a/views/home.templ +++ b/views/home.templ @@ -5,8 +5,9 @@ import ( "github.com/scottmckendry/mnemstart/data" ) -templ Home(user goth.User, settings *data.UserSettings) { +templ Home(user goth.User, settings *data.UserSettings, mappings []data.Mapping) { @templ.JSONScript("userSettings", settings) + @templ.JSONScript("mappings", mappings) @Page(true, user) {