From 9834693fabc9854d60efda0ade4ad3f23eae96e9 Mon Sep 17 00:00:00 2001 From: Alex Date: Mon, 18 Dec 2023 14:42:25 -0600 Subject: [PATCH] fix(web): access /search throw error (#5834) --- web/src/routes/(user)/search/+page.svelte | 10 +++++----- web/src/routes/(user)/search/+page.ts | 8 ++++++-- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/web/src/routes/(user)/search/+page.svelte b/web/src/routes/(user)/search/+page.svelte index eb8341a799..e8bc0c08d4 100644 --- a/web/src/routes/(user)/search/+page.svelte +++ b/web/src/routes/(user)/search/+page.svelte @@ -36,7 +36,7 @@ // behavior for history.back(). To prevent that we store the previous page // manually and navigate back to that. let previousRoute = AppRoute.EXPLORE as string; - $: albums = data.results.albums.items; + $: albums = data.results?.albums.items; const onKeyboardPress = (event: KeyboardEvent) => handleKeyboardPress(event); @@ -98,10 +98,10 @@ $: isMultiSelectionMode = selectedAssets.size > 0; $: isAllArchived = Array.from(selectedAssets).every((asset) => asset.isArchived); $: isAllFavorite = Array.from(selectedAssets).every((asset) => asset.isFavorite); - $: searchResultAssets = data.results.assets.items; + $: searchResultAssets = data.results?.assets.items; const onAssetDelete = (assetId: string) => { - searchResultAssets = searchResultAssets.filter((a: AssetResponseDto) => a.id !== assetId); + searchResultAssets = searchResultAssets?.filter((a: AssetResponseDto) => a.id !== assetId); }; const handleSelectAll = () => { selectedAssets = new Set(searchResultAssets); @@ -140,7 +140,7 @@
- {#if albums.length} + {#if albums && albums.length}
ALBUMS
@@ -161,7 +161,7 @@
{/if}
- {#if searchResultAssets.length > 0} + {#if searchResultAssets && searchResultAssets.length > 0}
diff --git a/web/src/routes/(user)/search/+page.ts b/web/src/routes/(user)/search/+page.ts index 1f2cffcbaf..c76de05d0c 100644 --- a/web/src/routes/(user)/search/+page.ts +++ b/web/src/routes/(user)/search/+page.ts @@ -1,12 +1,16 @@ import { authenticate } from '$lib/utils/auth'; -import { api } from '@api'; +import { SearchResponseDto, api } from '@api'; import type { PageLoad } from './$types'; export const load = (async (data) => { await authenticate(); const url = new URL(data.url.href); const term = url.searchParams.get('q') || url.searchParams.get('query') || undefined; - const { data: results } = await api.searchApi.search({}, { params: url.searchParams }); + let results: SearchResponseDto | null = null; + if (term) { + const { data } = await api.searchApi.search({}, { params: url.searchParams }); + results = data; + } return { term,