From ae949af7cdbb4d64e85b53e3c90742a616e56bf3 Mon Sep 17 00:00:00 2001 From: calledude <22471295+calledude@users.noreply.github.com> Date: Sat, 29 Jul 2023 23:05:47 +0200 Subject: [PATCH] Initial implementation of Tags page --- Modix.Web/Pages/Tags.razor | 164 +++++++++++++++++++++++++++++++++++-- 1 file changed, 158 insertions(+), 6 deletions(-) diff --git a/Modix.Web/Pages/Tags.razor b/Modix.Web/Pages/Tags.razor index ae7120e22..ee4ebded9 100644 --- a/Modix.Web/Pages/Tags.razor +++ b/Modix.Web/Pages/Tags.razor @@ -1,23 +1,175 @@ @page "/tags" +@using Modix.Data.Models.Core; +@using Modix.Data.Models.Tags; +@using Modix.Services.Tags; +@using Modix.Web.Services; +@using MudBlazor +@using System.Globalization; +@using Modix.Data.Models.Core; Tags -

Tags

-

Current count: @currentCount

- + + Tags + @if (Data is not null) + { + + + Create Tag + + + + + Preview + + + +
+ Save +
+ Cancel +
+
+ + + + + Create + + Refresh + + + + + Name + Last Modified + Owner + Content + Uses + + + @tag.Name + @tag.Created.ToString("dd/MM/yy, h:MM:ss tt") + @tag.OwnerName + + + + @tag.Uses + + + + + + } +
@code { - private int currentCount = 0; - private void IncrementCount() + public record TagData( + string Name, + DateTimeOffset Created, + bool IsOwnedByRole, + GuildUserBrief? OwnerUser, + GuildRoleBrief? OwnerRole, + string? OwnerName, + string Content, + uint Uses, + bool CanMaintain, + TagSummary TagSummary); + + + [Inject] + ITagService TagService { get; set; } = null!; + + [Inject] + DiscordUserService DiscordUserService { get; set; } = null!; + + [Inject] + IDialogService DialogService { get; set; } = null!; + + TagData[]? Data { get; set; } + string? query; + string? _tagNameValue; + string? _tagContentValue; + bool _createDialogVisible; + + protected override async Task OnInitializedAsync() => await FetchData(); + + private async Task FetchData() + { + var currentGuild = DiscordUserService.GetUserGuild(); + + var summaries = await TagService.GetSummariesAsync(new TagSearchCriteria + { + GuildId = currentGuild.Id, + }); + + Data = summaries + .Select(x => CreateTagData(x)) + .ToArray(); + + // foreach (var tag in data) + // { + // // TODO Revisit this functionality + // tag.CanMaintain = false; + // } + } + + private bool FilterFunction(TagData tag) + { + if (string.IsNullOrWhiteSpace(query)) + return true; + + if (tag.OwnerUser is not null && (tag.OwnerUser.Username.Contains(query) || tag.OwnerUser.Id.ToString() == query)) + return true; + + if (tag.OwnerRole?.Name.Contains(query) ?? false) + return true; + + if (tag.Name.Contains(query)) + return true; + + if (tag.Content.Contains(query)) + return true; + + return false; + } + + private async Task SaveTag() + { + var currentGuild = DiscordUserService.GetUserGuild(); + var currentUser = await DiscordUserService.GetCurrentUserAsync(); + await TagService.CreateTagAsync(currentGuild.Id, currentUser.Id, _tagNameValue, _tagContentValue); + var createdTag = await TagService.GetTagAsync(currentGuild.Id, _tagNameValue); + Data = Data!.Append(CreateTagData(createdTag)).ToArray(); + + _createDialogVisible = false; + } + + private TagData CreateTagData(TagSummary summary) + { + return new TagData( + summary.Name, + summary.CreateAction.Created, + summary.OwnerRole is not null, + summary.OwnerUser, + summary.OwnerRole, + summary.OwnerRole?.Name ?? summary.OwnerUser?.Username, + summary.Content, + summary.Uses, + false, + summary); + } + + private void ToggleDialog() { - currentCount++; + _createDialogVisible = !_createDialogVisible; } }