Sidebar.razor.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading.Tasks;
  4. using Microsoft.AspNetCore.Components;
  5. using Microsoft.AspNetCore.Components.Authorization;
  6. using Microsoft.AspNetCore.Identity;
  7. using HyperCube.Pages;
  8. using HyperCube.Models;
  9. using Microsoft.AspNetCore.Components.Server.ProtectedBrowserStorage;
  10. using Console = HyperCube.Utils.AdvConsole;
  11. namespace HyperCube.Shared
  12. {
  13. public partial class Sidebar : ComponentBase
  14. {
  15. [Inject]
  16. ProtectedSessionStorage storage { get; set; }
  17. [Inject]
  18. NavigationManager NavigationManager { get; set; }
  19. [Inject]
  20. AuthenticationStateProvider AuthenticationStateProvider { get; set; }
  21. [Inject]
  22. UserManager<IdentityUser> UserManager { get; set; }
  23. ModalProfile modalProfile { get; set; }
  24. ModalNotifications modalNotifications { get; set; }
  25. ModalFiles modalFiles { get; set; }
  26. ModalCompetency modalCompetency { get; set; }
  27. ModalRating modalRating { get; set; }
  28. ModalRules modalRules { get; set; }
  29. ModalAssets modalAssets { get; set; }
  30. ModalError modalError404 { get; set; }
  31. ModalLoading modalLoading { get; set; }
  32. AccountModel _currentAccount;
  33. bool _newNotification = false;
  34. Dictionary<int, Notification> _notifications = new();
  35. protected override async Task OnInitializedAsync()
  36. {
  37. var acc = await GetCurrentAcc();
  38. Console.WriteLine($"sidebar myFlag set1 {acc.Name}");
  39. //AppData.CurrentAccount = acc;
  40. await storage.SetAsync("acc", acc.Email);
  41. var myFlag = await storage.GetAsync<string>("acc");
  42. Console.WriteLine($"sidebar myFlag set {myFlag.Value}");
  43. await storage.SetAsync("completed", true);
  44. _newNotification = await CheckNotifications();
  45. }
  46. void LogoClick()
  47. {
  48. NavigationManager.NavigateTo("");
  49. }
  50. async Task ProfileClick()
  51. {
  52. //AccountModel account = new() { Name = "[SomeUserName]", UUID = "[SomeUserID]" };
  53. AccountModel account = await GetCurrentAcc();
  54. account.LoadRoles();
  55. modalProfile.Open(account);
  56. }
  57. void DesktopClick()
  58. {
  59. NavigationManager.NavigateTo("desktop");
  60. }
  61. void FilesClick()
  62. {
  63. modalFiles.Open();
  64. }
  65. void CompetencyClick()
  66. {
  67. modalCompetency.Open();
  68. }
  69. void RatingClick()
  70. {
  71. //modalRating.Open();
  72. //tmp
  73. modalNotifications.Open(_notifications, _currentAccount);
  74. }
  75. void RulesClick()
  76. {
  77. modalRules.Open();
  78. }
  79. async Task AssetsClick()
  80. {
  81. await modalAssets.Open(_currentAccount);
  82. }
  83. void ExitClick()
  84. {
  85. NavigationManager.NavigateTo("Identity/Account/Logout", true);
  86. }
  87. void ErrorClick()
  88. {
  89. modalError404.Open();
  90. }
  91. async Task<bool> CheckNotifications()
  92. {
  93. bool newNotifications = false;
  94. Notification notification;
  95. _notifications.Clear();
  96. var notifications = await MySQLConnector.Instance().SQLSelectComplex($"SELECT * FROM notifications WHERE recipientid='{_currentAccount.UUID}'");
  97. if (notifications.Count > 0)
  98. {
  99. foreach (var n in notifications)
  100. {
  101. notification = new((NotificationType)Convert.ToInt32(n["type"]),
  102. Convert.ToString(n["header"]),
  103. Convert.ToString(n["body"]),
  104. Convert.ToString(n["recipientid"]),
  105. Convert.ToString(n["senderid"]))
  106. {
  107. ID = Convert.ToInt32(n["id"]),
  108. IsRead = Convert.ToBoolean(n["isread"]),
  109. IsApproved = Convert.ToBoolean(n["isapproved"]),
  110. IsRejected = Convert.ToBoolean(n["isrejected"])
  111. };
  112. Console.WriteLine($"add notification. id: {notification.ID}, header: {notification.Header}, body: {notification.Body}");
  113. _notifications.Add(notification.ID, notification);
  114. if (!notification.IsRead)
  115. newNotifications = true;
  116. }
  117. }
  118. return newNotifications;
  119. }
  120. async Task<AccountModel> GetCurrentAcc()
  121. {
  122. if (_currentAccount == null)
  123. {
  124. var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
  125. var user = authState.User;
  126. if (user.Identity.IsAuthenticated)
  127. {
  128. var currentUser = await UserManager.GetUserAsync(user);
  129. var acc = AccountModel.Find(currentUser.Id);
  130. if (acc != null)
  131. {
  132. _currentAccount = AccountModel.Loaded[currentUser.Id];
  133. }
  134. else
  135. {
  136. Dictionary<string, AccountModel> accounts = await MySQLConnector.Instance().SQLSelectASPUsers();
  137. if (accounts.ContainsKey(currentUser.Id))
  138. _currentAccount = accounts[currentUser.Id];
  139. }
  140. }
  141. }
  142. return _currentAccount;
  143. }
  144. }
  145. }