summaryrefslogtreecommitdiff
path: root/Pages/Component/MiniPrincipalSelect.razor
blob: 89409bd25b2a7ec933e8d33a5a2e3cc31e69154f (plain)
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
@inject ISecurityService securityService
@inject IJSRuntime jsRuntime

<div @ref=div>
    @if(newName is not null) {
        <label>@Label</label>
        <input type="text" autocomplete="off" @bind=newName/>
        <button class="secondary" @onclick=Cancel>Cancel</button>
        <button @onclick=Submit>OK</button>
    } else {
        <label>@(Label):</label>
        <a href="javascript:;" @onclick=Edit>
            @if(SecurityIdentifier is null || SecurityIdentifier == WellKnownSid.NullSid) {
                <i>Please select a user or group</i>
            } else {
                @name
            }
        </a>
    }
</div>

@code {
    [Parameter]
    public string Label { get; set; }

    [Parameter]
    public EventCallback<SecurityIdentifier> OnChange { get; set; }

    private bool    edit = false;
    private string? name;
    private string? newName;

    private ElementReference div;

    private HyperBooru.SecurityIdentifier? securityIdentifier;

    public SecurityIdentifier? SecurityIdentifier {
        get => securityIdentifier;
        set {
            securityIdentifier = value;
            name = value is null ? null : securityService.TranslateName(value);
        }
    }

    private void Edit() {
        newName = SecurityIdentifier == WellKnownSid.NullSid ? "" : name;
        StateHasChanged();
    }

    private async void Cancel() {
        newName = null;
        await jsRuntime.InvokeVoidAsync("removeClass", div, "bad-principal");
        StateHasChanged();
    }

    private async void Submit() {
        var sid = securityService.TranslateName(newName!);
        if(sid is null) {
            await jsRuntime.InvokeVoidAsync("cycleClass", div, "bad-principal");
            return;
        }

        await jsRuntime.InvokeVoidAsync("removeClass", div, "bad-principal");
        SecurityIdentifier = sid;
        await OnChange.InvokeAsync(sid);
        newName = null;
        StateHasChanged();
    }
}