LaptopCardComponent.razor 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. @using RackPeek.Domain.Resources.Hardware.Desktops
  2. @using RackPeek.Domain.Resources.Hardware.Models
  3. @using RackPeek.Domain.Resources.Hardware.Laptops
  4. @using RackPeek.Domain.Resources.Hardware.Laptops.Cpus
  5. @using RackPeek.Domain.Resources.Hardware.Laptops.Drives
  6. @using RackPeek.Domain.Resources.Hardware.Laptops.Gpus
  7. @using RackPeek.Web.Components.Modals
  8. @inject GetLaptopUseCase GetLaptopUseCase
  9. @inject UpdateLaptopUseCase UpdateLaptopUseCase
  10. @inject DeleteLaptopUseCase DeleteLaptopUseCase
  11. @inject AddLaptopCpuUseCase AddCpuUseCase
  12. @inject UpdateLaptopCpuUseCase UpdateCpuUseCase
  13. @inject RemoveLaptopCpuUseCase RemoveCpuUseCase
  14. @inject AddLaptopDriveUseCase AddDriveUseCase
  15. @inject UpdateLaptopDriveUseCase UpdateDriveUseCase
  16. @inject RemoveLaptopDriveUseCase RemoveDriveUseCase
  17. @inject AddLaptopGpuUseCase AddGpuUseCase
  18. @inject UpdateLaptopGpuUseCase UpdateGpuUseCase
  19. @inject RemoveLaptopGpuUseCase RemoveGpuUseCase
  20. <div class="border border-zinc-800 rounded p-4 bg-zinc-900">
  21. <div class="flex justify-between items-center mb-3">
  22. <div class="text-zinc-100 hover:text-emerald-300">
  23. <NavLink href="@($"/resources/hardware/{Laptop.Name}")" class="block">
  24. @Laptop.Name
  25. </NavLink>
  26. </div>
  27. <div class="flex justify-between items-center mb-3">
  28. @if (!string.IsNullOrWhiteSpace(Laptop.Model))
  29. {
  30. <span class="text-xs text-zinc-400">
  31. @Laptop.Model
  32. </span>
  33. }
  34. <div class="flex items-center gap-2">
  35. <button
  36. class="text-xs text-red-400 hover:text-red-300 transition"
  37. title="Delete server"
  38. @onclick="ConfirmDelete">
  39. Delete
  40. </button>
  41. </div>
  42. </div>
  43. </div>
  44. <div class="grid grid-cols-1 md:grid-cols-2 gap-3 text-sm">
  45. <!-- CPU -->
  46. <div>
  47. <div class="flex items-center justify-between mb-1 group">
  48. <div class="text-zinc-400">
  49. CPU
  50. <button
  51. class="hover:text-emerald-400 transition"
  52. title="Add CPU"
  53. @onclick="OpenAddCpu">
  54. +
  55. </button>
  56. </div>
  57. </div>
  58. @if (Laptop.Cpus?.Any() == true)
  59. {
  60. @foreach (var cpu in Laptop.Cpus)
  61. {
  62. <div class="flex items-center justify-between text-zinc-300 group hover:bg-zinc-800/40 rounded px-1 py-0.5">
  63. <button
  64. class="hover:text-emerald-400"
  65. title="Edit CPU"
  66. @onclick="() => OpenEditCpu(cpu)">
  67. @cpu.Model — @cpu.Cores cores / @cpu.Threads threads
  68. </button>
  69. </div>
  70. }
  71. }
  72. </div>
  73. <!-- RAM -->
  74. <div>
  75. <div class="text-zinc-400 mb-1">
  76. RAM
  77. <button
  78. class="hover:text-emerald-400 transition"
  79. title="Edit RAM"
  80. @onclick="EditRam">
  81. +
  82. </button>
  83. </div>
  84. @if (Laptop.Ram is not null)
  85. {
  86. <div class="flex items-center justify-between text-zinc-300 group hover:bg-zinc-800/40 rounded px-1 py-0.5">
  87. <button
  88. class="hover:text-emerald-400"
  89. @onclick="EditRam">
  90. @($"{Laptop.Ram.Size} GB {Laptop.Ram.Mts} MT/s")
  91. </button>
  92. </div>
  93. }
  94. </div>
  95. <!-- Drives -->
  96. <div>
  97. <div class="flex items-center justify-between mb-1 group">
  98. <div class="text-zinc-400">
  99. Drives
  100. <button
  101. class="hover:text-emerald-400 transition"
  102. title="Add Drive"
  103. @onclick="OpenAddDrive">
  104. +
  105. </button>
  106. </div>
  107. </div>
  108. @if (Laptop.Drives?.Any() == true)
  109. {
  110. @foreach (var drive in Laptop.Drives)
  111. {
  112. <div class="flex items-center justify-between text-zinc-300 group hover:bg-zinc-800/40 rounded px-1 py-0.5">
  113. <button
  114. class="hover:text-emerald-400"
  115. @onclick="() => OpenEditDrive(drive)">
  116. @drive.Type — @drive.Size GB
  117. </button>
  118. </div>
  119. }
  120. }
  121. </div>
  122. <!-- GPUs -->
  123. <div>
  124. <div class="flex items-center justify-between mb-1 group">
  125. <div class="text-zinc-400">
  126. GPUs
  127. <button
  128. class="hover:text-emerald-400 transition"
  129. title="Add GPU"
  130. @onclick="OpenAddGpu">
  131. +
  132. </button>
  133. </div>
  134. </div>
  135. @if (Laptop.Gpus?.Any() == true)
  136. {
  137. @foreach (var gpu in Laptop.Gpus)
  138. {
  139. <div class="flex items-center justify-between text-zinc-300 group hover:bg-zinc-800/40 rounded px-1 py-0.5">
  140. <button
  141. class="hover:text-emerald-400"
  142. @onclick="() => OpenEditGpu(gpu)">
  143. @gpu.Model — @gpu.Vram GB VRAM
  144. </button>
  145. </div>
  146. }
  147. }
  148. </div>
  149. </div>
  150. </div>
  151. <CpuModal
  152. IsOpen="@_cpuModalOpen"
  153. IsOpenChanged="v => _cpuModalOpen = v"
  154. Value="@_editingCpu"
  155. OnSubmit="HandleCpuSubmit"
  156. OnDelete="HandleCpuDelete"/>
  157. <RamModal
  158. IsOpen="@_isRamModalOpen"
  159. IsOpenChanged="v => _isRamModalOpen = v"
  160. Value="@Laptop.Ram"
  161. OnSubmit="HandleRamSubmit"/>
  162. <DriveModal
  163. IsOpen="@_driveModalOpen"
  164. IsOpenChanged="v => _driveModalOpen = v"
  165. Value="@_editingDrive"
  166. OnSubmit="HandleDriveSubmit"
  167. OnDelete="HandleDriveDelete"/>
  168. <GpuModal
  169. IsOpen="@_gpuModalOpen"
  170. IsOpenChanged="v => _gpuModalOpen = v"
  171. Value="@_editingGpu"
  172. OnSubmit="HandleGpuSubmit"
  173. OnDelete="HandleGpuDelete" />
  174. <ConfirmModal
  175. IsOpen="_confirmDeleteOpen"
  176. IsOpenChanged="v => _confirmDeleteOpen = v"
  177. Title="Delete server"
  178. ConfirmText="Delete"
  179. ConfirmClass="bg-red-600 hover:bg-red-500"
  180. OnConfirm="DeleteServer">
  181. Are you sure you want to delete <strong>@Laptop.Name</strong>?
  182. <br />
  183. This will detach all dependent systems.
  184. </ConfirmModal>
  185. @code {
  186. [Parameter] [EditorRequired]
  187. public Laptop Laptop { get; set; } = default!;
  188. #region RAM
  189. private bool _isRamModalOpen;
  190. private void EditRam()
  191. {
  192. _isRamModalOpen = true;
  193. }
  194. private async Task HandleRamSubmit(Ram value)
  195. {
  196. _isRamModalOpen = false;
  197. await UpdateLaptopUseCase.ExecuteAsync(Laptop.Name, Laptop.Model, value.Size, value.Mts);
  198. Laptop = await GetLaptopUseCase.ExecuteAsync(Laptop.Name);
  199. }
  200. #endregion
  201. #region CPU
  202. bool _cpuModalOpen;
  203. int _editingCpuIndex;
  204. Cpu? _editingCpu;
  205. void OpenAddCpu()
  206. {
  207. _editingCpuIndex = -1;
  208. _editingCpu = null;
  209. _cpuModalOpen = true;
  210. }
  211. void OpenEditCpu(Cpu cpu)
  212. {
  213. _editingCpu = cpu;
  214. Laptop.Cpus ??= new();
  215. _editingCpuIndex = Laptop.Cpus.IndexOf(cpu);;
  216. _cpuModalOpen = true;
  217. }
  218. async Task HandleCpuSubmit(Cpu cpu)
  219. {
  220. Laptop.Cpus ??= new();
  221. if (_editingCpuIndex < 0)
  222. {
  223. await AddCpuUseCase.ExecuteAsync(Laptop.Name, cpu.Model, cpu.Cores, cpu.Threads);
  224. }
  225. else
  226. {
  227. await UpdateCpuUseCase.ExecuteAsync(Laptop.Name, _editingCpuIndex, cpu.Model, cpu.Cores, cpu.Threads);
  228. }
  229. Laptop = await GetLaptopUseCase.ExecuteAsync(Laptop.Name);
  230. }
  231. async Task HandleCpuDelete(Cpu cpu)
  232. {
  233. await RemoveCpuUseCase.ExecuteAsync(Laptop.Name, _editingCpuIndex);
  234. Laptop = await GetLaptopUseCase.ExecuteAsync(Laptop.Name);
  235. }
  236. #endregion
  237. #region Drives
  238. bool _driveModalOpen;
  239. int _editingDriveIndex;
  240. Drive? _editingDrive;
  241. void OpenAddDrive()
  242. {
  243. _editingDriveIndex = -1;
  244. _editingDrive = null;
  245. _driveModalOpen = true;
  246. }
  247. void OpenEditDrive(Drive drive)
  248. {
  249. _editingDrive = drive;
  250. Laptop.Drives ??= new();
  251. _editingDriveIndex = Laptop.Drives.IndexOf(drive);;
  252. _driveModalOpen = true;
  253. }
  254. async Task HandleDriveSubmit(Drive drive)
  255. {
  256. Laptop.Drives ??= new();
  257. if (_editingDriveIndex < 0)
  258. {
  259. await AddDriveUseCase.ExecuteAsync(Laptop.Name, drive.Type, drive.Size);
  260. }
  261. else
  262. {
  263. await UpdateDriveUseCase.ExecuteAsync(Laptop.Name, _editingDriveIndex, drive.Type, drive.Size);
  264. }
  265. Laptop = await GetLaptopUseCase.ExecuteAsync(Laptop.Name);
  266. StateHasChanged();
  267. }
  268. async Task HandleDriveDelete(Drive drive)
  269. {
  270. await RemoveDriveUseCase.ExecuteAsync(Laptop.Name, _editingDriveIndex);
  271. Laptop = await GetLaptopUseCase.ExecuteAsync(Laptop.Name);
  272. StateHasChanged();
  273. }
  274. #endregion
  275. #region GPUs
  276. bool _gpuModalOpen;
  277. int _editingGpuIndex;
  278. Gpu? _editingGpu;
  279. void OpenAddGpu()
  280. {
  281. _editingGpuIndex = -1;
  282. _editingGpu = null;
  283. _gpuModalOpen = true;
  284. }
  285. void OpenEditGpu(Gpu gpu)
  286. {
  287. Laptop.Gpus ??= new();
  288. _editingGpuIndex = Laptop.Gpus.IndexOf(gpu);
  289. _editingGpu = gpu;
  290. _gpuModalOpen = true;
  291. }
  292. async Task HandleGpuSubmit(Gpu gpu)
  293. {
  294. Laptop.Gpus ??= new();
  295. if (_editingGpuIndex < 0)
  296. {
  297. await AddGpuUseCase.ExecuteAsync(
  298. Laptop.Name,
  299. gpu.Model,
  300. gpu.Vram);
  301. }
  302. else
  303. {
  304. await UpdateGpuUseCase.ExecuteAsync(
  305. Laptop.Name,
  306. _editingGpuIndex,
  307. gpu.Model,
  308. gpu.Vram);
  309. }
  310. Laptop = await GetLaptopUseCase.ExecuteAsync(Laptop.Name);
  311. }
  312. async Task HandleGpuDelete(Gpu gpu)
  313. {
  314. await RemoveGpuUseCase.ExecuteAsync(Laptop.Name, _editingGpuIndex);
  315. Laptop = await GetLaptopUseCase.ExecuteAsync(Laptop.Name);
  316. }
  317. #endregion
  318. }
  319. @code {
  320. private bool _confirmDeleteOpen;
  321. [Parameter]
  322. public EventCallback<string> OnDeleted { get; set; }
  323. void ConfirmDelete()
  324. {
  325. _confirmDeleteOpen = true;
  326. }
  327. async Task DeleteServer()
  328. {
  329. _confirmDeleteOpen = false;
  330. await DeleteLaptopUseCase.ExecuteAsync(Laptop.Name);
  331. if (OnDeleted.HasDelegate)
  332. await OnDeleted.InvokeAsync(Laptop.Name);
  333. }
  334. }