MRB webassembly
This commit is contained in:
49
MesaFabApproval.Client/Services/LocalStorageService.cs
Normal file
49
MesaFabApproval.Client/Services/LocalStorageService.cs
Normal file
@ -0,0 +1,49 @@
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
using Microsoft.JSInterop;
|
||||
|
||||
namespace MesaFabApproval.Client.Services;
|
||||
|
||||
public interface ILocalStorageService {
|
||||
Task AddItem(string key, string value);
|
||||
Task AddItem<T>(string key, T value);
|
||||
Task RemoveItem(string key);
|
||||
Task<string> GetItem(string key);
|
||||
Task<T?> GetItem<T>(string key);
|
||||
}
|
||||
|
||||
public class LocalStorageService : ILocalStorageService {
|
||||
private readonly IJSRuntime _jsRuntime;
|
||||
|
||||
public LocalStorageService(IJSRuntime jsRuntime) {
|
||||
_jsRuntime = jsRuntime;
|
||||
}
|
||||
|
||||
public async Task AddItem(string key, string value) {
|
||||
await _jsRuntime.InvokeVoidAsync("localStorage.setItem", key, value);
|
||||
}
|
||||
|
||||
public async Task AddItem<T>(string key, T value) {
|
||||
string item = JsonSerializer.Serialize(value);
|
||||
await _jsRuntime.InvokeVoidAsync("localStorage.setItem", key, item);
|
||||
}
|
||||
|
||||
public async Task RemoveItem(string key) {
|
||||
await _jsRuntime.InvokeVoidAsync("localStorage.removeItem", key);
|
||||
}
|
||||
|
||||
public async Task<string> GetItem(string key) {
|
||||
return await _jsRuntime.InvokeAsync<string>("localStorage.getItem", key);
|
||||
}
|
||||
|
||||
public async Task<T?> GetItem<T>(string key) {
|
||||
string item = await _jsRuntime.InvokeAsync<string>("localStorage.getItem", key);
|
||||
|
||||
JsonSerializerOptions jsonSerializerOptions = new() {
|
||||
PropertyNameCaseInsensitive = true
|
||||
};
|
||||
|
||||
return JsonSerializer.Deserialize<T>(item, jsonSerializerOptions);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user