I have a page component and within it a breadcrumbs component and a business data component that displays all of the businesses for that page based on a category alias it is passed through the route.
Some of the breadcrumb links direct the user to another routable/page component and these work fine. However, some of them require a refresh of the business data component and it's these I'm having an issue with.
I've tried creating a service with a event that the breadcrumbs component can invoke when a link is clicked and the data component can subscribe to (following this post), but I can't get that to work as the return type is wrong when I try and call GetBusinesses as a result.
Here is the front end of my page component:
@if (breadcrumbs != null)
{
<Breadcrumbs Items="breadcrumbs"></Breadcrumbs>
}
<BrowseByBusiness Alias="@Alias"></BrowseByBusiness>
I set up my breadcrumbs like so (the Alias on the API call is a route parameter):
protected async override Task OnInitializedAsync()
{
categoryDto = await _publicClient.Client.GetFromJsonAsync<CategoryDto>($"api/Categories/GetCategoryByAlias/{Alias}");
breadcrumbs = new List<BreadcrumbItem>
{
new BreadcrumbItem("Browse By Category", "/browse-by-category"),
new BreadcrumbItem(categoryDto.Name, $"/browse-by-category/{categoryDto.Alias}"),
};
if (categoryDto.ParentCategory.Name != null)
{
breadcrumbs.Insert(1, new BreadcrumbItem(categoryDto.ParentCategory.Name, $"/browse-by-category/{categoryDto.ParentCategory.Alias}"));
}
}
My BrowseByBusiness component:
@foreach (var businessDto in businessesDto)
{
<Animate Animation="Animations.Fade" Easing="Easings.Ease" DurationMilliseconds="2000">
<BusinessCard businessDto="businessDto"></BusinessCard>
</Animate>
}
@code {
[Inject] IBusinessHttpRepository _businessRepo { get; set; }
public MetaData metaData = new MetaData();
IEnumerable<BusinessDto> businessesDto;
protected async override Task OnInitializedAsync()
{
await GetBusinesses();
}
private async Task GetBusinesses()
{
var pagingResponse = await _businessRepo.GetBusinessesByCategory(Alias, businessParameters);
businessesDto = pagingResponse.Items;
metaData = pagingResponse.MetaData;
}
}
My Breadcrumbs component:
<div class="flex-row border-bottom pb-2">
@foreach (var item in Items)
{
if (item.Equals(Last))
{
<span class="me-1">@item.Name</span>
}
else
{
<a class="me-1" href="@item.Url">@item.Name </a>
<span class="me-1">/</span>
}
}
</div>
@code {
[Parameter] public List<BreadcrumbItem> Items { get; set; }
private BreadcrumbItem Last;
protected async override Task OnInitializedAsync()
{
Last = Items.Last();
}
}
I'm using Blazor WASM and .net 6.