From 3c63ebc6135c4a7c1a1ac42d500c4e1a481f5984 Mon Sep 17 00:00:00 2001 From: Harrison Deng Date: Wed, 26 May 2021 16:57:45 -0500 Subject: [PATCH] Moved search bar into separate component and fixed results display. --- src/MultiShop/Client/Listing/ListingView.cs | 5 --- src/MultiShop/Client/Listing/TableView.razor | 4 -- src/MultiShop/Client/Pages/Search.razor | 18 +++++--- src/MultiShop/Client/Pages/Search.razor.cs | 43 ++++++++++++-------- src/MultiShop/Client/Shared/SearchBar.razor | 26 ++++++++++++ 5 files changed, 64 insertions(+), 32 deletions(-) create mode 100644 src/MultiShop/Client/Shared/SearchBar.razor diff --git a/src/MultiShop/Client/Listing/ListingView.cs b/src/MultiShop/Client/Listing/ListingView.cs index bbe13ec..2a01f08 100644 --- a/src/MultiShop/Client/Listing/ListingView.cs +++ b/src/MultiShop/Client/Listing/ListingView.cs @@ -17,10 +17,5 @@ namespace MultiShop.Client.Listing public Search.Status Status { get; set; } private protected abstract string GetCategoryTag(ResultsProfile.Category category); - - public ListingView(Search.Status status) - { - this.Status = status; - } } } \ No newline at end of file diff --git a/src/MultiShop/Client/Listing/TableView.razor b/src/MultiShop/Client/Listing/TableView.razor index 0bcbe02..5c12b08 100644 --- a/src/MultiShop/Client/Listing/TableView.razor +++ b/src/MultiShop/Client/Listing/TableView.razor @@ -75,10 +75,6 @@ @code { public override Views View => Views.Table; - public TableView(Search.Status status) : base(status) - { - } - private protected override string GetCategoryTag(ResultsProfile.Category c) { switch (c) diff --git a/src/MultiShop/Client/Pages/Search.razor b/src/MultiShop/Client/Pages/Search.razor index 5494b71..9aba2a9 100644 --- a/src/MultiShop/Client/Pages/Search.razor +++ b/src/MultiShop/Client/Pages/Search.razor @@ -1,14 +1,15 @@ @page "/search/{Query?}" @using MultiShop.Client.Extensions +@using MultiShop.Client.Listing
- -
- - -
+ + + + +
@if (status.SearchConfiguring) { @@ -238,7 +239,12 @@
@if (listings.Count > 0) { - @listingViews[CurrentView] + @switch (CurrentView) + { + case Views.Table: + + break; + } }
diff --git a/src/MultiShop/Client/Pages/Search.razor.cs b/src/MultiShop/Client/Pages/Search.razor.cs index b349776..0fe2b72 100644 --- a/src/MultiShop/Client/Pages/Search.razor.cs +++ b/src/MultiShop/Client/Pages/Search.razor.cs @@ -6,6 +6,7 @@ using System.Threading.Tasks; using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components.Authorization; using Microsoft.Extensions.Logging; +using MultiShop.Client.Shared; using MultiShop.Client.Extensions; using MultiShop.Client.Listing; using MultiShop.Shared.Models; @@ -17,6 +18,10 @@ namespace MultiShop.Client.Pages { [Inject] private ILogger Logger { get; set; } + + [Inject] + private HttpClient Http { get; set; } + [CascadingParameter] Task AuthenticationStateTask { get; set; } @@ -26,13 +31,10 @@ namespace MultiShop.Client.Pages [Parameter] public string Query { get; set; } - [Inject] - private HttpClient Http { get; set; } + private SearchBar searchBar; private Status status = new Status(); - private Dictionary listingViews; - private Views CurrentView = Views.Table; private SearchProfile activeSearchProfile; @@ -46,11 +48,7 @@ namespace MultiShop.Client.Pages await base.OnInitializedAsync(); AuthenticationState authState = await AuthenticationStateTask; - - listingViews = new Dictionary() { - {Views.Table, new TableView(status)} - }; - + if (authState.User.Identity.IsAuthenticated) { Logger.LogDebug($"User \"{authState.User.Identity.Name}\" is authenticated. Checking for saved profiles."); HttpResponseMessage searchProfileResponse = await Http.GetAsync("Profile/Search"); @@ -84,10 +82,21 @@ namespace MultiShop.Client.Pages } } + protected override async Task OnAfterRenderAsync(bool firstRender) { + await base.OnAfterRenderAsync(firstRender); + if (firstRender) { + searchBar.Query = Query; + searchBar.Searching = true; + await PerformSearch(Query); + searchBar.Searching = false; + } + } + private async Task PerformSearch(string query) { if (string.IsNullOrWhiteSpace(query)) return; if (status.Searching) return; + SearchProfile searchProfile = activeSearchProfile.DeepCopy(); status.Searching = true; Logger.LogDebug($"Received search request for \"{query}\"."); resultsChecked = 0; @@ -96,10 +105,10 @@ namespace MultiShop.Client.Pages List>(); foreach (string shopName in Shops.Keys) { - if (activeSearchProfile.ShopStates[shopName]) + if (searchProfile.ShopStates[shopName]) { Logger.LogDebug($"Querying \"{shopName}\" for products."); - Shops[shopName].SetupSession(query, activeSearchProfile.Currency); + Shops[shopName].SetupSession(query, searchProfile.Currency); int shopViableResults = 0; await foreach (ProductListing listing in Shops[shopName]) { @@ -111,12 +120,12 @@ namespace MultiShop.Client.Pages } - if (listing.Shipping == null && !activeSearchProfile.KeepUnknownShipping || (activeSearchProfile.EnableMaxShippingFee && listing.Shipping > activeSearchProfile.MaxShippingFee)) continue; + if (listing.Shipping == null && !searchProfile.KeepUnknownShipping || (searchProfile.EnableMaxShippingFee && listing.Shipping > searchProfile.MaxShippingFee)) continue; float shippingDifference = listing.Shipping != null ? listing.Shipping.Value : 0; - if (!(listing.LowerPrice + shippingDifference >= activeSearchProfile.LowerPrice && (!activeSearchProfile.EnableUpperPrice || listing.UpperPrice + shippingDifference <= activeSearchProfile.UpperPrice))) continue; - if ((listing.Rating == null && !activeSearchProfile.KeepUnrated) && activeSearchProfile.MinRating > (listing.Rating == null ? 0 : listing.Rating)) continue; - if ((listing.PurchaseCount == null && !activeSearchProfile.KeepUnknownPurchaseCount) || activeSearchProfile.MinPurchases > (listing.PurchaseCount == null ? 0 : listing.PurchaseCount)) continue; - if ((listing.ReviewCount == null && !activeSearchProfile.KeepUnknownRatingCount) || activeSearchProfile.MinReviews > (listing.ReviewCount == null ? 0 : listing.ReviewCount)) continue; + if (!(listing.LowerPrice + shippingDifference >= searchProfile.LowerPrice && (!searchProfile.EnableUpperPrice || listing.UpperPrice + shippingDifference <= searchProfile.UpperPrice))) continue; + if ((listing.Rating == null && !searchProfile.KeepUnrated) && searchProfile.MinRating > (listing.Rating == null ? 0 : listing.Rating)) continue; + if ((listing.PurchaseCount == null && !searchProfile.KeepUnknownPurchaseCount) || searchProfile.MinPurchases > (listing.PurchaseCount == null ? 0 : listing.PurchaseCount)) continue; + if ((listing.ReviewCount == null && !searchProfile.KeepUnknownRatingCount) || searchProfile.MinReviews > (listing.ReviewCount == null ? 0 : listing.ReviewCount)) continue; ProductListingInfo info = new ProductListingInfo(listing, shopName); listings.Add(info); @@ -142,7 +151,7 @@ namespace MultiShop.Client.Pages } shopViableResults += 1; - if (shopViableResults >= activeSearchProfile.MaxResults) break; + if (shopViableResults >= searchProfile.MaxResults) break; } Logger.LogDebug($"\"{shopName}\" has completed. There are {listings.Count} results in total."); } diff --git a/src/MultiShop/Client/Shared/SearchBar.razor b/src/MultiShop/Client/Shared/SearchBar.razor new file mode 100644 index 0000000..aa21f8c --- /dev/null +++ b/src/MultiShop/Client/Shared/SearchBar.razor @@ -0,0 +1,26 @@ + +
+ @Append + +
+ +@code { + [Parameter] + public RenderFragment Append { get; set; } + + [Parameter] + public string SearchPlaceholder { get; set; } + + public string Query { get; set; } + + [Parameter] + public EventCallback OnSearchRequested { get; set; } + + public bool Searching { get; set; } + public async Task Search() + { + Searching = true; + await OnSearchRequested.InvokeAsync(Query); + Searching = false; + } +} \ No newline at end of file