Skip to content

Commit

Permalink
Removed hardcoded form field names
Browse files Browse the repository at this point in the history
  • Loading branch information
fingon committed Apr 28, 2024
1 parent 10edc9b commit b8999f7
Show file tree
Hide file tree
Showing 12 changed files with 171 additions and 102 deletions.
4 changes: 0 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,3 @@ local Lixie instance which probably does not exist.

- style the pages properly (right now just basic Bootstrap and one or two
ugly bits remain)

- perhaps submit names + fields should still be from variables and not
hardcoded to templ files? problem is, the golang tags cannot(?) be based
on variables anyway
4 changes: 3 additions & 1 deletion config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,7 @@ const primaryStreamKey = "source"
// This is global user-specific configuration from/to cookie
type GlobalConfig struct {
// Full Text Search string
Search string `json:"search" cm:"gsearch"`
Search string `json:"s" cm:"gsearch"`
}

const globalSearchKey = "gsearch" // must match cm: tag above
30 changes: 15 additions & 15 deletions log.templ
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
package main

import "github.com/fingon/lixie/data"
import "fmt"
import "strconv"

templ LogListRuleLink(rule *data.LogRule) {
Expand Down Expand Up @@ -44,23 +43,23 @@ templ LogListTable(m LogListModel) {
<td>
if m.Config.Expand != log.Hash() && !m.DisableActions {
{primaryStreamKey}={log.Stream[primaryStreamKey]}
<a href={m.Config.WithExpand(log.Hash()).ToLink()}>
<a href={m.Config.WithExpand(log.Hash()).Query().ToLink()}>
+{strconv.Itoa(len(log.StreamKeys)-1)}
</a>
} else {
<a href={m.Config.WithExpand(0).ToLink()}>-</a>
<a href={m.Config.WithExpand(0).Query().ToLink()}>-</a>
for _, k := range log.StreamKeys {
{k}={log.Stream[k]}<br/>
}
}
</td>
<td>
if m.Config.Expand != log.Hash() && !m.DisableActions {
<a href={m.Config.WithExpand(log.Hash()).ToLink()}>
<a href={m.Config.WithExpand(log.Hash()).Query().ToLink()}>
+{strconv.Itoa(len(log.FieldsKeys))}
</a>
} else {
<a href={m.Config.WithExpand(0).ToLink()}>-</a>
<a href={m.Config.WithExpand(0).Query().ToLink()}>-</a>
for _, k := range log.FieldsKeys {
{k}={toJSON(log.Fields[k])}<br/>
}
Expand All @@ -87,7 +86,7 @@ templ LogListTable(m LogListModel) {
hx-trigger="revealed"
hx-swap="outerHTML"
hx-select="tbody > tr"
hx-get={m.Config.WithBeforeHash(m.Logs[len(m.Logs)-1].Hash()).ToLinkString()}>
hx-get={m.Config.WithBeforeHash(m.Logs[len(m.Logs)-1].Hash()).Query().ToLinkString()}>
Loading More...
</span>
</td>
Expand All @@ -102,21 +101,23 @@ templ LogList(m LogListModel) {
@Row("refresh-and-count") {
@Col(3) {
if m.Config.AutoRefresh {
<a class="btn btn-sm btn-primary" href={m.Config.ToLink2("ar=false")}>Turn off autorefresh</a>
<div hx-get={m.Config.ToLinkString()}
<a class="btn btn-sm btn-primary"
href={m.Config.Query().Add(llAutoRefreshKey, "false").ToLink()}>Turn off autorefresh</a>
<div hx-get={m.Config.Query().ToLinkString()}
hx-trigger="every 1s"
hx-target="#container"
hx-select="#container" />
} else {
<a class="btn btn-sm btn-primary" href={m.Config.ToLink2("ar=true")}>Turn on autorefresh</a>
<a class="btn btn-sm btn-primary" href={m.Config.ToLink()}>Refresh</a>
<a class="btn btn-sm btn-primary"
href={m.Config.Query().Add(llAutoRefreshKey, "true").ToLink()}>Turn on autorefresh</a>
<a class="btn btn-sm btn-primary" href={m.Config.Query().ToLink()}>Refresh</a>
}
}
@Col(2) {
<form>
<input class="form-text" type="text" name="gsearch"
<input class="form-text" type="text" name={globalSearchKey}
hx-trigger="change, keyup delay:200ms changed"
hx-post={m.Config.ToLinkString()}
hx-post={m.Config.Query().ToLinkString()}
hx-select="#logs"
hx-swap="outerHTML"
hx-target="#logs"
Expand All @@ -128,11 +129,10 @@ templ LogList(m LogListModel) {
for verdict := range data.NumLogVerdicts {
<li class="nav-item">
if m.Config.Filter == verdict {
<a class="nav-link active bg-success-subtle" href={m.Config.ToLink()}>
<a class="nav-link active bg-success-subtle" href={m.Config.Query().ToLink()}>
{data.LogVerdictToString(verdict)} filtered</a>
} else {
<a class="nav-link" href={m.Config.ToLink2(
fmt.Sprintf("f=%d", verdict))}>
<a class="nav-link" href={m.Config.Query().Add(llFilterKey, strconv.Itoa(verdict)).ToLink()}>
No {data.LogVerdictToString(verdict)}</a>
}
</li>
Expand Down
45 changes: 15 additions & 30 deletions log_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@ package main

import (
"net/http"
"net/url"
"strconv"

"github.com/a-h/templ"
"github.com/fingon/lixie/cm"
"github.com/fingon/lixie/data"
)
Expand All @@ -23,16 +21,21 @@ type LogListConfig struct {
Global GlobalConfig

// Local state (cookie)
AutoRefresh bool `json:"ar" cm:"ar"`
Filter int `json:"f" cm:"f"`
AutoRefresh bool `json:"ar" cm:"auto-refresh"`
Filter int `json:"f" cm:"filter"`

// These are only handled via links
BeforeHash uint64
Expand uint64
}

const expandKey = "exp"
const beforeKey = "b"
// These must match ^ cm tags
const llAutoRefreshKey = "auto-refresh"
const llFilterKey = "filter"

// These are handled only in templates
const expandKey = "expand"
const beforeKey = "before"

func (self *LogListConfig) Init(s cm.CookieSource, wr *cm.URLWrapper, w http.ResponseWriter) error {
// Global config
Expand Down Expand Up @@ -72,40 +75,22 @@ func (self LogListConfig) WithExpand(v uint64) LogListConfig {
return self
}

func (self LogListConfig) ToLinkString2(extra string) string {
func (self LogListConfig) Query() *QueryWrapper {
base := topLevelLog.Path + "/"
v := url.Values{}

q := QueryWrapper{Base: base}

// Cookie-based stuff is handled in Init

// Link-based things start here
if self.BeforeHash != 0 {
v.Set(beforeKey, strconv.FormatUint(self.BeforeHash, 10))
q.Add(beforeKey, strconv.FormatUint(self.BeforeHash, 10))
}
if self.Expand != 0 {
v.Set(expandKey, strconv.FormatUint(self.Expand, 10))
q.Add(expandKey, strconv.FormatUint(self.Expand, 10))
}
switch {
case len(v) > 0 && extra != "":
return base + "?" + extra + "&" + v.Encode()
case len(v) > 0:
return base + "?" + v.Encode()
case extra != "":
return base + "?" + extra
}
return base
}

func (self LogListConfig) ToLinkString() string {
return self.ToLinkString2("")
}

func (self LogListConfig) ToLink2(extra string) templ.SafeURL {
return templ.URL(self.ToLinkString2(extra))
}

func (self LogListConfig) ToLink() templ.SafeURL {
return self.ToLink2("")
return &q
}

type LogListModel struct {
Expand Down
2 changes: 1 addition & 1 deletion log_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (

func TestLogList(t *testing.T) {
conf := LogListConfig{Expand: uint64(42), BeforeHash: 13}
s := conf.ToLinkString()
s := conf.Query().ToLinkString()
u, err := url.Parse(s)
assert.Equal(t, err, nil)

Expand Down
2 changes: 1 addition & 1 deletion log_rule.templ
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ templ LogRuleList(m LogRuleListModel) {
}
@Col(2) {
<form>
<input class="form-text" type="text" name="gsearch"
<input class="form-text" type="text" name={globalSearchKey}
hx-trigger="change, keyup delay:200ms changed"
hx-post={m.Config.ToLinkString()}
hx-select="#rules"
Expand Down
14 changes: 11 additions & 3 deletions log_rule_templ.go

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

3 changes: 2 additions & 1 deletion log_rule_templ.txt
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ Spam
\">Loading More...</span></td></tr>
</tbody></table>

<form><input class=\"form-text\" type=\"text\" name=\"gsearch\" hx-trigger=\"change, keyup delay:200ms changed\" hx-post=\"
<form><input class=\"form-text\" type=\"text\" name=\"
\" hx-trigger=\"change, keyup delay:200ms changed\" hx-post=\"
\" hx-select=\"#rules\" hx-swap=\"outerHTML\" hx-target=\"#rules\" value=\"
\" placeholder=\"Search for text\"></form>

Loading

0 comments on commit b8999f7

Please sign in to comment.