176 lines
5.1 KiB
C#
176 lines
5.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;
|
|
|
|
|
|
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 = false;
|
|
private bool enableApi = true;
|
|
private bool enableChat = true;
|
|
|
|
private bool expandNavMenu = true;
|
|
private bool myUser = false;
|
|
private bool hasProperty = false;
|
|
|
|
private ILogger _logger;
|
|
|
|
//componentUpdateService.RefreshRequested += RefreshMe;
|
|
|
|
public Navbar()
|
|
{
|
|
|
|
}
|
|
|
|
private async Task ExpandNavMenu()
|
|
{
|
|
await JsRuntime.InvokeVoidAsync("toggleBootstrapNavMenu");
|
|
}
|
|
|
|
private void RefreshMe()
|
|
{
|
|
_logger.Debug($"Navbar refresh called! {DateTime.Now} ");
|
|
|
|
//OnInitialized();
|
|
InitUser();
|
|
StateHasChanged();
|
|
}
|
|
|
|
private void ToggleNavMenu()
|
|
{
|
|
expandNavMenu = !expandNavMenu;
|
|
|
|
JsRuntime.InvokeVoidAsync("toggleBootstrapNavMenu", expandNavMenu);
|
|
}
|
|
|
|
private async Task SignOut()
|
|
{
|
|
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.User = null;
|
|
sessionService.IsAuthenticated = false;
|
|
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} ");
|
|
|
|
InitUser();
|
|
}
|
|
|
|
private void OnLocationChanged(object sender, LocationChangedEventArgs e)
|
|
{
|
|
// Collapse the navbar on navigation
|
|
|
|
expandNavMenu = false;
|
|
JsRuntime.InvokeVoidAsync("toggleBootstrapNavMenu", false);
|
|
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
// Unsubscribe from the event when the component is disposed
|
|
navigationManager.LocationChanged -= OnLocationChanged;
|
|
}
|
|
|
|
private void InitUser()
|
|
{
|
|
if (sessionService.User != null)
|
|
{
|
|
myUser = true;
|
|
}
|
|
else
|
|
{
|
|
_logger.Debug($"Navbar myUser false! {DateTime.Now} ");
|
|
myUser = false;
|
|
}
|
|
|
|
var properties = sessionService.User?.HasProperties;
|
|
|
|
if (properties == null)
|
|
return;
|
|
|
|
hasProperty = properties.Count > 0;
|
|
|
|
foreach (var property in properties)
|
|
{
|
|
_logger.Detail($"First property: {property.Value} ");
|
|
}
|
|
}
|
|
|
|
protected override void OnAfterRender(bool firstRender)
|
|
{
|
|
base.OnAfterRender(firstRender);
|
|
|
|
if (firstRender)
|
|
componentUpdateService.RefreshRequested += RefreshMe;
|
|
}
|
|
}
|
|
}
|