summary refs log tree commit diff
path: root/extra/admin-api/Utilities/Spacebar.AdminAPI.TestClient/Pages/Users.razor
blob: c0e678678070e814fbbd0bf0f532ffe4b69a9f89 (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
@page "/Users"
@using System.Net.Http.Headers
@using System.Reflection
@using Spacebar.AdminApi.Models
@using Spacebar.AdminAPI.TestClient.Services
@using ArcaneLibs.Blazor.Components
@using ArcaneLibs.Extensions
@inject Config Config
@inject ILocalStorageService LocalStorage

<PageTitle>Users</PageTitle>

<details>
    <summary>Displayed columns</summary>
    @foreach (var column in DisplayedColumns) {
        var value = column.Value;
        <span>
        <InputCheckbox @bind-Value:get="@(value)" @bind-Value:set="@(b => {
                                                                       DisplayedColumns[column.Key] = b;
                                                                       StateHasChanged();
                                                                   })"/>
            @column.Key.Name
    </span>
        <br/>
    }
</details>

<p>Got @UserList.Count users.</p>
<table class="table table-bordered">
    @{
        var columns = DisplayedColumns.Where(kvp => kvp.Value).Select(kvp => kvp.Key).ToList();
    }
    <thead>
        <tr>
            @foreach (var column in columns) {
                <th>@column.Name</th>
            }
            <th>Actions</th>
        </tr>
    </thead>
    <tbody>
        @foreach (var user in UserList.Where(x => !x.Deleted).OrderByDescending(x=>x.MessageCount)) {
            <tr>
                @foreach (var column in columns) {
                    <td>@column.GetValue(user)</td>
                }
                <td>
                    <LinkButton href="@($"/Users/Delete/{user.Id}")" Color="#ff0000">Delete</LinkButton>
                </td>
            </tr>
        }
    </tbody>
</table>

@code {

    private Dictionary<PropertyInfo, bool> DisplayedColumns { get; set; } = typeof(UserModel).GetProperties()
        .ToDictionary(p => p, p => p.Name == "Username" || p.Name == "Id" || p.Name == "MessageCount");

    private List<UserModel> UserList { get; set; } = new();

    protected override async Task OnInitializedAsync() {
        var hc = new StreamingHttpClient();
        hc.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", Config.AccessToken);

        // var request = new HttpRequestMessage(HttpMethod.Get, Config.AdminUrl + "/_spacebar/admin/users/");
        
        var response = hc.GetAsyncEnumerableFromJsonAsync<UserModel>(Config.AdminUrl + "/_spacebar/admin/users/");
        // if (!response.IsSuccessStatusCode) throw new Exception(await response.Content.ReadAsStringAsync());
        // var content = response.Content.ReadFromJsonAsAsyncEnumerable<UserModel>();
        await foreach (var user in response) {
            // Console.WriteLine(user.ToJson(indent: false, ignoreNull: true));
            UserList.Add(user!);
            if(UserList.Count % 1000 == 0)
                StateHasChanged();
        }
        StateHasChanged();
    }

}