~netlandish/links

0b0bf23a3f63d334d56dd889b1d0d5b1ada6cdbc — Yader Velasquez 9 months ago 6ddb856
Use url.Value object to store advanced search queries.
Use template func to print and add query params in templates.

References: https://todo.code.netlandish.com/~netlandish/links/47
5 files changed, 22 insertions(+), 7 deletions(-)

M cmd/links/main.go
M core/routes.go
M helpers.go
M static/js/advancedsearch.js
M templates/link_list.html
M cmd/links/main.go => cmd/links/main.go +1 -0
@@ 238,6 238,7 @@ func run() error {
		"isTagUsedInFilter": func(tag string, activeTags string) bool {
			return strings.Contains(activeTags, tag)
		},
		"addQueryElement": links.AddQueryElement,
	})
	err = srv.LoadTemplatesFS(links.TemplateFS, "templates/*.html", "templates/*.txt")
	if err != nil {

M core/routes.go => core/routes.go +7 -2
@@ 1310,19 1310,23 @@ func (s *Service) OrgLinksList(c echo.Context) error {
		excludeTag,
		search string
	)
	queries := make(url.Values)

	if c.QueryParam("tag") != "" {
		tag = c.QueryParam("tag")
		op.Var("tag", tag)
		queries.Add("tag", tag)
	}
	if c.QueryParam("exclude") != "" {
		excludeTag = c.QueryParam("exclude")
		op.Var("excludeTag", excludeTag)
		queries.Add("exclude", excludeTag)

	}
	if c.QueryParam("q") != "" {
		search = c.QueryParam("q")
		op.Var("search", search)
		queries.Add("q", search)
	}

	err := links.Execute(links.LangContext(c), op, &result)


@@ 1375,14 1379,15 @@ func (s *Service) OrgLinksList(c echo.Context) error {
		"org":              org,
		"isOrgLink":        isOrgLink,
		"canRead":          canRead,
		"tagFilter":        tag,
		"excludeTagFilter": excludeTag,
		"tagFilter":        strings.Replace(tag, ",", ", ", -1),
		"excludeTagFilter": strings.Replace(excludeTag, ",", ", ", -1),
		"navFlag":          navFlag,
		"advancedSearch":   advancedSearch,
		"hasUnreadFilter":  hasUnreadFilter,
		"hasStarredFilter": hasStarredFilter,
		"hasAllFilter":     hasAllFilter,
		"currURL":          currURL,
		"queries":          queries,
	}

	if search != "" {

M helpers.go => helpers.go +11 -0
@@ 791,3 791,14 @@ func (t TagQuery) GetSubQuery(inputTag, inputExcludeTag *string) (string, []inte

	return subQ.ToSql()
}

func AddQueryElement(q url.Values, param, val string) template.URL {
	curVal := q.Get(param)
	if curVal == "" {
		curVal = val
	} else {
		curVal += fmt.Sprintf(",%s", val)
	}
	q.Set(param, curVal)
	return template.URL(q.Encode())
}

M static/js/advancedsearch.js => static/js/advancedsearch.js +2 -2
@@ 79,7 79,7 @@ form.addEventListener("submit", function(e) {
        form.elements.tag.disabled = true;
    }
    else {
        tagValue = tagValue.split(",").map(slugify).join(", ");
        tagValue = tagValue.split(",").map(slugify).join(",");
        if (tagValue.endsWith(",")) {
            tagValue = tagValue.slice(0, -1);
        }


@@ 90,7 90,7 @@ form.addEventListener("submit", function(e) {
        form.elements.exclude.disabled = true;
    }
    else {
        excludeValue = excludeValue.split(",").map(slugify).join(", ");
        excludeValue = excludeValue.split(",").map(slugify).join(",");
        if (excludeValue.endsWith(",")) {
            excludeValue = excludeValue.slice(0, -1);
        }

M templates/link_list.html => templates/link_list.html +1 -3
@@ 151,9 151,7 @@
            {{if isTagUsedInFilter .Slug $.tagFilter}}
                <small class="link-tag__item link-tag__item--simple">#{{.Name}}</small>
            {{else}}
                <!-- NOTE change to url.Values -->
                <a class="link-tag__item link-tag__item--simple"
                   href="{{if $.search}}?q={{$.search}}&{{else}}?{{end}}{{if $.tagFilter}}tag={{$.tagFilter}}{{urlquery (printf ", %s" .Slug)}}{{if $.excludeTagFilter}}&exclude={{$.excludeTagFilter}}{{end}}{{else}}tag={{.Slug}}{{if $.excludeTagFilter}}&exclude={{$.excludeTagFilter}}{{end}}{{end}}">#{{.Name}}</a>
                <a href="{{if $.queries}}?{{addQueryElement $.queries "tag" .Slug}}{{else}}?tag={{.Slug}}{{end}}" class="link-tag__item link-tag__item--simple">#{{.Name}}</a>
            {{end}}
          {{end}}
        </div>