summary refs log tree commit diff
path: root/testFrontend/SafeNSound.Demo/Pages/Devices.razor
blob: 695c1386fd3db0165eb7d6e0babf308749754758 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
@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="@Logout">Log out</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);
    }
    
    private async Task Logout() {
        await Client.LogOut();
    }

}