Skip to content

Commit

Permalink
commit
Browse files Browse the repository at this point in the history
  • Loading branch information
neozhu committed Dec 13, 2024
1 parent 3d26e92 commit 9991c89
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 18 deletions.
18 changes: 3 additions & 15 deletions src/CleanAspire.ClientApp/Components/WebpushrSetup.razor
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,9 @@
{
if (firstRender)
{
var result = await ApiClientService.ExecuteAsync(() => ApiClient.Webpushr.Config.GetAsync());
result.Switch(
async ok =>
{
var webpushr = new Webpushr(JS);
await webpushr.SetupWebpushrAsync(ok.PublicKey!);
},
invalid =>
{
Snackbar.Add(L["Invalid configuration received. Please check the Webpushr settings."], Severity.Error);
},
error =>
{
Snackbar.Add(L["An error occurred while fetching the Webpushr configuration. Please try again later."], Severity.Error);
});
var publicKey = await WebpushrService.GetPublicKeyAsync();
var webpushr = new Webpushr(JS);
await webpushr.SetupWebpushrAsync(publicKey!);
}
}
}
1 change: 1 addition & 0 deletions src/CleanAspire.ClientApp/Services/JsInterop/Webpushr.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using CleanAspire.Api.Client;
using Microsoft.JSInterop;

namespace CleanAspire.ClientApp.Services.JsInterop;
Expand Down
33 changes: 30 additions & 3 deletions src/CleanAspire.ClientApp/Services/WebpushrService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,47 @@

using System.Net.Http.Headers;
using System.Text;
using CleanAspire.Api.Client;
using CleanAspire.Api.Client.Models;
using CleanAspire.ClientApp.Services.Interfaces;

namespace CleanAspire.ClientApp.Services;

public class WebpushrService
{
private readonly IHttpClientFactory _httpClientFactory;
private readonly ApiClient _apiClient;
private readonly IStorageService _storageService;
private readonly ILogger<WebpushrService> _logger;

public WebpushrService(IHttpClientFactory httpClientFactory, ILogger<WebpushrService> logger)
private const string WEBPUSHRPUBLICKEY = "_webpushrPublicKey";
public WebpushrService(IHttpClientFactory httpClientFactory, ApiClient apiClient, IStorageService storageService, ILogger<WebpushrService> logger)
{
_httpClientFactory = httpClientFactory;
_apiClient = apiClient;
_storageService = storageService;
_logger = logger;
}

public async Task<string> GetPublicKeyAsync()
{
try
{
var publicKey = await _storageService.GetItemAsync<string>(WEBPUSHRPUBLICKEY);
if (string.IsNullOrEmpty(publicKey))
{
var res = await _apiClient.Webpushr.Config.GetAsync();
if (res != null)
{
await _storageService.SetItemAsync(WEBPUSHRPUBLICKEY, res.PublicKey);
publicKey = res.PublicKey;
}
}
return publicKey??string.Empty;
}
catch (Exception ex)
{
return string.Empty;
}
}
public async Task SendNotificationAsync(string title, string message, string targetUrl, string? sid = null)
{
var client = _httpClientFactory.CreateClient("Webpushr");
Expand Down

0 comments on commit 9991c89

Please sign in to comment.