Skip to content

Commit

Permalink
remove Tavenem.Blazor.IndexedDB
Browse files Browse the repository at this point in the history
  • Loading branch information
neozhu committed Dec 14, 2024
1 parent 070e155 commit 2329759
Show file tree
Hide file tree
Showing 24 changed files with 201 additions and 244 deletions.
2 changes: 1 addition & 1 deletion src/CleanAspire.Api/CleanAspire.Api.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<PackageReference Include="Microsoft.Extensions.ApiDescription.Server" Version="9.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="9.0.0" />
<PackageReference Include="Microsoft.Extensions.Identity.Core" Version="9.0.0" />
<PackageReference Include="Scalar.AspNetCore" Version="1.2.49" />
<PackageReference Include="Scalar.AspNetCore" Version="1.2.55" />
<PackageReference Include="Scrutor" Version="5.0.2" />
<PackageReference Include="SixLabors.ImageSharp" Version="3.1.6" />
<PackageReference Include="StrongGrid" Version="0.110.0" />
Expand Down
2 changes: 1 addition & 1 deletion src/CleanAspire.Application/CleanAspire.Application.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="ZiggyCreatures.FusionCache" Version="2.0.0-preview-2" />
<PackageReference Include="ZiggyCreatures.FusionCache" Version="2.0.0-preview-3" />
<ProjectReference Include="..\CleanAspire.Domain\CleanAspire.Domain.csproj" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ public GetProductByIdQueryHandler(IApplicationDbContext dbContext)
{
throw new KeyNotFoundException($"Product with Id '{request.Id}' was not found.");
}

return product;
}
}
13 changes: 6 additions & 7 deletions src/CleanAspire.ClientApp/CleanAspire.ClientApp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,17 @@
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Http" Version="9.0.0 " />
<PackageReference Include="Blazored.LocalStorage" Version="4.5.0" />
<PackageReference Include="Microsoft.Kiota.Abstractions" Version="1.15.2" />
<PackageReference Include="Microsoft.Kiota.Http.HttpClientLibrary" Version="1.15.2" />
<PackageReference Include="Microsoft.Kiota.Abstractions" Version="1.16.0" />
<PackageReference Include="Microsoft.Kiota.Http.HttpClientLibrary" Version="1.16.0" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="9.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="9.0.0" PrivateAssets="all" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Authentication" Version="9.0.0" />
<PackageReference Include="Microsoft.Kiota.Serialization.Form" Version="1.15.2" />
<PackageReference Include="Microsoft.Kiota.Serialization.Json" Version="1.15.2" />
<PackageReference Include="Microsoft.Kiota.Serialization.Multipart" Version="1.15.2" />
<PackageReference Include="Microsoft.Kiota.Serialization.Text" Version="1.15.2" />
<PackageReference Include="Microsoft.Kiota.Serialization.Form" Version="1.16.0" />
<PackageReference Include="Microsoft.Kiota.Serialization.Json" Version="1.16.0" />
<PackageReference Include="Microsoft.Kiota.Serialization.Multipart" Version="1.16.0" />
<PackageReference Include="Microsoft.Kiota.Serialization.Text" Version="1.16.0" />
<PackageReference Include="MudBlazor" Version="8.0.0-preview.5" />
<PackageReference Include="OneOf" Version="3.0.271" />
<PackageReference Include="Tavenem.Blazor.IndexedDB" Version="4.1.4" />
</ItemGroup>

<ItemGroup>
Expand Down
13 changes: 9 additions & 4 deletions src/CleanAspire.ClientApp/Components/WebpushrSetup.razor
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
@code {
@inject OnlineStatusInterop OnlineStatusInterop
@code {
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
var publicKey = await WebpushrService.GetPublicKeyAsync();
var webpushr = new Webpushr(JS);
await webpushr.SetupWebpushrAsync(publicKey!);
var online = await OnlineStatusInterop.GetOnlineStatusAsync();
if (online)
{
var publicKey = await WebpushrService.GetPublicKeyAsync();
var webpushr = new Webpushr(JS);
await webpushr.SetupWebpushrAsync(publicKey!);
}
}
}
}
32 changes: 1 addition & 31 deletions src/CleanAspire.ClientApp/DependencyInjection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,7 @@
using CleanAspire.ClientApp.Services.Interfaces;
using CleanAspire.ClientApp.Services.UserPreferences;
using CleanAspire.ClientApp.Services;
using System.Text.Json;
using CleanAspire.ClientApp.IndexDb;
using System.Text.Json.Serialization.Metadata;
using CleanAspire.Api.Client.Models;
using System.Text.Json.Serialization;
using Tavenem.DataStorage;

namespace CleanAspire.ClientApp;

public static class DependencyInjection
Expand Down Expand Up @@ -49,30 +44,5 @@ public static void TryAddMudBlazor(this IServiceCollection services, IConfigurat
#endregion
}

