Files
FrontOffice/src/FrontOffice.Main/Shared/MainLayout.razor.cs

69 lines
1.9 KiB
C#

using Blazored.LocalStorage;
using FrontOffice.Main.Utilities;
using Microsoft.AspNetCore.Components;
using Microsoft.JSInterop;
using MudBlazor;
using Microsoft.AspNetCore.Components.Authorization;
namespace FrontOffice.Main.Shared;
public partial class MainLayout
{
private const string TokenStorageKey = "auth:token";
private MudThemeProvider _mudThemeProvider;
private bool _isDark;
private bool _drawerOpen;
private bool _isAuthenticated;
private string? _email;
[Inject] private ILocalStorageService LocalStorage { get; set; } = default!;
[Inject] private AuthService AuthService { get; set; } = default!;
[Inject] private AuthDialogService AuthDialogService { get; set; } = default!;
private void ToggleTheme() => _isDark = !_isDark;
private void ToggleDrawer() => _drawerOpen = !_drawerOpen;
private async void Back()
{
await JSRuntime.InvokeVoidAsync("history.back");
}
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
await JSRuntime.InvokeVoidAsync("changeNavBgOnBodyScroll", "top", null, 1);
await CheckAuthStatus();
StateHasChanged();
}
}
private async Task CheckAuthStatus()
{
_isAuthenticated = await AuthService.IsAuthenticatedAsync();
}
private async Task OpenAuthDialog()
{
await AuthDialogService.ShowAuthDialogAsync();
await CheckAuthStatus();
if (_isAuthenticated)
{
Snackbar.Add(GlobalConstants.SuccessMsg, Severity.Success);
}
StateHasChanged();
Navigation.NavigateTo(RouteConstants.Main.MainPage);
}
private void NavigateToProfile()
{
Navigation.NavigateTo(RouteConstants.Profile.Index);
}
private async Task Logout()
{
await AuthService.LogoutAsync();
_isAuthenticated = false;
StateHasChanged();
}
}