Sidebar.razor.cs 5.5 KB

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