public static void AddIndexedDbService(this IServiceCollection services, IConfiguration config)
{
var options = new JsonSerializerOptions(JsonSerializerDefaults.Web);
options.TypeInfoResolverChain.Add(LocalItemContext.Default.WithAddedModifier(static typeInfo =>
{
if (typeInfo.Type == typeof(IIdItem))
{
typeInfo.PolymorphismOptions ??= new JsonPolymorphismOptions
{
IgnoreUnrecognizedTypeDiscriminators = true,
UnknownDerivedTypeHandling = JsonUnknownDerivedTypeHandling.FallBackToNearestAncestor,
};
typeInfo.PolymorphismOptions.DerivedTypes.Add(new JsonDerivedType(typeof(LocalCredential), LocalCredential.ItemTypeName));
typeInfo.PolymorphismOptions.DerivedTypes.Add(new JsonDerivedType(typeof(LocalProfileResponse), LocalProfileResponse.ItemTypeName));
}
}));

services.AddIndexedDbService();
services.AddIndexedDb(
LocalItemContext.DATABASENAME,
1,
options);
}


}

25 changes: 0 additions & 25 deletions src/CleanAspire.ClientApp/IndexDb/LocalCredential.cs

This file was deleted.

18 changes: 0 additions & 18 deletions src/CleanAspire.ClientApp/IndexDb/LocalItemContext.cs

This file was deleted.

31 changes: 0 additions & 31 deletions src/CleanAspire.ClientApp/IndexDb/LocalProfileResponse.cs

This file was deleted.

23 changes: 0 additions & 23 deletions src/CleanAspire.ClientApp/IndexDb/LocalWebpushrOptions.cs

This file was deleted.

4 changes: 3 additions & 1 deletion src/CleanAspire.ClientApp/Pages/Products/Index.razor
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
@page "/products/index"
@using CleanAspire.ClientApp.Pages.Products.Components
@using CleanAspire.ClientApp.Services.Proxies
@inject ProductServiceProxy ProductServiceProxy

<PageTitle>@Title</PageTitle>

Expand Down Expand Up @@ -82,7 +84,7 @@ RowStyleFunc="_rowStyleFunc"
query.Keywords = _keywords;
query.OrderBy = state.SortDefinitions.FirstOrDefault()?.SortBy ?? "Id";
query.SortDirection = state.SortDefinitions.FirstOrDefault()?.Descending ?? true ? SortDirection.Descending.ToString() : SortDirection.Ascending.ToString();
var result = await ApiClient.Products.Pagination.PostAsync(query);
var result = await ProductServiceProxy.GetProductsAsync(query);
return new GridData<ProductDto> { TotalItems = (int)result.TotalItems, Items = result.Items };
}
finally
Expand Down
8 changes: 3 additions & 5 deletions src/CleanAspire.ClientApp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
using CleanAspire.ClientApp;
using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.Extensions.Options;
using Microsoft.Kiota.Abstractions.Authentication;
using Microsoft.Kiota.Http.HttpClientLibrary;
using CleanAspire.ClientApp.Services.Identity;
Expand All @@ -14,8 +13,8 @@
using Microsoft.Kiota.Serialization.Form;
using Microsoft.Kiota.Serialization.Multipart;
using CleanAspire.ClientApp.Services;
using Microsoft.Extensions.DependencyInjection;
using CleanAspire.ClientApp.Services.JsInterop;
using CleanAspire.ClientApp.Services.Proxies;

var builder = WebAssemblyHostBuilder.CreateDefault(args);

Expand All @@ -28,6 +27,8 @@
builder.Services.AddSingleton<UserProfileStore>();
builder.Services.AddSingleton<OnlineStatusInterop>();
builder.Services.AddSingleton<OfflineModeState>();
builder.Services.AddSingleton<IndexedDbCache>();
builder.Services.AddSingleton<ProductServiceProxy>();

var clientAppSettings = builder.Configuration.GetSection(ClientAppSettings.KEY).Get<ClientAppSettings>();
builder.Services.AddSingleton(clientAppSettings!);
Expand Down Expand Up @@ -88,11 +89,8 @@

builder.Services.AddLocalization(options => options.ResourcesPath = "Resources");

builder.Services.AddIndexedDbService(builder.Configuration);

var app = builder.Build();



await app.RunAsync();

1 change: 0 additions & 1 deletion src/CleanAspire.ClientApp/Services/DialogServiceHelper.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using CleanAspire.ClientApp.Components;
using Microsoft.AspNetCore.Components;
using Microsoft.Extensions.Options;
using MudBlazor;

namespace CleanAspire.ClientApp.Services;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,24 @@
using System.Security.Claims;
using CleanAspire.Api.Client;
using CleanAspire.Api.Client.Models;
using CleanAspire.ClientApp.IndexDb;
using CleanAspire.ClientApp.Services.JsInterop;
using Microsoft.AspNetCore.Components.Authorization;



using Microsoft.Kiota.Abstractions;
using Tavenem.Blazor.IndexedDB;


