Sidebar.razor.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. protected override async Task OnInitializedAsync()
  35. {
  36. var acc = await GetCurrentAcc();
  37. Console.WriteLine($"sidebar myFlag set1 {acc.Name}");
  38. //AppData.CurrentAccount = acc;
  39. await storage.SetAsync("acc", acc.Email);
  40. var myFlag = await storage.GetAsync<string>("acc");
  41. Console.WriteLine($"sidebar myFlag set {myFlag.Value}");
  42. await storage.SetAsync("completed", true);
  43. _newNotification = await CheckNotifications();
  44. }
  45. void LogoClick()
  46. {
  47. NavigationManager.NavigateTo("");
  48. }
  49. async Task ProfileClick()
  50. {
  51. //AccountModel account = new() { Name = "[SomeUserName]", UUID = "[SomeUserID]" };
  52. AccountModel account = await GetCurrentAcc();
  53. account.LoadRoles();
  54. modalProfile.Open(account);
  55. }
  56. void DesktopClick()
  57. {
  58. NavigationManager.NavigateTo("desktop");
  59. }
  60. void FilesClick()
  61. {
  62. modalFiles.Open();
  63. }
  64. void CompetencyClick()
  65. {
  66. modalCompetency.Open();
  67. }
  68. void RatingClick()
  69. {
  70. //modalRating.Open();
  71. //tmp
  72. modalNotifications.Open(_currentAccount.notifications, _currentAccount);
  73. }
  74. void RulesClick()
  75. {
  76. modalRules.Open();
  77. }
  78. async Task AssetsClick()
  79. {
  80. await modalAssets.Open(_currentAccount);
  81. }
  82. void ExitClick()
  83. {
  84. NavigationManager.NavigateTo("Identity/Account/Logout", true);
  85. }
  86. void ErrorClick()
  87. {
  88. modalError404.Open();
  89. }
  90. async Task<bool> CheckNotifications()
  91. {
  92. bool newNotifications = false;
  93. Notification notification;
  94. _currentAccount.notifications.Clear();
  95. var notifications = await MySQLConnector.Instance().SQLSelectComplex($"SELECT * FROM notifications WHERE recipientid='{_currentAccount.UUID}'");
  96. if (notifications.Count > 0)
  97. {
  98. foreach (var n in notifications)
  99. {
  100. notification = new((NotificationType)Convert.ToInt32(n["type"]),
  101. Convert.ToString(n["header"]),
  102. Convert.ToString(n["body"]),
  103. Convert.ToString(n["recipientid"]),
  104. Convert.ToString(n["senderid"]))
  105. {
  106. ID = Convert.ToInt32(n["id"]),
  107. IsRead = Convert.ToBoolean(n["isread"]),
  108. IsApproved = Convert.ToBoolean(n["isapproved"]),
  109. IsRejected = Convert.ToBoolean(n["isrejected"])
  110. };
  111. Console.WriteLine($"add notification. id: {notification.ID}, header: {notification.Header}, body: {notification.Body}");
  112. _currentAccount.notifications.Add(notification.ID, notification);
  113. if (!notification.IsRead)
  114. newNotifications = true;
  115. }
  116. }
  117. return newNotifications;
  118. }
  119. async Task<AccountModel> GetCurrentAcc()
  120. {
  121. if (_currentAccount == null)
  122. {
  123. var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
  124. var user = authState.User;
  125. if (user.Identity.IsAuthenticated)
  126. {
  127. var currentUser = await UserManager.GetUserAsync(user);
  128. var acc = AccountModel.Find(currentUser.Id);
  129. if (acc != null)
  130. {
  131. _currentAccount = AccountModel.Loaded[currentUser.Id];
  132. }
  133. else
  134. {
  135. Dictionary<string, AccountModel> accounts = await MySQLConnector.Instance().SQLSelectASPUsers();
  136. if (accounts.ContainsKey(currentUser.Id))
  137. _currentAccount = accounts[currentUser.Id];
  138. }
  139. }
  140. }
  141. return _currentAccount;
  142. }
  143. }
  144. }