working on search

This commit is contained in:
2025-08-19 09:08:10 +02:00
parent 9aca5bd088
commit 91147d736d
4 changed files with 56 additions and 21 deletions

View File

@@ -5,6 +5,7 @@ import { useTinyRouter } from 'svelte-tiny-router';
import { logout, user } from '../user.svelte.js'; import { logout, user } from '../user.svelte.js';
import { t } from '../translations.svelte.js'; import { t } from '../translations.svelte.js';
let key = $state(null);
const router = useTinyRouter(); const router = useTinyRouter();
const modules = $state([]); const modules = $state([]);
@@ -27,6 +28,12 @@ function go(path){
return false; return false;
} }
async function search(e){
e.preventDefault();
router.navigate(`/search?key=${key}`);
return false;
}
onMount(fetchModules); onMount(fetchModules);
</script> </script>
@@ -37,6 +44,10 @@ onMount(fetchModules);
</style> </style>
<nav> <nav>
<form onsubmit={search}>
<input type="text" bind:value={key} />
<button type="submit">{t('search')}</button>
</form>
<a href="#" onclick={() => go('/user')}>{t('users')}</a> <a href="#" onclick={() => go('/user')}>{t('users')}</a>
<a href="#" onclick={() => go('/company')}>{t('companies')}</a> <a href="#" onclick={() => go('/company')}>{t('companies')}</a>
<a href="#" onclick={() => go('/project')}>{t('projects')}</a> <a href="#" onclick={() => go('/project')}>{t('projects')}</a>

View File

@@ -1,40 +1,60 @@
<script> <script>
import { onMount } from 'svelte'; import { onMount } from 'svelte';
import { useTinyRouter } from 'svelte-tiny-router';
import { t } from '../../translations.svelte.js'; import { api } from '../../urls.svelte.js';
import { t } from '../../translations.svelte.js';
const router = useTinyRouter();
console.log(router);
let fulltext = false; let fulltext = false;
let html = ""; let key = $state(router.getQueryParam('key'));
let key = ""; let input = $state(router.getQueryParam('key'));
let error = $state(null);
async function doSearch(ev){ async function setKey(ev){
if (ev) ev.preventDefault(); if (ev) ev.preventDefault();
const url = `${location.protocol}//${location.host.replace('5173','8080')}/legacy/search`; key = input;
const resp = await fetch(url,{
credentials : 'include',
method : 'POST',
body : JSON.stringify({key:key,fulltext:fulltext?'on':'off'})
});
if (resp.ok){
html = await resp.text();
if (!html) html = t('nothing_found');
}
} }
onMount(() => { onMount(() => {
let params = new URLSearchParams(location.search); let params = new URLSearchParams(location.search);
key = params.get('key'); key = params.get('key');
if (key) doSearch();
}); });
function doSearch(ignored){
let url = window.location.origin + window.location.pathname;
if (key) url += '?key=' + encodeURI(key);
window.history.replaceState(history.state, '', url);
const data = { key : key, fulltext : fulltext };
fetch(api('bookmark/search'),{
credentials:'include',
method: 'POST',
body: JSON.stringify(data)
}).then(handleBookmarks);
}
async function handleBookmarks(resp){
if (resp.ok){
} else {
error = await resp.text();
}
}
$effect(() => doSearch(key))
</script> </script>
<fieldset class="search"> <fieldset class="search">
<legend>{t('search')}</legend> <legend>{t('search')}</legend>
<form onsubmit={doSearch}> {#if error}
<span class="error">{error}</span>
{/if}
<form onsubmit={setKey}>
<label> <label>
{t('key')} {t('key')}
<input type="text" bind:value={key} /> <input type="text" bind:value={input} />
</label> </label>
<label> <label>
<input type="checkbox" bind:checked={fulltext} /> <input type="checkbox" bind:checked={fulltext} />
@@ -43,11 +63,11 @@
<button type="submit">{t('go')}</button> <button type="submit">{t('go')}</button>
</form> </form>
</fieldset> </fieldset>
{#if html} {#if key}
<fieldset> <fieldset>
<legend> <legend>
{t('results')} {t('results')}
</legend> </legend>
{@html html} {key}
</fieldset> </fieldset>
{/if} {/if}

View File

@@ -12,6 +12,7 @@ include("messages")
include("markdown") include("markdown")
include("notes") include("notes")
include("project") include("project")
include("search")
include("tags") include("tags")
include("task") include("task")
include("time") include("time")

View File

@@ -283,3 +283,6 @@ legend{
vertical-align: top; vertical-align: top;
} }
nav > form{
display:inline;
}