From 5b9e67fd5129dec75169a1a070c70f910dff6da2 Mon Sep 17 00:00:00 2001 From: Alan Pearce Date: Sat, 4 May 2024 12:54:31 +0200 Subject: feat: frontend search implementation --- frontend/static/search.js | 26 ++++++++++++++++++++++++++ frontend/templates/blocks/options.gotmpl | 13 +++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 frontend/static/search.js create mode 100644 frontend/templates/blocks/options.gotmpl (limited to 'frontend') diff --git a/frontend/static/search.js b/frontend/static/search.js new file mode 100644 index 0000000..20f1a7d --- /dev/null +++ b/frontend/static/search.js @@ -0,0 +1,26 @@ +const search = document.getElementById("search"); +const results = document.getElementById("results"); +search.addEventListener("submit", function (ev) { + const url = new URL(this.action); + url.search = new URLSearchParams(new FormData(this)).toString(); + const res = fetch(url, { + headers: { + fetch: "true", + }, + }) + .then(function (res) { + window.history.pushState(null, null, url); + if (res.ok) { + return res.text(); + } else { + throw new Error(res.statusText); + } + }) + .then(function (html) { + results.innerHTML = html; + }) + .catch(function (error) { + console.error("fetch failed", error); + }); + ev.preventDefault(); +}); diff --git a/frontend/templates/blocks/options.gotmpl b/frontend/templates/blocks/options.gotmpl new file mode 100644 index 0000000..3451eb3 --- /dev/null +++ b/frontend/templates/blocks/options.gotmpl @@ -0,0 +1,13 @@ +{{ define "results" }} + {{ range $opt, $data := .Results }} +
+ + {{ $opt }} + +

+ {{ $data.Description }} +

+
+ {{ end }} + +{{ end }} -- cgit 1.4.1