2025-10-07 23:40:43 +03:30
|
|
|
using Blazored.LocalStorage;
|
|
|
|
|
using FrontOffice.Main.Utilities;
|
|
|
|
|
using Microsoft.AspNetCore.Components;
|
2025-09-28 01:28:44 +03:30
|
|
|
using Microsoft.JSInterop;
|
|
|
|
|
using MudBlazor;
|
|
|
|
|
|
|
|
|
|
namespace FrontOffice.Main.Shared;
|
|
|
|
|
public partial class MainLayout
|
|
|
|
|
{
|
2025-10-07 23:40:43 +03:30
|
|
|
private const string TokenStorageKey = "auth:token";
|
|
|
|
|
|
2025-09-28 01:28:44 +03:30
|
|
|
private MudThemeProvider _mudThemeProvider;
|
2025-09-28 10:55:21 +03:30
|
|
|
private bool _isDark;
|
|
|
|
|
private bool _drawerOpen;
|
2025-10-07 23:40:43 +03:30
|
|
|
private bool _isAuthenticated;
|
2025-09-28 10:55:21 +03:30
|
|
|
private string? _email;
|
|
|
|
|
|
2025-10-07 23:40:43 +03:30
|
|
|
[Inject] private ILocalStorageService LocalStorage { get; set; } = default!;
|
|
|
|
|
|
2025-09-28 10:55:21 +03:30
|
|
|
private void ToggleTheme() => _isDark = !_isDark;
|
|
|
|
|
private void ToggleDrawer() => _drawerOpen = !_drawerOpen;
|
2025-09-28 01:28:44 +03:30
|
|
|
private async void Back()
|
|
|
|
|
{
|
|
|
|
|
await JSRuntime.InvokeVoidAsync("history.back");
|
|
|
|
|
}
|
2025-10-07 23:40:43 +03:30
|
|
|
|
2025-10-07 00:39:38 +03:30
|
|
|
protected override async Task OnAfterRenderAsync(bool firstRender)
|
|
|
|
|
{
|
|
|
|
|
if (firstRender)
|
|
|
|
|
{
|
|
|
|
|
await JSRuntime.InvokeVoidAsync("changeNavBgOnBodyScroll", "top", null, 1);
|
2025-10-07 23:40:43 +03:30
|
|
|
await CheckAuthStatus();
|
|
|
|
|
StateHasChanged();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task CheckAuthStatus()
|
|
|
|
|
{
|
|
|
|
|
var token = await LocalStorage.GetItemAsync<string>(TokenStorageKey);
|
|
|
|
|
_isAuthenticated = !string.IsNullOrWhiteSpace(token);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task OpenAuthDialog()
|
|
|
|
|
{
|
|
|
|
|
var dialog = await DialogService.ShowAsync<AuthDialog>("ورود به حساب کاربری");
|
|
|
|
|
var result = await dialog.Result;
|
|
|
|
|
|
|
|
|
|
if (!result.Canceled)
|
|
|
|
|
{
|
|
|
|
|
await CheckAuthStatus();
|
2025-10-20 20:55:55 +03:30
|
|
|
Snackbar.Add(GlobalConstants.SuccessMsg, Severity.Success);
|
2025-10-07 00:39:38 +03:30
|
|
|
StateHasChanged();
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-10-07 23:40:43 +03:30
|
|
|
|
|
|
|
|
private void NavigateToProfile()
|
|
|
|
|
{
|
|
|
|
|
Navigation.NavigateTo(RouteConstants.Profile.Index);
|
|
|
|
|
}
|
2025-10-20 21:02:13 +03:30
|
|
|
|
|
|
|
|
private async Task Logout()
|
|
|
|
|
{
|
|
|
|
|
await LocalStorage.RemoveItemAsync(TokenStorageKey);
|
|
|
|
|
await LocalStorage.RemoveItemAsync("auth:phone-number");
|
|
|
|
|
await LocalStorage.RemoveItemAsync("auth:redirect");
|
|
|
|
|
await LocalStorage.RemoveItemAsync("referral:code");
|
|
|
|
|
|
|
|
|
|
_isAuthenticated = false;
|
|
|
|
|
Snackbar.Add("با موفقیت از حساب کاربری خارج شدید.", Severity.Success);
|
|
|
|
|
StateHasChanged();
|
|
|
|
|
|
|
|
|
|
// Navigate to home page
|
|
|
|
|
Navigation.NavigateTo(RouteConstants.Main.MainPage);
|
|
|
|
|
}
|
2025-09-28 01:28:44 +03:30
|
|
|
}
|