diff --git a/Blazor/Blazor/Pages/Add.razor.cs b/Blazor/Blazor/Pages/Add.razor.cs index 34b5550..9cf08de 100644 --- a/Blazor/Blazor/Pages/Add.razor.cs +++ b/Blazor/Blazor/Pages/Add.razor.cs @@ -1,5 +1,4 @@ -using Blazored.LocalStorage; -using Blazor.Models; +using Blazor.Models; using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components.Forms; using Blazor.Services; diff --git a/Blazor/Blazor/Pages/Edit.razor b/Blazor/Blazor/Pages/Edit.razor new file mode 100644 index 0000000..57cedae --- /dev/null +++ b/Blazor/Blazor/Pages/Edit.razor @@ -0,0 +1,82 @@ +@page "/edit/{Id:int}" + +

Edit

+ + + + + +

+ +

+

+ +

+

+ +

+

+ +

+

+ Enchant categories: +

+ @foreach (var item in enchantCategories) + { + + } +
+

+

+ Repair with: +

+ @foreach (var item in repairWith) + { + + } +
+

+

+ +

+

+ +

+

+ +

+ + +
\ No newline at end of file diff --git a/Blazor/Blazor/Pages/Edit.razor.cs b/Blazor/Blazor/Pages/Edit.razor.cs new file mode 100644 index 0000000..ec3f108 --- /dev/null +++ b/Blazor/Blazor/Pages/Edit.razor.cs @@ -0,0 +1,119 @@ +using Blazor.Models; +using Blazor.Services; +using Microsoft.AspNetCore.Components; +using Microsoft.AspNetCore.Components.Forms; + +namespace Blazor.Pages +{ + public partial class Edit + { + [Parameter] + public int Id { get; set; } + + /// + /// The default enchant categories. + /// + private List enchantCategories = new List() { "armor", "armor_head", "armor_chest", "weapon", "digger", "breakable", "vanishable" }; + + /// + /// The current item model + /// + private ItemModel itemModel = new() + { + EnchantCategories = new List(), + RepairWith = new List() + }; + + /// + /// The default repair with. + /// + private List repairWith = new List() { "oak_planks", "spruce_planks", "birch_planks", "jungle_planks", "acacia_planks", "dark_oak_planks", "crimson_planks", "warped_planks" }; + + [Inject] + public IDataService DataService { get; set; } + + [Inject] + public NavigationManager NavigationManager { get; set; } + + [Inject] + public IWebHostEnvironment WebHostEnvironment { get; set; } + + protected override async Task OnInitializedAsync() + { + var item = await DataService.GetById(Id); + + var fileContent = await File.ReadAllBytesAsync($"{WebHostEnvironment.WebRootPath}/images/default.png"); + + if (File.Exists($"{WebHostEnvironment.WebRootPath}/images/{itemModel.Name}.png")) + { + fileContent = await File.ReadAllBytesAsync($"{WebHostEnvironment.WebRootPath}/images/{item.Name}.png"); + } + + // Set the model with the item + itemModel = new ItemModel + { + Id = item.Id, + DisplayName = item.DisplayName, + Name = item.Name, + RepairWith = item.RepairWith, + EnchantCategories = item.EnchantCategories, + MaxDurability = item.MaxDurability, + StackSize = item.StackSize, + ImageContent = fileContent + }; + } + + private async void HandleValidSubmit() + { + await DataService.Update(Id, itemModel); + + NavigationManager.NavigateTo("list"); + } + + private async Task LoadImage(InputFileChangeEventArgs e) + { + // Set the content of the image to the model + using (var memoryStream = new MemoryStream()) + { + await e.File.OpenReadStream().CopyToAsync(memoryStream); + itemModel.ImageContent = memoryStream.ToArray(); + } + } + + private void OnEnchantCategoriesChange(string item, object checkedValue) + { + if ((bool)checkedValue) + { + if (!itemModel.EnchantCategories.Contains(item)) + { + itemModel.EnchantCategories.Add(item); + } + + return; + } + + if (itemModel.EnchantCategories.Contains(item)) + { + itemModel.EnchantCategories.Remove(item); + } + } + + private void OnRepairWithChange(string item, object checkedValue) + { + if ((bool)checkedValue) + { + if (!itemModel.RepairWith.Contains(item)) + { + itemModel.RepairWith.Add(item); + } + + return; + } + + if (itemModel.RepairWith.Contains(item)) + { + itemModel.RepairWith.Remove(item); + } + } + } +} diff --git a/Blazor/Blazor/Pages/List.razor.cs b/Blazor/Blazor/Pages/List.razor.cs index c3fb239..5b31d3e 100644 --- a/Blazor/Blazor/Pages/List.razor.cs +++ b/Blazor/Blazor/Pages/List.razor.cs @@ -18,6 +18,9 @@ namespace Blazor.Pages [Inject] public IWebHostEnvironment WebHostEnvironment { get; set; } + [Inject] + public NavigationManager NavigationManager { get; set; } + private async Task OnReadData(DataGridReadDataEventArgs e) { if (e.CancellationToken.IsCancellationRequested)