Added messages to category manager for success, failure

This commit is contained in:
Mia Rose Winter 2024-02-04 14:32:22 +01:00
parent b1d3031333
commit 894a653cdd
Signed by: miawinter
GPG key ID: 4B6F6A83178F595E
3 changed files with 34 additions and 9 deletions

View file

@ -20,6 +20,12 @@
<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">
@ -58,23 +64,30 @@
private InputModel Model { get; set; } = new();
private List<Category> Categories { get; set; } = new();
private string Message { get; set; } = string.Empty;
protected override async Task OnInitializedAsync() {
await using var context = await ContextFactory.CreateDbContextAsync();
Categories = await context.Set<Category>().OrderBy(c => c.Color).ToListAsync();
}
private async Task AddCategory_OnValidSubmit() {
await using var context = await ContextFactory.CreateDbContextAsync();
try {
await using var context = await ContextFactory.CreateDbContextAsync();
var category = new Category {
Name = Model.Name.Trim(),
Color = Model.Color
};
var category = new Category {
Name = Model.Name.Trim(),
Color = Model.Color
};
await context.AddAsync(category);
await context.SaveChangesAsync();
Categories.Add(category);
Model = new();
await context.AddAsync(category);
await context.SaveChangesAsync();
Categories.Add(category);
Model = new();
Message = Localizer["Category_Success"];
} catch {
Message = Localizer["Category_Error"];
}
}
private sealed class InputModel {

View file

@ -131,4 +131,10 @@
<data name="Category_Color_Secondary" xml:space="preserve">
<value>Sekundär</value>
</data>
<data name="Category_Success" xml:space="preserve">
<value>Kategorie hinzugefügt.</value>
</data>
<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>
</root>

View file

@ -131,4 +131,10 @@
<data name="Category_Color_Primary" xml:space="preserve">
<value>Primary</value>
</data>
<data name="Category_Success" xml:space="preserve">
<value>Category added.</value>
</data>
<data name="Category_Error" xml:space="preserve">
<value>Failed to add category. Categories need to be unique, ignoring case and accents.</value>
</data>
</root>