about summary refs log tree commit diff stats
path: root/frontend/static
diff options
context:
space:
mode:
authorAlan Pearce2024-05-12 13:18:37 +0200
committerAlan Pearce2024-05-12 13:18:37 +0200
commit84acac6ddf46351d4c049c7bb25d72584b2c0fb7 (patch)
tree624560c54f08e6ca53229a6d6d15ac840dfe4319 /frontend/static
parent00ff3a75357d295d2f7432888a146954ed9f0b7a (diff)
downloadsearchix-84acac6ddf46351d4c049c7bb25d72584b2c0fb7.tar.lz
searchix-84acac6ddf46351d4c049c7bb25d72584b2c0fb7.tar.zst
searchix-84acac6ddf46351d4c049c7bb25d72584b2c0fb7.zip
fix(js): make reload after error execute attempted search
Diffstat (limited to 'frontend/static')
-rw-r--r--frontend/static/search.js13
1 files changed, 7 insertions, 6 deletions
diff --git a/frontend/static/search.js b/frontend/static/search.js
index 15cabe2..0ca6d45 100644
--- a/frontend/static/search.js
+++ b/frontend/static/search.js
@@ -66,6 +66,8 @@ function renderFragmentHTML(html) {
 
 async function getResults(url) {
   try {
+    state.url = url.toJSON();
+    history.pushState(state, null, url);
     const res = await fetch(url, {
       headers: {
         fetch: "true",
@@ -74,7 +76,8 @@ async function getResults(url) {
 
     if (res.ok) {
       state.fragment = await res.text();
-      history.pushState(state, null, url);
+      state.opened = [];
+      history.replaceState(state, null, url);
       renderFragmentHTML(state.fragment);
     } else {
       throw new Error(`${res.status} ${res.statusText}: ${await res.text()}`);
@@ -83,16 +86,14 @@ async function getResults(url) {
     range.deleteContents();
     range.insertNode(new Text(error.message));
     console.error("fetch failed", error);
-  } finally {
-    state.url = url.toJSON();
-    state.opened = [];
-    history.pushState(state, null, url);
   }
 }
 
 search.addEventListener("submit", function (ev) {
   const url = new URL(this.action);
-  url.search = new URLSearchParams(new FormData(this)).toString();
+  const formData = new FormData(this);
+  url.search = new URLSearchParams(formData).toString();
+  state.input = formData.get("query");
   getResults(url);
   ev.preventDefault();
 });