From 84acac6ddf46351d4c049c7bb25d72584b2c0fb7 Mon Sep 17 00:00:00 2001 From: Alan Pearce Date: Sun, 12 May 2024 13:18:37 +0200 Subject: fix(js): make reload after error execute attempted search --- frontend/static/search.js | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) (limited to 'frontend/static') 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(); }); -- cgit 1.4.1