Refactored `AuthService` to introduce a reusable `CreateDefaultClient` method, reducing code duplication. Updated all relevant methods in `AuthService` to use this new method. Added `CultureService` to manage application culture/localization, including support for setting, getting, and initializing culture from `localStorage` or browser settings. Introduced `DocReceiverElementService` for retrieving document receiver elements (signatures) and `EnvelopeService` for managing envelope data retrieval with optional filters. Both services include error handling and consistent JSON deserialization. These changes improve code maintainability, reusability, and adhere to the single responsibility principle.
75 lines
2.1 KiB
C#
75 lines
2.1 KiB
C#
using System.Globalization;
|
|
using Microsoft.JSInterop;
|
|
|
|
namespace EnvelopeGenerator.Server.Client.Services;
|
|
|
|
/// <summary>
|
|
/// Service for managing application culture/localization.
|
|
/// </summary>
|
|
public class CultureService
|
|
{
|
|
private readonly IJSRuntime _jsRuntime;
|
|
private const string CULTURE_KEY = "AppCulture";
|
|
|
|
public CultureService(IJSRuntime jsRuntime)
|
|
{
|
|
_jsRuntime = jsRuntime;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets the list of supported cultures.
|
|
/// </summary>
|
|
public static CultureInfo[] SupportedCultures { get; } = new[]
|
|
{
|
|
new CultureInfo("de-DE"),
|
|
new CultureInfo("en-US"),
|
|
new CultureInfo("fr-FR")
|
|
};
|
|
|
|
/// <summary>
|
|
/// Sets the application culture and stores it in localStorage.
|
|
/// </summary>
|
|
public async Task SetCultureAsync(string culture)
|
|
{
|
|
if (!SupportedCultures.Any(c => c.Name == culture))
|
|
throw new ArgumentException($"Culture '{culture}' is not supported.", nameof(culture));
|
|
|
|
await _jsRuntime.InvokeVoidAsync("localStorage.setItem", CULTURE_KEY, culture);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets the stored culture from localStorage.
|
|
/// </summary>
|
|
public async Task<string?> GetCultureAsync()
|
|
{
|
|
try
|
|
{
|
|
return await _jsRuntime.InvokeAsync<string?>("localStorage.getItem", CULTURE_KEY);
|
|
}
|
|
catch
|
|
{
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Initializes the culture from localStorage or browser settings.
|
|
/// </summary>
|
|
public async Task<CultureInfo> InitializeCultureAsync()
|
|
{
|
|
var storedCulture = await GetCultureAsync();
|
|
|
|
if (!string.IsNullOrEmpty(storedCulture) &&
|
|
SupportedCultures.Any(c => c.Name == storedCulture))
|
|
{
|
|
return new CultureInfo(storedCulture);
|
|
}
|
|
|
|
// Fallback to browser culture or default
|
|
var browserCulture = CultureInfo.CurrentCulture.Name;
|
|
var matchedCulture = SupportedCultures.FirstOrDefault(c => c.Name == browserCulture);
|
|
|
|
return matchedCulture ?? SupportedCultures[0]; // Default to German
|
|
}
|
|
}
|