Implemented extended category management

This commit is contained in:
Mia Rose Winter 2024-02-20 15:40:53 +01:00
parent d6a70d0a03
commit c226331974
Signed by: miawinter
GPG key ID: 4B6F6A83178F595E
3 changed files with 69 additions and 43 deletions

View file

@ -4,55 +4,56 @@
@using System.ComponentModel.DataAnnotations
@using Wave.Utilities
@rendermode InteractiveServer
@attribute [Authorize(Policy = "CategoryManagePermissions")]
@inject IDbContextFactory<ApplicationDbContext> ContextFactory
@inject IStringLocalizer<ManageCategories> Localizer
@inject IMessageDisplay Message
<PageTitle>@(TitlePrefix + Localizer["Title"])</PageTitle>
<ModalComponent Id="@ModalId">
<ChildContent>
<InputLabelComponent LabelText="@Localizer["Category_Label"]">
<InputText @bind-Value="Model.Name" required aria-required class="input input-bordered w-full"
autocomplete="off" placeholder="@Localizer["Category_Name_Placeholder"]"/>
</InputLabelComponent>
<InputLabelComponent>
<InputSelect @bind-Value="Model.Color" required aria-required class="select select-bordered w-full">
@foreach (var color in Enum.GetValues<CategoryColors>()) {
string postfix = CategoryUtilities.GetCssClassPostfixForColor(color);
<option value="@color" class="text-@postfix-content bg-@postfix">
@Localizer["Category_Color_" + color]
</option>
}
</InputSelect>
</InputLabelComponent>
</ChildContent>
<Actions>
<button class="btn btn-primary" @onclick="AddCategory">@Localizer["Category_Submit"]</button>
</Actions>
</ModalComponent>
<h1 class="text-3xl lg:text-5xl font-light mb-6 text-primary">@Localizer["Title"]</h1>
<!-- DaisyUI doesn't seem to respect tailwinds safelist, so we add these classes to trick it
into generating the corresponding css classes
<div class="text-primary text-error text-warning text-info text-secondary text-accent"></div>
<div class="text-primary-content text-error-content text-warning-content text-info-content text-secondary-content text-accent-content"></div>
<div class="bg-primary bg-error bg-warning bg-info bg-secondary bg-accent"></div>
-->
@if (!string.IsNullOrWhiteSpace(Message)) {
<div class="alert alert-info">
<span>@Message</span>
</div>
}
<section class="w-full">
<EditForm method="post" class="w-full" FormName="add-category"
Model="Model" OnValidSubmit="AddCategory_OnValidSubmit">
<InputLabelComponent LabelText="@Localizer["Category_Label"]" For="() => Model.Name">
<div class="join join-vertical md:join-horizontal">
<InputText @bind-Value="Model.Name" required aria-required class="input input-bordered md:flex-1 md:max-w-xs join-item"
autocomplete="off" placeholder="@Localizer["Category_Name_Placeholder"]" />
<InputSelect @bind-Value="Model.Color" required aria-required class="select select-bordered join-item">
@foreach (var color in Enum.GetValues<CategoryColors>()) {
string postfix = CategoryUtilities.GetCssClassPostfixForColor(color);
<option value="@color" class="text-@postfix-content bg-@postfix">
@Localizer["Category_Color_" + color]
</option>
}
</InputSelect>
<button type="submit" class="btn btn-primary join-item">@Localizer["Category_Submit"]</button>
</div>
</InputLabelComponent>
</EditForm>
</section>
<div class="flex gap-2 mb-3">
<button class="btn btn-sm btn-primary" onclick="@(ModalId).showModal()">
@Localizer["Category_Label"]
</button>
</div>
<ul class="flex flex-col gap-2 max-w-xs">
@foreach (var category in Categories) {
string postfix = CategoryUtilities.GetCssClassPostfixForColor(category.Color);
<li class="bg-@postfix text-@postfix-content p-2 border-2 border-current">@category.Name</li>
<li class="bg-@postfix text-@postfix-content p-2 border-2 border-current flex items-center">
<span class="flex-1">@category.Name</span>
<button type="button" class="btn btn-sm btn-square btn-error" title="delete" @onclick="async () => await DeleteCategory(category)">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor" class="w-5 h-5">
<path fill-rule="evenodd" d="M8.75 1A2.75 2.75 0 0 0 6 3.75v.443c-.795.077-1.584.176-2.365.298a.75.75 0 1 0 .23 1.482l.149-.022.841 10.518A2.75 2.75 0 0 0 7.596 19h4.807a2.75 2.75 0 0 0 2.742-2.53l.841-10.52.149.023a.75.75 0 0 0 .23-1.482A41.03 41.03 0 0 0 14 4.193V3.75A2.75 2.75 0 0 0 11.25 1h-2.5ZM10 4c.84 0 1.673.025 2.5.075V3.75c0-.69-.56-1.25-1.25-1.25h-2.5c-.69 0-1.25.56-1.25 1.25v.325C8.327 4.025 9.16 4 10 4ZM8.58 7.72a.75.75 0 0 0-1.5.06l.3 7.5a.75.75 0 1 0 1.5-.06l-.3-7.5Zm4.34.06a.75.75 0 1 0-1.5-.06l-.3 7.5a.75.75 0 1 0 1.5.06l.3-7.5Z" clip-rule="evenodd" />
</svg>
</button>
</li>
}
</ul>
@ -60,18 +61,17 @@
[CascadingParameter(Name = "TitlePrefix")]
private string TitlePrefix { get; set; } = default!;
[SupplyParameterFromForm]
private InputModel Model { get; set; } = new();
private List<Category> Categories { get; set; } = new();
private List<Category> Categories { get; } = new();
private string Message { get; set; } = string.Empty;
private static string ModalId => "CreateCategoryDialog";
protected override async Task OnInitializedAsync() {
await using var context = await ContextFactory.CreateDbContextAsync();
Categories = await context.Set<Category>().OrderBy(c => c.Color).ToListAsync();
(await context.Set<Category>().OrderBy(c => c.Color).ToListAsync()).ForEach(c => Categories.Add(c));
}
private async Task AddCategory_OnValidSubmit() {
private async Task AddCategory() {
try {
await using var context = await ContextFactory.CreateDbContextAsync();
@ -83,10 +83,24 @@
await context.AddAsync(category);
await context.SaveChangesAsync();
Categories.Add(category);
Categories.Sort((c1, c2) => c1.Color.CompareTo(c2.Color));
Model = new();
Message = Localizer["Category_Success"];
Message.ShowSuccess(Localizer["Category_Success"]);
} catch {
Message = Localizer["Category_Error"];
Message.ShowError(Localizer["Category_Error"]);
}
}
private async Task DeleteCategory(Category category) {
try {
await using var context = await ContextFactory.CreateDbContextAsync();
context.Remove(category);
await context.SaveChangesAsync();
Categories.Remove(category);
Message.ShowSuccess(Localizer["Category_Delete_Success"]);
} catch {
Message.ShowError(Localizer["Category_Delete_Error"]);
}
}

View file

@ -137,4 +137,10 @@
<data name="Category_Error" xml:space="preserve">
<value>Fehler beim hinzufügen der Kategorie. Kategorien müssen einzigartis sein, ungeachtet Groß-/Kleinschreibung oder Akzenten.</value>
</data>
<data name="Category_Delete_Success" xml:space="preserve">
<value>Kategorie wurde erfolgreich gelöscht</value>
</data>
<data name="Category_Delete_Error" xml:space="preserve">
<value>Konnte Kategorie nicht löschen, wird sie noch verwendet?</value>
</data>
</root>

View file

@ -137,4 +137,10 @@
<data name="Category_Error" xml:space="preserve">
<value>Failed to add category. Categories need to be unique, ignoring case and accents.</value>
</data>
<data name="Category_Delete_Success" xml:space="preserve">
<value>Successfully deleted Category</value>
</data>
<data name="Category_Delete_Error" xml:space="preserve">
<value>Could not delete Category, is it still used?</value>
</data>
</root>