Skip to content

Commit

Permalink
Run all components on InteractiveServer for now
Browse files Browse the repository at this point in the history
  • Loading branch information
mitch-b committed Jan 8, 2025
1 parent 0147993 commit 00ffaef
Show file tree
Hide file tree
Showing 10 changed files with 36 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
@inject IWishlistService WishlistService
@inject NavigationManager NavigationManager

@rendermode InteractiveAuto
@rendermode InteractiveServer

<h3>Add Item to Wishlist</h3>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@
@using Microsoft.Extensions.Logging
@using OpenWish.Web.Client.Components.Wishlist

@inject NavigationManager NavigationManager
@inject ILogger<Index> Logger
@inject IUserContextService UserContextService
@inject IWishlistService WishlistService

@rendermode InteractiveAuto
@rendermode InteractiveServer

<h3>My Wishlists</h3>

Expand Down Expand Up @@ -37,6 +36,10 @@ else
</div>
}

<div class="mt-4">
<p>RenderMode: @RendererInfo.Name , Interactive: @RendererInfo.IsInteractive.ToString()</p>
</div>

@code {
private IEnumerable<WishlistModel>? wishlists;
private string? _userId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
@inject IUserContextService UserContextService
@inject ILogger<NewWishlist> Logger

@rendermode InteractiveAuto
@rendermode InteractiveServer

<h3>Create a Wishlist</h3>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
@inject IUserContextService UserContextService
@inject IWishlistService WishlistService

@rendermode InteractiveAuto
@rendermode InteractiveServer

<PageTitle>@(_wishlist is not null ? _wishlist?.Name : "") Wishlist</PageTitle>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,9 @@ public static class ServiceCollectionExtensions
{
public static IServiceCollection AddOpenWishWasmClientServices(this IServiceCollection services, IConfiguration configuration)
{
// authentication
//services.AddScoped<BaseAddressAuthorizationMessageHandler>();
//services.RemoveAll<AuthenticationStateProvider>();
// https://jonhilton.net/blazor-share-auth-state/
services.AddSingleton<AuthenticationStateProvider, PersistentAuthenticationStateProvider>();
// services.TryAddScoped<AuthenticationStateProvider, PersistentAuthenticationStateProvider>();

services.AddScoped<IUserContextService, UserContextService>();
services.AddScoped<IWishlistService, WishlistHttpClientService>();
Expand Down
3 changes: 3 additions & 0 deletions src/OpenWish.Web.Client/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@
builder.Services.AddAuthorizationCore();
builder.Services.AddCascadingAuthenticationState();
builder.Services.AddAuthenticationStateDeserialization();
// .AddApiAuthorization(); causes an error when running the app from WASM - Specified cast is invalid.
// TODO: investigate the error and can do InteractiveAuto render mode...
builder.Services.AddApiAuthorization();
//builder.Services.AddSingleton<AuthenticationStateProvider, PersistentAuthenticationStateProvider>();

// Register HttpClient with authorization
builder.Services.AddHttpClient("OpenWish.API", client => client.BaseAddress = new Uri(builder.HostEnvironment.BaseAddress))
Expand Down
22 changes: 10 additions & 12 deletions src/OpenWish.Web.Client/Services/WishlistHttpClientService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,67 +6,65 @@ namespace OpenWish.Web.Client.Services;

public class WishlistHttpClientService(HttpClient httpClient) : IWishlistService
{
private readonly HttpClient _httpClient = httpClient;

public async Task<WishlistModel> CreateWishlistAsync(WishlistModel wishlist, string ownerId)
{
var response = await _httpClient.PostAsJsonAsync("api/wishlists", wishlist);
var response = await httpClient.PostAsJsonAsync("api/wishlists", wishlist);
response.EnsureSuccessStatusCode();
return await response.Content.ReadFromJsonAsync<WishlistModel>();
}

public async Task<WishlistModel> GetWishlistAsync(int id)
{
return await _httpClient.GetFromJsonAsync<WishlistModel>($"api/wishlists/{id}");
return await httpClient.GetFromJsonAsync<WishlistModel>($"api/wishlists/{id}");
}

public async Task<IEnumerable<WishlistModel>> GetUserWishlistsAsync(string userId)
{
// Assuming userId is not required since the server knows the authenticated user
return await _httpClient.GetFromJsonAsync<IEnumerable<WishlistModel>>("api/wishlists");
return await httpClient.GetFromJsonAsync<IEnumerable<WishlistModel>>("api/wishlists");
}

public async Task<WishlistModel> UpdateWishlistAsync(int id, WishlistModel wishlist)
{
var response = await _httpClient.PutAsJsonAsync($"api/wishlists/{id}", wishlist);
var response = await httpClient.PutAsJsonAsync($"api/wishlists/{id}", wishlist);
response.EnsureSuccessStatusCode();
return await response.Content.ReadFromJsonAsync<WishlistModel>();
}

public async Task DeleteWishlistAsync(int id)
{
var response = await _httpClient.DeleteAsync($"api/wishlists/{id}");
var response = await httpClient.DeleteAsync($"api/wishlists/{id}");
response.EnsureSuccessStatusCode();
}

// Wishlist Items
public async Task<WishlistItemModel> AddItemToWishlistAsync(int wishlistId, WishlistItemModel item)
{
var response = await _httpClient.PostAsJsonAsync($"api/wishlists/{wishlistId}/items", item);
var response = await httpClient.PostAsJsonAsync($"api/wishlists/{wishlistId}/items", item);
response.EnsureSuccessStatusCode();
return await response.Content.ReadFromJsonAsync<WishlistItemModel>();
}

public async Task<WishlistItemModel> GetWishlistItemAsync(int wishlistId, int itemId)
{
return await _httpClient.GetFromJsonAsync<WishlistItemModel>($"api/wishlists/{wishlistId}/items/{itemId}");
return await httpClient.GetFromJsonAsync<WishlistItemModel>($"api/wishlists/{wishlistId}/items/{itemId}");
}

public async Task<IEnumerable<WishlistItemModel>> GetWishlistItemsAsync(int wishlistId)
{
return await _httpClient.GetFromJsonAsync<IEnumerable<WishlistItemModel>>($"api/wishlists/{wishlistId}/items");
return await httpClient.GetFromJsonAsync<IEnumerable<WishlistItemModel>>($"api/wishlists/{wishlistId}/items");
}

public async Task<WishlistItemModel> UpdateWishlistItemAsync(int wishlistId, int itemId, WishlistItemModel item)
{
var response = await _httpClient.PutAsJsonAsync($"api/wishlists/{wishlistId}/items/{itemId}", item);
var response = await httpClient.PutAsJsonAsync($"api/wishlists/{wishlistId}/items/{itemId}", item);
response.EnsureSuccessStatusCode();
return await response.Content.ReadFromJsonAsync<WishlistItemModel>();
}

public async Task<bool> RemoveItemFromWishlistAsync(int wishlistId, int itemId)
{
var response = await _httpClient.DeleteAsync($"api/wishlists/{wishlistId}/items/{itemId}");
var response = await httpClient.DeleteAsync($"api/wishlists/{wishlistId}/items/{itemId}");
if (response.IsSuccessStatusCode)
{
return true;
Expand Down
1 change: 1 addition & 0 deletions src/OpenWish.Web.Client/_Imports.razor
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
@using Microsoft.AspNetCore.Components.Forms
@using Microsoft.AspNetCore.Components.Routing
@using Microsoft.AspNetCore.Components.Web
@using Microsoft.AspNetCore.Components.WebAssembly.Authentication
@using static Microsoft.AspNetCore.Components.Web.RenderMode
@using Microsoft.AspNetCore.Components.Web.Virtualization
@using Microsoft.JSInterop
Expand Down
22 changes: 12 additions & 10 deletions src/OpenWish.Web/Components/Routes.razor
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
<Router AppAssembly="typeof(Program).Assembly" AdditionalAssemblies="new[] { typeof(Client._Imports).Assembly }">
<Found Context="routeData">
<AuthorizeRouteView RouteData="routeData" DefaultLayout="typeof(Layout.MainLayout)">
<NotAuthorized>
<RedirectToLogin />
</NotAuthorized>
</AuthorizeRouteView>
<FocusOnNavigate RouteData="routeData" Selector="h1" />
</Found>
</Router>
<CascadingAuthenticationState>
<Router AppAssembly="typeof(Program).Assembly" AdditionalAssemblies="new[] { typeof(Client._Imports).Assembly }">
<Found Context="routeData">
<AuthorizeRouteView RouteData="routeData" DefaultLayout="typeof(Layout.MainLayout)">
<NotAuthorized>
<RedirectToLogin />
</NotAuthorized>
</AuthorizeRouteView>
<FocusOnNavigate RouteData="routeData" Selector="h1" />
</Found>
</Router>
</CascadingAuthenticationState>
1 change: 1 addition & 0 deletions src/OpenWish.Web/Components/_Imports.razor
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
@using Microsoft.AspNetCore.Components.Forms
@using Microsoft.AspNetCore.Components.Routing
@using Microsoft.AspNetCore.Components.Web
@using Microsoft.AspNetCore.Components.WebAssembly.Authentication
@using static Microsoft.AspNetCore.Components.Web.RenderMode
@using Microsoft.AspNetCore.Components.Web.Virtualization
@using Microsoft.JSInterop
Expand Down

0 comments on commit 00ffaef

Please sign in to comment.