summaryrefslogtreecommitdiff
path: root/Pages/Component/TagEditDialog.razor
blob: afa312e2ae1dc5b124c2109c89664ff3d8ae53e9 (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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
@inject IDbContextFactory<HBContext> dbFactory;
@inject ITagService tagService
@implements IDialog

<Dialog Title=@Title @ref=dialog>
    <label>
        Name
        @if(nameExists) {
            <p class="error">Tag with that name already exists!</p>
        }
    </label>
    <input type="text" @bind=tagName required/>
    <label>Namespace</label>
    <input type="text" @bind=tagNamespace/>
    <label>
        Alias
        @if(aliasExists) {
            <p class="error">Tag with that alias already exists!</p>
        }
    </label>
    <input type="text" @bind=tagAlias/>
    <ButtonContainer>
        <button @onclick=Hide class="secondary">Cancel</button>
        <button @onclick=Submit>@(TagDefinition is null ? "Create" : "Apply")</button>
    </ButtonContainer>
</Dialog>

@code {
    [Parameter]
    public TagDefinition? TagDefinition { get; set; }

    [Parameter]
    public EventHandler OnTagUpdate { get; set; }

    public bool Visible {
        get => visible;
        set {
            if(value)
                Load();
            visible = dialog.Visible = value;
        }
    }

    private string Title =>
        TagDefinition is null ? "Create a new tag definition" : "Edit tag definition";

    private Dialog dialog;

    private string? tagName;
    private string? tagNamespace;
    private string? tagAlias;

    private bool nameExists  = false;
    private bool aliasExists = false;

    private bool visible = false;

    public void Show() => Visible = true;
    public void Hide() => Visible = false;

    public void Show(TagDefinition? toEdit) {
        TagDefinition = toEdit;
        Visible       = true;
    }

    public void Show(string? @namespace) {
        TagDefinition = null;
        Visible       = true;
        tagNamespace  = @namespace;
    }

    private void Load() {
        tagName      = TagDefinition?.Name;
        tagNamespace = TagDefinition?.Namespace;
        tagAlias     = TagDefinition?.Alias;
        nameExists   = false;
        aliasExists  = false;
    }

    private void Submit() {
        try {
            if(TagDefinition is null) {
                tagService.CreateTagDefinition(tagName, tagNamespace, tagAlias);
            } else {
                tagService.UpdateTagDefinition(TagDefinition, tagName, tagNamespace, tagAlias);
            }
        } catch(ApiModels.TagDuplicateException e) {
            nameExists  = e.NameExists;
            aliasExists = e.AliasExists;
            return;
        }

        OnTagUpdate.Invoke(this, new EventArgs());
        Hide();
    }
}