From 0ae37dc136b2888cc3f45a4bd032146f0cfea89b Mon Sep 17 00:00:00 2001 From: nifranco Date: Fri, 28 Oct 2022 15:02:46 +0200 Subject: [PATCH] Creation of the form --- BlazorTp1/Models/Items.cs | 15 ----- BlazorTp1/Pages/Add.razor | 66 ++++++++++++++++++++ BlazorTp1/Pages/Add.razor.cs | 117 ++++++++++++++++++++++++++++++++++- 3 files changed, 182 insertions(+), 16 deletions(-) delete mode 100644 BlazorTp1/Models/Items.cs diff --git a/BlazorTp1/Models/Items.cs b/BlazorTp1/Models/Items.cs deleted file mode 100644 index d2207dd..0000000 --- a/BlazorTp1/Models/Items.cs +++ /dev/null @@ -1,15 +0,0 @@ -namespace BlazorTp1.Models -{ - public class Items - { - public int Id { get; set; } - public string DisplayName { get; set; } - public string Name { get; set; } - public int StackSize { get; set; } - public int MaxDurability { get; set; } - public List EnchantCategories { get; set; } - public List RepairWith { get; set; } - public DateTime CreatedDate { get; set; } - public DateTime? UpdatedDate { get; set; } - } -} diff --git a/BlazorTp1/Pages/Add.razor b/BlazorTp1/Pages/Add.razor index cfbc78b..83fc777 100644 --- a/BlazorTp1/Pages/Add.razor +++ b/BlazorTp1/Pages/Add.razor @@ -1,5 +1,71 @@ @page "/add" +@using BlazorTp1.Models

Add

+ + + + +

+ +

+

+ +

+

+ +

+

+ +

+

+ Enchant categories: +

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

+

+ Repair with: +

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

+

+ +

+

+ +

+ + +
diff --git a/BlazorTp1/Pages/Add.razor.cs b/BlazorTp1/Pages/Add.razor.cs index 1c2cff2..ce51866 100644 --- a/BlazorTp1/Pages/Add.razor.cs +++ b/BlazorTp1/Pages/Add.razor.cs @@ -1,6 +1,121 @@ -namespace BlazorTp1.Pages +using Blazored.LocalStorage; +using BlazorTp1.Models; +using Microsoft.AspNetCore.Components; +using Microsoft.AspNetCore.Components.Forms; + +namespace BlazorTp1.Pages { public partial class Add { + [Inject] + public ILocalStorageService LocalStorage { get; set; } + + [Inject] + public IWebHostEnvironment WebHostEnvironment { get; set; } + + /// + /// The default enchant categories. + /// + private List enchantCategories = new List() { "armor", "armor_head", "armor_chest", "weapon", "digger", "breakable", "vanishable" }; + + /// + /// 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" }; + + /// + /// The current item model + /// + private ItemModel itemModel = new() + { + EnchantCategories = new List(), + RepairWith = new List() + }; + + private async void HandleValidSubmit() + { + // Get the current data + var currentData = await LocalStorage.GetItemAsync>("data"); + + // Simulate the Id + itemModel.Id = currentData.Max(s => s.Id) + 1; + + // Add the item to the current data + currentData.Add(new Item + { + Id = itemModel.Id, + DisplayName = itemModel.DisplayName, + Name = itemModel.Name, + RepairWith = itemModel.RepairWith, + EnchantCategories = itemModel.EnchantCategories, + MaxDurability = itemModel.MaxDurability, + StackSize = itemModel.StackSize, + CreatedDate = DateTime.Now + }); + + // Save the image + var imagePathInfo = new DirectoryInfo($"{WebHostEnvironment.WebRootPath}/images"); + + // Check if the folder "images" exist + if (!imagePathInfo.Exists) + { + imagePathInfo.Create(); + } + + // Determine the image name + var fileName = new FileInfo($"{imagePathInfo}/{itemModel.Name}.png"); + + // Write the file content + await File.WriteAllBytesAsync(fileName.FullName, itemModel.ImageContent); + + // Save the data + await LocalStorage.SetItemAsync("data", currentData); + } + + 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); + } + } } }