123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- using System;
- using System.Collections.Generic;
- using System.Threading.Tasks;
- using Microsoft.AspNetCore.Components;
- using Microsoft.AspNetCore.Components.Authorization;
- using Microsoft.AspNetCore.Identity;
- using HyperCube.Pages;
- using HyperCube.Models;
- using Microsoft.AspNetCore.Components.Server.ProtectedBrowserStorage;
- using Console = HyperCube.Utils.AdvConsole;
- namespace HyperCube.Shared
- {
- public partial class Sidebar : ComponentBase
- {
- [Inject]
- ProtectedSessionStorage storage { get; set; }
- [Inject]
- NavigationManager NavigationManager { get; set; }
- [Inject]
- AuthenticationStateProvider AuthenticationStateProvider { get; set; }
- [Inject]
- UserManager<IdentityUser> UserManager { get; set; }
- ModalProfile modalProfile { get; set; }
- ModalNotifications modalNotifications { get; set; }
- ModalFiles modalFiles { get; set; }
- ModalCompetency modalCompetency { get; set; }
- ModalRating modalRating { get; set; }
- ModalRules modalRules { get; set; }
- ModalAssets modalAssets { get; set; }
- ModalError modalError404 { get; set; }
- ModalLoading modalLoading { get; set; }
- AccountModel _currentAccount;
- bool _newNotification = false;
- protected override async Task OnInitializedAsync()
- {
- var acc = await GetCurrentAcc();
- Console.WriteLine($"sidebar myFlag set1 {acc.Name}");
- //AppData.CurrentAccount = acc;
- await storage.SetAsync("acc", acc.Email);
- var myFlag = await storage.GetAsync<string>("acc");
- Console.WriteLine($"sidebar myFlag set {myFlag.Value}");
- await storage.SetAsync("completed", true);
- _newNotification = await CheckNotifications();
- }
- void LogoClick()
- {
- NavigationManager.NavigateTo("");
- }
- async Task ProfileClick()
- {
- //AccountModel account = new() { Name = "[SomeUserName]", UUID = "[SomeUserID]" };
- AccountModel account = await GetCurrentAcc();
- account.LoadRoles();
- modalProfile.Open(account);
- }
- void DesktopClick()
- {
- NavigationManager.NavigateTo("desktop");
- }
- void FilesClick()
- {
- modalFiles.Open();
- }
- void CompetencyClick()
- {
- modalCompetency.Open();
- }
- void RatingClick()
- {
- //modalRating.Open();
- //tmp
- modalNotifications.Open(_currentAccount.notifications, _currentAccount);
- }
- void RulesClick()
- {
- modalRules.Open();
- }
- async Task AssetsClick()
- {
- await modalAssets.Open(_currentAccount);
- }
- void ExitClick()
- {
- NavigationManager.NavigateTo("Identity/Account/Logout", true);
- }
- void ErrorClick()
- {
- modalError404.Open();
- }
- async Task<bool> CheckNotifications()
- {
- bool newNotifications = false;
- Notification notification;
- _currentAccount.notifications.Clear();
- var notifications = await MySQLConnector.Instance().SQLSelectComplex($"SELECT * FROM notifications WHERE recipientid='{_currentAccount.UUID}'");
- if (notifications.Count > 0)
- {
- foreach (var n in notifications)
- {
- notification = new((NotificationType)Convert.ToInt32(n["type"]),
- Convert.ToString(n["header"]),
- Convert.ToString(n["body"]),
- Convert.ToString(n["recipientid"]),
- Convert.ToString(n["senderid"]))
- {
- ID = Convert.ToInt32(n["id"]),
- IsRead = Convert.ToBoolean(n["isread"]),
- IsApproved = Convert.ToBoolean(n["isapproved"]),
- IsRejected = Convert.ToBoolean(n["isrejected"])
- };
- Console.WriteLine($"add notification. id: {notification.ID}, header: {notification.Header}, body: {notification.Body}");
- _currentAccount.notifications.Add(notification.ID, notification);
- if (!notification.IsRead)
- newNotifications = true;
- }
- }
- return newNotifications;
- }
- async Task<AccountModel> GetCurrentAcc()
- {
- if (_currentAccount == null)
- {
- var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
- var user = authState.User;
- if (user.Identity.IsAuthenticated)
- {
- var currentUser = await UserManager.GetUserAsync(user);
- var acc = AccountModel.Find(currentUser.Id);
- if (acc != null)
- {
- _currentAccount = AccountModel.Loaded[currentUser.Id];
- }
- else
- {
- Dictionary<string, AccountModel> accounts = await MySQLConnector.Instance().SQLSelectASPUsers();
- if (accounts.ContainsKey(currentUser.Id))
- _currentAccount = accounts[currentUser.Id];
- }
- }
- }
- return _currentAccount;
- }
- }
- }
|