SystemsListPage.razor 3.2 KB

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