SystemsListPage.razor 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. @page "/systems/list"
  2. @using RackPeek.Domain.Persistence
  3. @using RackPeek.Domain.Resources.SystemResources
  4. @using RackPeek.Domain.Resources.SystemResources.UseCases
  5. @inject NavigationManager Nav
  6. <!-- TODO: Get rid of First -->
  7. <ResourcesListComponent TResource="SystemResource"
  8. Title="@PageTitle"
  9. TestId="systems"
  10. Resources="@Systems"
  11. ShouldGroup="true"
  12. GroupBy="@(s => {
  13. if (s.RunsOn is null) return "Unkown";
  14. return s.RunsOn.FirstOrDefault();
  15. })"
  16. OnCreated="NavigateToNewResource">
  17. <ItemTemplate Context="systemResource">
  18. <div data-testid=@($"systems-list-item-{systemResource.Name.Replace(" ", "-")}")>
  19. <SystemCardComponent System="systemResource"
  20. OnSave="UpdateSystem"
  21. OnDeleted="Reload"/>
  22. </div>
  23. </ItemTemplate>
  24. </ResourcesListComponent>
  25. @code {
  26. [Inject] IResourceCollection Repo { get; set; } = default!;
  27. [Inject] ISystemRepository SystemRepo { get; set; } = default!;
  28. [Inject] UpdateSystemUseCase UpdateSystemUseCase { get; set; } = default!;
  29. [Parameter]
  30. [SupplyParameterFromQuery(Name = "type")]
  31. public string? Type { get; set; }
  32. [Parameter]
  33. [SupplyParameterFromQuery(Name = "os")]
  34. public string? Os { get; set; }
  35. public IReadOnlyList<SystemResource> Systems { get; set; } = new List<SystemResource>();
  36. // Computed title that reflects active filters
  37. private string PageTitle
  38. {
  39. get
  40. {
  41. var type = Normalize(Type);
  42. var os = Normalize(Os);
  43. if (string.IsNullOrEmpty(type) && string.IsNullOrEmpty(os))
  44. return "Systems";
  45. var parts = new List<string>();
  46. if (!string.IsNullOrEmpty(type)) parts.Add(type);
  47. if (!string.IsNullOrEmpty(os)) parts.Add(os);
  48. return $"Systems ({string.Join(" / ", parts)})";
  49. }
  50. }
  51. protected override async Task OnInitializedAsync()
  52. {
  53. await Reload();
  54. await base.OnInitializedAsync();
  55. }
  56. protected override async Task OnParametersSetAsync()
  57. {
  58. // If query params change while staying on the page, keep list + title in sync
  59. await Reload();
  60. await base.OnParametersSetAsync();
  61. }
  62. private async Task Reload(string _ = "")
  63. {
  64. var type = Normalize(Type);
  65. var os = Normalize(Os);
  66. if (string.IsNullOrEmpty(type) && string.IsNullOrEmpty(os))
  67. {
  68. Systems = await Repo.GetAllOfTypeAsync<SystemResource>();
  69. }
  70. else
  71. {
  72. Systems = await SystemRepo.GetFilteredAsync(type, os);
  73. }
  74. }
  75. private static string? Normalize(string? s)
  76. => string.IsNullOrWhiteSpace(s) ? null : s.Trim();
  77. private async Task UpdateSystem(SystemEditModel edit)
  78. {
  79. await UpdateSystemUseCase.ExecuteAsync(
  80. edit.Name,
  81. edit.Type,
  82. edit.Os,
  83. edit.Cores,
  84. edit.Ram,
  85. edit.RunsOn,
  86. edit.Notes
  87. );
  88. await Reload();
  89. }
  90. private Task NavigateToNewResource(string name)
  91. {
  92. Nav.NavigateTo($"resources/systems/{Uri.EscapeDataString(name)}");
  93. return Task.CompletedTask;
  94. }
  95. }