create function for displaying query result toasts to reduce code duplication

This commit is contained in:
maxDorninger
2025-10-27 21:49:26 +01:00
parent 994e558090
commit b846480b84
3 changed files with 13 additions and 4 deletions

View File

@@ -99,3 +99,10 @@ export function formatSecondsToOptimalUnit(seconds: number): string {
return '0s';
}
export function handleQueryNotificationToast(count: number = 0, query: string = "") {
if (count > 0 && query.length > 0) toast.success(`Found ${count} ${count > 1 ? 'result' : 'results'} for search term "${query}".`);
else if (count == 0) toast.info(`No results found for "${query}".`);
}

View File

@@ -14,6 +14,7 @@
import { base } from '$app/paths';
import client from '$lib/api';
import type { components } from '$lib/api/api';
import {handleQueryNotificationToast} from "$lib/utils.ts";
let searchTerm: string = $state('');
let metadataProvider: 'tmdb' | 'tvdb' = $state('tmdb');
@@ -36,12 +37,11 @@
})
: await client.GET('/api/v1/movies/recommended');
if (data && data.length > 0) {
toast.success(`Found ${data.length} result(s) for "${query}".`);
results = data as components['schemas']['MetaDataProviderSearchResult'][];
} else {
toast.info(`No results found for "${query}".`);
results = null;
}
handleQueryNotificationToast(data?.length ?? 0, query)
}
</script>

View File

@@ -18,6 +18,7 @@
import { resolve } from '$app/paths';
import client from '$lib/api';
import type { components } from '$lib/api/api';
import {handleQueryNotificationToast} from "$lib/utils.ts";
onMount(() => {
search('');
@@ -36,12 +37,13 @@
})
: await client.GET('/api/v1/tv/recommended');
if (results.data && results.data.length > 0) {
toast.success(`Found ${results.data.length} result(s) for "${query}".`);
handleQueryNotificationToast(results.data.length, query)
data = results.data as components['schemas']['MetaDataProviderSearchResult'][];
} else {
toast.info(`No results found for "${query}".`);
handleQueryNotificationToast(0, query)
data = null;
}
}
</script>