summary refs log tree commit diff
path: root/testFrontend/SafeNSound.Demo/Pages/Devices.razor
diff options
context:
space:
mode:
Diffstat (limited to 'testFrontend/SafeNSound.Demo/Pages/Devices.razor')
-rw-r--r--testFrontend/SafeNSound.Demo/Pages/Devices.razor81
1 files changed, 81 insertions, 0 deletions
diff --git a/testFrontend/SafeNSound.Demo/Pages/Devices.razor b/testFrontend/SafeNSound.Demo/Pages/Devices.razor
new file mode 100644

index 0000000..5f90132 --- /dev/null +++ b/testFrontend/SafeNSound.Demo/Pages/Devices.razor
@@ -0,0 +1,81 @@ +@page "/{UserType}/Devices" + +@if (_isInitialized) { + <h3>Devices (@WhoAmI.UserId#@WhoAmI.DeviceId[..5])</h3> + <hr/> + <LinkButton OnClick="@CreateDevice">Create new device</LinkButton> + <LinkButton OnClick="@ReloadDevices">Reload Devices</LinkButton> + <LinkButton OnClick="@DeleteAccount">Delete account</LinkButton> + @foreach (var device in CurrentDevices) { + <div class="device-card"> + <h4>ID: @device.Id + @if (device.Id == WhoAmI.DeviceId) { + <span> (current)</span> + } + </h4> + <span>Name: @device.Name</span><br/> + <span>First seen: @device.CreatedAt.ToLocalTime()</span><br/> + <span>Last seen: @device.LastSeen.ToLocalTime()</span><br/> + <LinkButton OnClick="@(() => DeleteDevice(device))">Log out</LinkButton> + </div> + } +} + +@code { + private bool _isInitialized; + + [Parameter] + public string UserType { get; set; } = string.Empty; + + private SafeNSoundClient Client { get; set; } = null!; + private RegisterDto Auth { get; set; } = null!; + private WhoAmI WhoAmI { get; set; } = null!; + private List<DeviceDto> CurrentDevices { get; set; } = null!; + + protected override async Task OnInitializedAsync() { + if (UserType is not "User" and not "Monitor" and not "Admin") { + NavigationManager.NavigateTo("/"); + return; + } + + Client = UserType switch { + "User" => App.UserClient, + "Monitor" => App.MonitorClient, + "Admin" => App.AdminClient, + _ => throw new InvalidOperationException("Invalid UserType") + }; + + Auth = UserType switch { + "User" => App.UserAuth, + "Monitor" => App.MonitorAuth, + "Admin" => App.AdminAuth, + _ => throw new InvalidOperationException("Invalid UserType") + }; + + WhoAmI = await Client.WhoAmI(); + + await ReloadDevices(); + + _isInitialized = true; + } + + private async Task CreateDevice() { + await Authentication.Login(Auth); + await ReloadDevices(); + } + + private async Task DeleteDevice(DeviceDto device) { + await Client.DeleteDevice(device.Id!); + await ReloadDevices(); + } + + private async Task ReloadDevices() { + CurrentDevices = await Client.GetDevices(); + StateHasChanged(); + } + + private async Task DeleteAccount() { + await Client.DeleteAccount(Auth); + } + +} \ No newline at end of file