SwitchCardComponent.razor 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. @inject UpdateSwitchUseCase UpdateSwitchUseCase
  2. @inject GetSwitchUseCase GetSwitchUseCase
  3. @inject AddSwitchPortUseCase AddSwitchPortUseCase
  4. @inject UpdateSwitchPortUseCase UpdateSwitchPortUseCase
  5. @inject RemoveSwitchPortUseCase RemoveSwitchPortUseCase
  6. @inject DeleteSwitchUseCase DeleteUseCase
  7. @using RackPeek.Domain.Resources.Hardware.Models
  8. @using RackPeek.Domain.Resources.Hardware.Switches
  9. @using RackPeek.Domain.Resources.Hardware.Switches.Ports
  10. @using RackPeek.Web.Components.Modals
  11. <div class="border border-zinc-800 rounded p-4 bg-zinc-900">
  12. <div class="flex justify-between items-center mb-3">
  13. <div class="text-zinc-100 hover:text-emerald-300">
  14. <NavLink href="@($"/resources/hardware/{Switch.Name}")" class="block">
  15. @Switch.Name
  16. </NavLink>
  17. </div>
  18. <div class="flex gap-3 text-xs">
  19. @if (!_isEditing)
  20. {
  21. <button class="text-zinc-400 hover:text-zinc-200"
  22. @onclick="BeginEdit">
  23. Edit
  24. </button>
  25. <button
  26. class="text-xs text-red-400 hover:text-red-300 transition"
  27. title="Delete server"
  28. @onclick="ConfirmDelete">
  29. Delete
  30. </button>
  31. }
  32. else
  33. {
  34. <button class="text-emerald-400 hover:text-emerald-300"
  35. @onclick="Save">
  36. Save
  37. </button>
  38. <button class="text-zinc-500 hover:text-zinc-300"
  39. @onclick="Cancel">
  40. Cancel
  41. </button>
  42. }
  43. </div>
  44. </div>
  45. <div class="grid grid-cols-1 md:grid-cols-2 gap-3 text-sm">
  46. <!-- Model -->
  47. <div>
  48. <div class="text-zinc-400 mb-1">Model</div>
  49. @if (_isEditing)
  50. {
  51. <input class="input"
  52. @bind="_edit.Model" />
  53. }
  54. else if (!string.IsNullOrWhiteSpace(Switch.Model))
  55. {
  56. <div class="text-zinc-300">@Switch.Model</div>
  57. }
  58. </div>
  59. <!-- Features -->
  60. <div>
  61. <div class="text-zinc-400 mb-1">Features</div>
  62. @if (_isEditing)
  63. {
  64. <div class="flex gap-4">
  65. <label class="flex items-center gap-2 text-zinc-300">
  66. <input type="checkbox" @bind="_edit.Managed" />
  67. Managed
  68. </label>
  69. <label class="flex items-center gap-2 text-zinc-300">
  70. <input type="checkbox" @bind="_edit.Poe" />
  71. PoE
  72. </label>
  73. </div>
  74. }
  75. else
  76. {
  77. <div class="flex gap-2 flex-wrap">
  78. @if (Switch.Managed == true)
  79. {
  80. <span class="text-xs px-2 py-0.5 rounded bg-zinc-800 text-zinc-300">
  81. Managed
  82. </span>
  83. }
  84. @if (Switch.Poe == true)
  85. {
  86. <span class="text-xs px-2 py-0.5 rounded bg-zinc-800 text-zinc-300">
  87. PoE
  88. </span>
  89. }
  90. </div>
  91. }
  92. </div>
  93. <!-- Ports -->
  94. <div class="md:col-span-2">
  95. <div class="flex items-center justify-between mb-1 group">
  96. <div class="text-zinc-400">
  97. Ports
  98. <button class="hover:text-emerald-400 ml-1"
  99. title="Add Port"
  100. @onclick="OpenAddPort">
  101. +
  102. </button>
  103. </div>
  104. </div>
  105. @if (Switch.Ports?.Any() == true)
  106. {
  107. @foreach (var port in Switch.Ports)
  108. {
  109. <div class="flex items-center justify-between text-zinc-300 group hover:bg-zinc-800/40 rounded px-1 py-0.5">
  110. <button class="hover:text-emerald-400"
  111. title="Edit Port"
  112. @onclick="() => OpenEditPort(port)">
  113. @port.Count× @port.Type — @port.Speed Gbps
  114. </button>
  115. </div>
  116. }
  117. }
  118. </div>
  119. </div>
  120. </div>
  121. <PortModal
  122. IsOpen="@_portModalOpen"
  123. IsOpenChanged="v => _portModalOpen = v"
  124. Value="@_editingPort"
  125. OnSubmit="HandlePortSubmit"
  126. OnDelete="HandlePortDelete" />
  127. <ConfirmModal
  128. IsOpen="_confirmDeleteOpen"
  129. IsOpenChanged="v => _confirmDeleteOpen = v"
  130. Title="Delete server"
  131. ConfirmText="Delete"
  132. ConfirmClass="bg-red-600 hover:bg-red-500"
  133. OnConfirm="DeleteServer">
  134. Are you sure you want to delete <strong>@Switch.Name</strong>?
  135. </ConfirmModal>
  136. @code {
  137. [Parameter, EditorRequired]
  138. public Switch Switch { get; set; } = default!;
  139. bool _isEditing;
  140. SwitchEditModel _edit = new();
  141. void BeginEdit()
  142. {
  143. _edit = SwitchEditModel.From(Switch);
  144. _isEditing = true;
  145. }
  146. async Task Save()
  147. {
  148. _isEditing = false;
  149. await UpdateSwitchUseCase.ExecuteAsync(
  150. Switch.Name,
  151. _edit.Model,
  152. _edit.Managed,
  153. _edit.Poe);
  154. Switch = await GetSwitchUseCase.ExecuteAsync(Switch.Name);
  155. }
  156. void Cancel()
  157. {
  158. _isEditing = false;
  159. }
  160. #region Ports
  161. bool _portModalOpen;
  162. int _editingPortIndex;
  163. Port? _editingPort;
  164. void OpenAddPort()
  165. {
  166. _editingPortIndex = -1;
  167. _editingPort = null;
  168. _portModalOpen = true;
  169. }
  170. void OpenEditPort(Port port)
  171. {
  172. Switch.Ports ??= new();
  173. _editingPortIndex = Switch.Ports.IndexOf(port);
  174. _editingPort = port;
  175. _portModalOpen = true;
  176. }
  177. async Task HandlePortSubmit(Port port)
  178. {
  179. if (_editingPortIndex < 0)
  180. {
  181. await AddSwitchPortUseCase.ExecuteAsync(
  182. Switch.Name,
  183. port.Type,
  184. port.Speed,
  185. port.Count);
  186. }
  187. else
  188. {
  189. await UpdateSwitchPortUseCase.ExecuteAsync(
  190. Switch.Name,
  191. _editingPortIndex,
  192. port.Type,
  193. port.Speed,
  194. port.Count);
  195. }
  196. Switch = await GetSwitchUseCase.ExecuteAsync(Switch.Name);
  197. StateHasChanged();
  198. }
  199. async Task HandlePortDelete(Port _)
  200. {
  201. await RemoveSwitchPortUseCase.ExecuteAsync(
  202. Switch.Name,
  203. _editingPortIndex);
  204. Switch = await GetSwitchUseCase.ExecuteAsync(Switch.Name);
  205. StateHasChanged();
  206. }
  207. #endregion
  208. public class SwitchEditModel
  209. {
  210. public string? Model { get; set; }
  211. public bool? Managed { get; set; }
  212. public bool? Poe { get; set; }
  213. public static SwitchEditModel From(Switch Switch)
  214. {
  215. return new SwitchEditModel
  216. {
  217. Model = Switch.Model,
  218. Managed = Switch.Managed,
  219. Poe = Switch.Poe
  220. };
  221. }
  222. }
  223. }
  224. @code {
  225. private bool _confirmDeleteOpen;
  226. [Parameter]
  227. public EventCallback<string> OnDeleted { get; set; }
  228. void ConfirmDelete()
  229. {
  230. _confirmDeleteOpen = true;
  231. }
  232. async Task DeleteServer()
  233. {
  234. _confirmDeleteOpen = false;
  235. await DeleteUseCase.ExecuteAsync(Switch.Name);
  236. if (OnDeleted.HasDelegate)
  237. await OnDeleted.InvokeAsync(Switch.Name);
  238. }
  239. }