From 8a748e281c2790f3a492c9258151b7bd220af9ae Mon Sep 17 00:00:00 2001 From: MuslemRahimi Date: Fri, 7 Mar 2025 11:48:21 +0100 Subject: [PATCH] add loading bar to search --- src/lib/components/Searchbar.svelte | 142 ++++++++++++++++------------ 1 file changed, 84 insertions(+), 58 deletions(-) diff --git a/src/lib/components/Searchbar.svelte b/src/lib/components/Searchbar.svelte index eed17af5..e7964f79 100644 --- a/src/lib/components/Searchbar.svelte +++ b/src/lib/components/Searchbar.svelte @@ -73,7 +73,7 @@ closePopup?.dispatchEvent(new MouseEvent("click")); } - await goto( + goto( `/${assetType === "ETF" ? "etf" : assetType === "Index" ? "index" : "stocks"}/${symbol}`, ); @@ -168,8 +168,8 @@ `/api/searchbar?query=${encodeURIComponent(inputValue)}&limit=10`, ); searchBarData = await response?.json(); - }, 50); // delay - isLoading = false; + isLoading = false; + }, 300); // delay } function handleKeyDown(symbol) { @@ -251,7 +251,7 @@ ) { (async () => { // Add 500 ms delay is important otherwise bug since #each has searchHistory and updates too quickly and redirects to wrong symbol - await new Promise((resolve) => setTimeout(resolve, 500)); + await new Promise((resolve) => setTimeout(resolve, 800)); // Update search history after delay searchHistory = updatedSearchHistory; @@ -330,7 +330,13 @@
- {#if isLoading} + {#if isLoading && inputValue?.length > 0} +
+ +
+ {:else if isLoading} {:else if inputValue?.length > 0}
- - {#if inputValue?.length > 0 && searchBarData?.length > 0} -
- Suggestions -
- {#each searchBarData as item} - handleSearch(item?.symbol, item?.type)} + {#if isLoading && inputValue?.length > 0}{:else} + + {#if inputValue?.length > 0 && searchBarData?.length > 0} +
-
- {item?.symbol} - {item?.name} - {item?.type} -
- - {/each} - {:else if inputValue?.length === 0 || !showSuggestions} -
- {searchHistory?.length > 0 ? "Recent" : "Popular"} -
- {#each searchHistory?.length > 0 ? searchHistory : popularList as item} - handleSearch(item?.symbol, item?.type)} + Suggestions +
+ {#each searchBarData as item} + handleSearch(item?.symbol, item?.type)} + > +
+ {item?.symbol} + {item?.name} + {item?.type} +
+
+ {/each} + {:else if inputValue?.length === 0} +
-
- {item?.symbol} - {item?.name} - {item?.type} -
- - {/each} - {:else} - - No results found - - {/if} - + {searchHistory?.length > 0 ? "Recent" : "Popular"} +
+ {#each searchHistory as item} + handleSearch(item?.symbol, item?.type)} + > +
+ {item?.symbol} + {item?.name} + {item?.type} +
+
+ {/each} + {:else if showSuggestions} + {#each searchHistory as item} + handleSearch(item?.symbol, item?.type)} + > +
+ {item?.symbol} + {item?.name} + {item?.type} +
+
+ {/each} + {:else} + + No results found + + {/if} +
+ {/if} @@ -423,11 +452,11 @@ bind:checked={searchBarModalChecked} /> - + - - -