|
1 | 1 | @page "/tags"
|
| 2 | +@using Modix.Data.Models.Core; |
| 3 | +@using Modix.Data.Models.Tags; |
| 4 | +@using Modix.Services.Tags; |
| 5 | +@using Modix.Web.Services; |
| 6 | +@using MudBlazor |
| 7 | +@using System.Globalization; |
| 8 | +@using Modix.Data.Models.Core; |
2 | 9 |
|
3 | 10 | <CascadingAuthenticationState>
|
4 | 11 | <AuthorizeView>
|
5 | 12 | <Authorized>
|
6 | 13 |
|
7 | 14 | <PageTitle>Tags</PageTitle>
|
8 |
| - <h1>Tags</h1> |
9 |
| - <p role="status">Current count: @currentCount</p> |
10 |
| - <button class="btn btn-primary" @onclick="IncrementCount">Click me</button> |
| 15 | + <MudContainer> |
| 16 | + <MudText Typo="Typo.h3">Tags</MudText> |
| 17 | + @if (Data is not null) |
| 18 | + { |
| 19 | + <MudDialog @bind-IsVisible="_createDialogVisible" Options="new DialogOptions { FullWidth= true }"> |
| 20 | + <TitleContent> |
| 21 | + <MudText Typo="Typo.h5">Create Tag</MudText> |
| 22 | + </TitleContent> |
| 23 | + <DialogContent> |
| 24 | + <MudTextField @bind-Value="_tagNameValue" Label="Name"></MudTextField> |
| 25 | + <MudTextField @bind-Value="_tagContentValue" Label="Content" Lines="5" Immediate="true"></MudTextField> |
| 26 | + <MudText Typo="Typo.subtitle1">Preview</MudText> |
| 27 | + <MudMarkdown Value="@_tagContentValue" /> |
| 28 | + </DialogContent> |
| 29 | + <DialogActions> |
| 30 | + <div style="justify-content: left; flex-grow: 1;"> |
| 31 | + <MudButton Disabled="@(_tagNameValue is null || _tagContentValue is null)" Color="Color.Success" OnClick="SaveTag">Save</MudButton> |
| 32 | + </div> |
| 33 | + <MudButton Color="Color.Error" OnClick="ToggleDialog">Cancel</MudButton> |
| 34 | + </DialogActions> |
| 35 | + </MudDialog> |
| 36 | + |
| 37 | + <MudTable Items="Data" SortLabel="Sort By" Bordered="true" Filter="FilterFunction"> |
| 38 | + <ToolBarContent> |
| 39 | + <AuthorizeView Context="e" Policy="@nameof(AuthorizationClaim.CreateTag)"> |
| 40 | + <MudButton OnClick="ToggleDialog" StartIcon="@Icons.Material.Filled.Create" Color="Color.Primary">Create</MudButton> |
| 41 | + </AuthorizeView> |
| 42 | + <MudButton OnClick="FetchData" StartIcon="@Icons.Material.Filled.Refresh" Color="Color.Primary">Refresh</MudButton> |
| 43 | + <MudSpacer/> |
| 44 | + <MudTextField DebounceInterval="1000" @bind-Value="query" Placeholder="Filter" Adornment="Adornment.Start" AdornmentIcon="@Icons.Material.Filled.Search" IconSize="Size.Medium" Class="mt-0"></MudTextField> |
| 45 | + </ToolBarContent> |
| 46 | + <HeaderContent> |
| 47 | + <MudTh Style="text-align:center"><MudTableSortLabel T="TagData" SortBy="x => x.Name">Name</MudTableSortLabel></MudTh> |
| 48 | + <MudTh Style="text-align:center"><MudTableSortLabel T="TagData" SortBy="x => x.Created">Last Modified</MudTableSortLabel></MudTh> |
| 49 | + <MudTh Style="text-align:center"><MudTableSortLabel T="TagData" SortBy="x => x.OwnerName">Owner</MudTableSortLabel></MudTh> |
| 50 | + <MudTh Style="text-align:center"><MudTableSortLabel T="TagData" SortBy="x => x.Content">Content</MudTableSortLabel></MudTh> |
| 51 | + <MudTh Style="text-align:center"><MudTableSortLabel T="TagData" SortBy="x => x.Uses">Uses</MudTableSortLabel></MudTh> |
| 52 | + </HeaderContent> |
| 53 | + <RowTemplate Context="tag"> |
| 54 | + <MudTd DataLabel="Name">@tag.Name</MudTd> |
| 55 | + <MudTd style="white-space:nowrap" DataLabel="Last Modified">@tag.Created.ToString("dd/MM/yy, h:MM:ss tt")</MudTd> |
| 56 | + <MudTd DataLabel="Owner">@tag.OwnerName</MudTd> |
| 57 | + <MudTd DataLabel="Content"> |
| 58 | + <MudMarkdown Value="@tag.Content"/> |
| 59 | + </MudTd> |
| 60 | + <MudTd DataLabel="Uses">@tag.Uses</MudTd> |
| 61 | + </RowTemplate> |
| 62 | + <PagerContent> |
| 63 | + <MudTablePager PageSizeOptions="new [] { 10, 20, 30, 40, 50, int.MaxValue }" ></MudTablePager> |
| 64 | + </PagerContent> |
| 65 | + </MudTable> |
| 66 | + } |
| 67 | + </MudContainer> |
11 | 68 |
|
12 | 69 | </Authorized>
|
13 | 70 | </AuthorizeView>
|
14 | 71 | </CascadingAuthenticationState>
|
15 | 72 |
|
16 | 73 | @code {
|
17 |
| - private int currentCount = 0; |
18 | 74 |
|
19 |
| - private void IncrementCount() |
| 75 | + public record TagData( |
| 76 | + string Name, |
| 77 | + DateTimeOffset Created, |
| 78 | + bool IsOwnedByRole, |
| 79 | + GuildUserBrief? OwnerUser, |
| 80 | + GuildRoleBrief? OwnerRole, |
| 81 | + string? OwnerName, |
| 82 | + string Content, |
| 83 | + uint Uses, |
| 84 | + bool CanMaintain, |
| 85 | + TagSummary TagSummary); |
| 86 | + |
| 87 | + |
| 88 | + [Inject] |
| 89 | + ITagService TagService { get; set; } = null!; |
| 90 | + |
| 91 | + [Inject] |
| 92 | + DiscordUserService DiscordUserService { get; set; } = null!; |
| 93 | + |
| 94 | + [Inject] |
| 95 | + IDialogService DialogService { get; set; } = null!; |
| 96 | + |
| 97 | + TagData[]? Data { get; set; } |
| 98 | + string? query; |
| 99 | + string? _tagNameValue; |
| 100 | + string? _tagContentValue; |
| 101 | + bool _createDialogVisible; |
| 102 | + |
| 103 | + protected override async Task OnInitializedAsync() => await FetchData(); |
| 104 | + |
| 105 | + private async Task FetchData() |
| 106 | + { |
| 107 | + var currentGuild = DiscordUserService.GetUserGuild(); |
| 108 | + |
| 109 | + var summaries = await TagService.GetSummariesAsync(new TagSearchCriteria |
| 110 | + { |
| 111 | + GuildId = currentGuild.Id, |
| 112 | + }); |
| 113 | + |
| 114 | + Data = summaries |
| 115 | + .Select(x => CreateTagData(x)) |
| 116 | + .ToArray(); |
| 117 | + |
| 118 | + // foreach (var tag in data) |
| 119 | + // { |
| 120 | + // // TODO Revisit this functionality |
| 121 | + // tag.CanMaintain = false; |
| 122 | + // } |
| 123 | + } |
| 124 | + |
| 125 | + private bool FilterFunction(TagData tag) |
| 126 | + { |
| 127 | + if (string.IsNullOrWhiteSpace(query)) |
| 128 | + return true; |
| 129 | + |
| 130 | + if (tag.OwnerUser is not null && (tag.OwnerUser.Username.Contains(query) || tag.OwnerUser.Id.ToString() == query)) |
| 131 | + return true; |
| 132 | + |
| 133 | + if (tag.OwnerRole?.Name.Contains(query) ?? false) |
| 134 | + return true; |
| 135 | + |
| 136 | + if (tag.Name.Contains(query)) |
| 137 | + return true; |
| 138 | + |
| 139 | + if (tag.Content.Contains(query)) |
| 140 | + return true; |
| 141 | + |
| 142 | + return false; |
| 143 | + } |
| 144 | + |
| 145 | + private async Task SaveTag() |
| 146 | + { |
| 147 | + var currentGuild = DiscordUserService.GetUserGuild(); |
| 148 | + var currentUser = await DiscordUserService.GetCurrentUserAsync(); |
| 149 | + await TagService.CreateTagAsync(currentGuild.Id, currentUser.Id, _tagNameValue, _tagContentValue); |
| 150 | + var createdTag = await TagService.GetTagAsync(currentGuild.Id, _tagNameValue); |
| 151 | + Data = Data!.Append(CreateTagData(createdTag)).ToArray(); |
| 152 | + |
| 153 | + _createDialogVisible = false; |
| 154 | + } |
| 155 | + |
| 156 | + private TagData CreateTagData(TagSummary summary) |
| 157 | + { |
| 158 | + return new TagData( |
| 159 | + summary.Name, |
| 160 | + summary.CreateAction.Created, |
| 161 | + summary.OwnerRole is not null, |
| 162 | + summary.OwnerUser, |
| 163 | + summary.OwnerRole, |
| 164 | + summary.OwnerRole?.Name ?? summary.OwnerUser?.Username, |
| 165 | + summary.Content, |
| 166 | + summary.Uses, |
| 167 | + false, |
| 168 | + summary); |
| 169 | + } |
| 170 | + |
| 171 | + private void ToggleDialog() |
20 | 172 | {
|
21 |
| - currentCount++; |
| 173 | + _createDialogVisible = !_createDialogVisible; |
22 | 174 | }
|
23 | 175 | }
|
0 commit comments