Extract search request URL builder into js function in web interface

This commit is contained in:
Debanjum Singh Solanky
2023-06-27 15:50:41 -07:00
parent 09f739b8cc
commit 1b11d5723d

View File

@@ -93,15 +93,8 @@
if (rerank)
setQueryFieldInUrl(query);
// Generate Backend API URL to execute Search
if (type == 'all')
url = `/api/search?q=${encodeURIComponent(query)}&n=${results_count}&client=web`;
else if (type === "image")
url = `/api/search?q=${encodeURIComponent(query)}&t=${type}&n=${results_count}&client=web`;
else
url = `/api/search?q=${encodeURIComponent(query)}&t=${type}&n=${results_count}&r=${rerank}&client=web`;
// Execute Search and Render Results
url = createRequestUrl(query, type, results_count, rerank);
fetch(url)
.then(response => response.json())
.then(data => {
@@ -157,6 +150,18 @@
});
}
function createRequestUrl(query, results_count, type, rerank) {
// Generate Backend API URL to execute Search
let url = `/api/search?q=${encodeURIComponent(query)}&n=${results_count}&client=web`;
// If type is not 'all', append type to URL
if (type !== 'all')
url += `&t=${type}`;
// Rerank is only supported by text types
if (type !== "image")
url += `&r=${rerank}`;
return url;
}
function setTypeFieldInUrl(type) {
var url = new URL(window.location.href);
url.searchParams.set("t", type.value);