namespace CleanAspire.ClientApp.Services.Identity;

public class CookieAuthenticationStateProvider(ApiClient apiClient, UserProfileStore profileStore, IServiceProvider serviceProvider) : AuthenticationStateProvider, ISignInManagement
{

private const string CACHEKEY_CREDENTIAL = "_Credential";
private bool authenticated = false;
private readonly ClaimsPrincipal unauthenticated = new(new ClaimsIdentity());
public override async Task<AuthenticationState> GetAuthenticationStateAsync()
{
var indexedDb = serviceProvider.GetRequiredKeyedService<IndexedDb>(LocalItemContext.DATABASENAME);
var indexedDb = serviceProvider.GetRequiredService<IndexedDbCache>();
var onlineStatusInterop = serviceProvider.GetRequiredService<OnlineStatusInterop>();
var offlineState = serviceProvider.GetRequiredService<OfflineModeState>();
bool enableOffline = offlineState.Enabled;
Expand All @@ -38,44 +40,12 @@ public override async Task<AuthenticationState> GetAuthenticationStateAsync()
// store the profile to indexedDB
if (profileResponse != null && enableOffline)
{
var exists = await indexedDb[LocalItemContext.STORENAME].Query<LocalProfileResponse>().AnyAsync(x => x.UserId == profileResponse.UserId);
if (!exists)
{
await indexedDb[LocalItemContext.STORENAME].StoreItemAsync(new LocalProfileResponse()
{
AvatarUrl = profileResponse.AvatarUrl,
Email = profileResponse.Email,
LanguageCode = profileResponse.LanguageCode,
Nickname = profileResponse.Nickname,
Provider = profileResponse.Provider,
SuperiorId = profileResponse.SuperiorId,
TenantId = profileResponse.TenantId,
TimeZoneId = profileResponse.TimeZoneId,
UserId = profileResponse.UserId,
Username = profileResponse.Username,
});
}
await indexedDb.SaveDataAsync(IndexedDbCache.DATABASENAME, CACHEKEY_CREDENTIAL, profileResponse);
}
}
else if (enableOffline)
{
var localProfile = await indexedDb[LocalItemContext.STORENAME].Query<LocalProfileResponse>().FirstOrDefaultAsync();
if (localProfile != null)
{
profileResponse = new ProfileResponse()
{
AvatarUrl = localProfile.AvatarUrl,
Email = localProfile.Email,
LanguageCode = localProfile.LanguageCode,
Nickname = localProfile.Nickname,
Provider = localProfile.Provider,
SuperiorId = localProfile.SuperiorId,
TenantId = localProfile.TenantId,
TimeZoneId = localProfile.TimeZoneId,
UserId = localProfile.UserId,
Username = localProfile.Username,
};
}
profileResponse = await indexedDb.GetDataAsync<ProfileResponse>(IndexedDbCache.DATABASENAME, CACHEKEY_CREDENTIAL);
}

profileStore.Set(profileResponse);
Expand Down Expand Up @@ -105,7 +75,7 @@ await indexedDb[LocalItemContext.STORENAME].StoreItemAsync(new LocalProfileRespo

public async Task LoginAsync(LoginRequest request, bool remember = true, CancellationToken cancellationToken = default)
{
var indexedDb = serviceProvider.GetRequiredKeyedService<IndexedDb>("CleanAspire.IndexedDB");
var indexedDb = serviceProvider.GetRequiredService<IndexedDbCache>();
var onlineStatusInterop = serviceProvider.GetRequiredService<OnlineStatusInterop>();
var offlineState = serviceProvider.GetRequiredService<OfflineModeState>();
bool offlineModel = offlineState.Enabled;
Expand All @@ -123,26 +93,13 @@ public async Task LoginAsync(LoginRequest request, bool remember = true, Cancell
if (offlineModel)
{
// Store response in IndexedDB for offline access
var exists = await indexedDb[LocalItemContext.STORENAME].Query<LocalCredential>().AnyAsync(x => x.AccessToken == request.Email);
if (!exists)
{
await indexedDb[LocalItemContext.STORENAME].StoreItemAsync(new LocalCredential()
{
AccessToken = request.Email,
ExpiresIn = int.MaxValue,
RefreshToken = request.Email,
TokenType = "Email"
});
}
await indexedDb.SaveDataAsync(IndexedDbCache.DATABASENAME,request.Email!, request.Email);
}
}
else if (offlineModel)
{
// Offline login logic
var storedToken = await indexedDb[LocalItemContext.STORENAME]
.Query<LocalCredential>()
.FirstOrDefaultAsync(x => x.AccessToken == request.Email);

var storedToken = await indexedDb.GetDataAsync<string>(IndexedDbCache.DATABASENAME, request.Email!);
if (storedToken == null)
{
throw new InvalidOperationException("No offline data available for the provided email.");
Expand Down
Loading

0 comments on commit 2329759

Please sign in to comment.