Skip to content

Commit

Permalink
feat(settings): add setting for toggling search suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
scottmckendry committed Aug 25, 2024
1 parent 07c2b26 commit f526619
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 53 deletions.
12 changes: 8 additions & 4 deletions data/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ type Mapping struct {
}

type UserSettings struct {
SearchEngine string
LeaderKey string
SearchEngine string
LeaderKey string
ShowSuggestions bool
}

func NewStore(db *sql.DB) *Storage {
Expand Down Expand Up @@ -280,6 +281,8 @@ func (s *Storage) GetUserSettings(email string) *UserSettings {
settings.SearchEngine = value
case "LeaderKey":
settings.LeaderKey = value
case "ShowSuggestions":
settings.ShowSuggestions = value == "true"
}
}

Expand All @@ -288,8 +291,9 @@ func (s *Storage) GetUserSettings(email string) *UserSettings {

func (s *Storage) UpdateUserSettings(email string, settings *UserSettings) error {
settingsMap := map[string]interface{}{
"SearchEngine": settings.SearchEngine,
"LeaderKey": settings.LeaderKey,
"SearchEngine": settings.SearchEngine,
"LeaderKey": settings.LeaderKey,
"ShowSuggestions": fmt.Sprintf("%t", settings.ShowSuggestions), // "true" or "false
}

for settingKey, settingValue := range settingsMap {
Expand Down
2 changes: 1 addition & 1 deletion handlers/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ func (h *Handler) HandleSettingsUpdate(w http.ResponseWriter, r *http.Request) {
userSettings := h.store.GetUserSettings(user.Email)
userSettings.LeaderKey = r.FormValue("leaderKey")
userSettings.SearchEngine = r.FormValue("searchEngine")
userSettings.ShowSuggestions = r.FormValue("suggestions") == "on"

log.Println(userSettings)
h.store.UpdateUserSettings(user.Email, userSettings)

mappings := h.store.GetMappings(user.Email)
Expand Down
30 changes: 20 additions & 10 deletions views/search.templ
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,26 @@ var searchEngines = map[string]SearchEngine{

templ Search(settings *data.UserSettings) {
<form action={ templ.SafeURL(searchEngines[settings.SearchEngine].URL) } method="get">
<input
type="text"
id="search"
name={ searchEngines[settings.SearchEngine].QueryParam }
placeholder="Search..."
hx-post="/search/suggest"
hx-target="#suggestions"
hx-trigger="input changed delay:500ms"
autocomplete="off"
/>
if settings.ShowSuggestions {
<input
type="text"
id="search"
name={ searchEngines[settings.SearchEngine].QueryParam }
placeholder="Search..."
hx-post="/search/suggest"
hx-target="#suggestions"
hx-trigger="input changed delay:500ms"
autocomplete="off"
/>
} else {
<input
type="text"
id="search"
name={ searchEngines[settings.SearchEngine].QueryParam }
placeholder="Search..."
autocomplete="off"
/>
}
// hidden form value that indicates the search engine
<input type="hidden" name="search_engine" value={ settings.SearchEngine }/>
</form>
Expand Down
74 changes: 51 additions & 23 deletions views/search_templ.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 20 additions & 9 deletions views/settings.templ
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,26 @@ import (

templ Settings(user goth.User, settings *data.UserSettings) {
@Modal("Settings") {
<label for="leaderKey">Leader Key</label>
<input type="text" name="leaderKey" value={ settings.LeaderKey } placeholder="Leader Key" data-include-edit=""/>
<label for="searchEngine">Search Engine</label>
<select name="searchEngine" data-include-edit="">
// See search.templ for the full list of search engines
for name := range searchEngines {
<option value={ name } selected?={ settings.SearchEngine == name }>{ name }</option>
}
</select>
<div class="setting">
<label for="leaderKey">Leader Key</label>
<input type="text" name="leaderKey" value={ settings.LeaderKey } placeholder="Leader Key" data-include-edit=""/>
</div>
<div class="setting">
<label for="searchEngine">Search Engine</label>
<select name="searchEngine" data-include-edit="">
// See search.templ for the full list of search engines
for name := range searchEngines {
<option value={ name } selected?={ settings.SearchEngine == name }>{ name }</option>
}
</select>
</div>
<div class="setting">
<label for="suggestions" class="tooltip-container">
Show Google-Powered Suggestions <icon class="bx bx-help-circle"></icon>
<p class="tooltip-text">This feature will send your search queries to Google to provide suggestions - regardless of your search engine choice.</p>
</label>
<input type="checkbox" name="suggestions" checked?={ settings.ShowSuggestions } data-include-edit=""/>
</div>
<div class="action-buttons">
<button class="button" _="on click trigger closeModal" hx-put="/update-settings" hx-include="input[data-include-edit],select[data-include-edit]">Save</button>
</div>
Expand Down
22 changes: 16 additions & 6 deletions views/settings_templ.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f526619

Please sign in to comment.