194 lines
6.1 KiB
C#
194 lines
6.1 KiB
C#
using AyCode.Interfaces.StorageHandlers;
|
|
using Microsoft.AspNetCore.Components;
|
|
using Microsoft.Extensions.Localization;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using AyCode.Core.Loggers;
|
|
using AyCode.Services.Loggers;
|
|
using TIAM.Core.Loggers;
|
|
using TIAM.Resources;
|
|
using TIAMSharedUI.Resources;
|
|
using TIAMWebApp.Shared.Application.Interfaces;
|
|
using TIAMWebApp.Shared.Application.Utility;
|
|
using TIAMSharedUI.Pages.Components;
|
|
using TIAMWebApp.Shared.Application.Models.ClientSide;
|
|
using Newtonsoft.Json;
|
|
using Microsoft.AspNetCore.Components.Authorization;
|
|
using Microsoft.JSInterop;
|
|
using Microsoft.EntityFrameworkCore.Metadata.Internal;
|
|
using Microsoft.AspNetCore.Components.Routing;
|
|
using AyCode.Blazor.Components.Services;
|
|
using TIAM.Models;
|
|
|
|
|
|
namespace TIAMSharedUI.Shared.Components
|
|
{
|
|
public partial class Navbar : ComponentBase
|
|
{
|
|
[Inject] public required IEnumerable<IAcLogWriterClientBase> LogWriters { get; set; }
|
|
|
|
[Inject] public ISecureStorageHandler SecureStorageHandler { get; set; }
|
|
|
|
[Inject] public ISessionService sessionService { get; set; }
|
|
[Inject] public IStringLocalizer<TIAMResources> localizer { get; set; }
|
|
|
|
[Inject] public NavigationManager navigationManager { get; set; }
|
|
|
|
[Inject] public IComponentUpdateService componentUpdateService { get; set; }
|
|
|
|
[Inject] private IUserDataService UserDataService { get; set; }
|
|
|
|
[Inject] private IJSRuntime JsRuntime { get; set; }
|
|
|
|
[Inject] AuthenticationStateProvider AuthStateProvider { get; set; }
|
|
|
|
private bool enableLogin = true;
|
|
private bool enableEvents = false;
|
|
private bool enableTransfer = true;
|
|
private bool enableLanguage = true;
|
|
private bool enableApi = true;
|
|
private bool enableChat = true;
|
|
|
|
private string? NavMenuCssClass => collapseNavMenu ? "collapse" : "show";
|
|
private bool collapseNavMenu = true;
|
|
private bool myUser = false;
|
|
private bool hasProperty = false;
|
|
private bool isSysAdmin = false;
|
|
private bool isDevAdmin = false;
|
|
private bool isDriver = false;
|
|
private Guid? userId = null;
|
|
private string userEmail = "";
|
|
private string userFullName = "";
|
|
|
|
private ILogger _logger;
|
|
|
|
//componentUpdateService.RefreshRequested += RefreshMe;
|
|
|
|
public Navbar()
|
|
{
|
|
|
|
}
|
|
|
|
private void RefreshMe()
|
|
{
|
|
_logger.Debug($"Navbar refresh called! {DateTime.Now} ");
|
|
|
|
//OnInitialized();
|
|
InitUser();
|
|
StateHasChanged();
|
|
}
|
|
|
|
private void ToggleNavMenu()
|
|
{
|
|
collapseNavMenu = !collapseNavMenu;
|
|
_logger.Debug($"{NavMenuCssClass}");
|
|
//JsRuntime.InvokeVoidAsync("toggleBootstrapNavMenu", expandNavMenu);
|
|
}
|
|
|
|
private async Task SignOut()
|
|
{
|
|
//SiteViewModel.ClearAll();
|
|
|
|
bool serverResult;
|
|
string userDetailsStr = await SecureStorageHandler.GetFromSecureStorageAsync(nameof(Setting.UserBasicDetails));
|
|
|
|
if (!string.IsNullOrEmpty(userDetailsStr))
|
|
{
|
|
var userBasicDetail = JsonConvert.DeserializeObject<UserBasicDetails>(userDetailsStr);
|
|
serverResult = await UserDataService.Logout(userBasicDetail?.RefreshToken);
|
|
}
|
|
|
|
await SecureStorageHandler.ClearAllSecureStorageAsync();
|
|
var result = await AuthStateProvider.GetAuthenticationStateAsync();
|
|
|
|
sessionService.ClearAll();
|
|
navigationManager.NavigateTo("/");
|
|
myUser = false;
|
|
}
|
|
|
|
|
|
protected override void OnInitialized()
|
|
{
|
|
base.OnInitialized();
|
|
navigationManager.LocationChanged += OnLocationChanged;
|
|
_logger = new LoggerClient<Navbar>(LogWriters.ToArray());
|
|
|
|
|
|
_logger.Debug($"Navbar OnInit {DateTime.Now} ");
|
|
|
|
if (sessionService.IsAuthenticated) InitUser();
|
|
}
|
|
|
|
private void OnLocationChanged(object sender, LocationChangedEventArgs e)
|
|
{
|
|
// Collapse the navbar on navigation
|
|
collapseNavMenu = true;
|
|
_logger.Debug($"{NavMenuCssClass}");
|
|
//JsRuntime.InvokeVoidAsync("toggleBootstrapNavMenu", false);
|
|
StateHasChanged();
|
|
}
|
|
|
|
private void NavToAcc()
|
|
{
|
|
var url = $"user/account/{userId}";
|
|
navigationManager.NavigateTo(url);
|
|
}
|
|
|
|
private void NavToTransfers()
|
|
{
|
|
var url = $"user/transfers/{userId}";
|
|
navigationManager.NavigateTo(url);
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
// Unsubscribe from the event when the component is disposed
|
|
navigationManager.LocationChanged -= OnLocationChanged;
|
|
}
|
|
|
|
private void InitUser()
|
|
{
|
|
if (sessionService.User != null)
|
|
{
|
|
myUser = true;
|
|
userId = sessionService.User.UserId;
|
|
userEmail = sessionService.User.Email;
|
|
|
|
if (sessionService.User.UserModelDto.ProfileDto.FullName != null)
|
|
{
|
|
userFullName = sessionService.User.UserModelDto.ProfileDto.FullName!;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
_logger.Debug($"Navbar myUser false! {DateTime.Now} ");
|
|
myUser = false;
|
|
}
|
|
|
|
var properties = sessionService.User?.HasProperties;
|
|
if (properties == null) return;
|
|
|
|
hasProperty = properties.Count > 0;
|
|
isSysAdmin = sessionService.IsSysAdmin;
|
|
isDevAdmin = sessionService.IsDevAdmin;
|
|
isDriver = sessionService.IsDriver;
|
|
|
|
foreach (var property in properties)
|
|
{
|
|
_logger.Detail($"First property: {property.Value} ");
|
|
}
|
|
}
|
|
|
|
protected override void OnAfterRender(bool firstRender)
|
|
{
|
|
base.OnAfterRender(firstRender);
|
|
|
|
if (firstRender)
|
|
componentUpdateService.GetOrAddComponent<Navbar>().RefreshRequested += RefreshMe;
|
|
}
|
|
}
|
|
}
|