diff --git a/BlazorProject/BlazorProject.csproj b/BlazorProject/BlazorProject.csproj index 66f51c3..5b70e3e 100644 --- a/BlazorProject/BlazorProject.csproj +++ b/BlazorProject/BlazorProject.csproj @@ -7,6 +7,7 @@ + @@ -21,4 +22,8 @@ + + + + diff --git a/BlazorProject/Models/ItemModel.cs b/BlazorProject/Models/ItemModel.cs new file mode 100644 index 0000000..6f6f0bf --- /dev/null +++ b/BlazorProject/Models/ItemModel.cs @@ -0,0 +1,36 @@ +using System.ComponentModel.DataAnnotations; + +namespace BlazorProject.Models; + +public class ItemModel +{ + public int Id { get; set; } + + [Required] + [StringLength(50, ErrorMessage = "Le nom affiché ne doit pas dépasser 50 caractères.")] + public string DisplayName { get; set; } + + [Required] + [StringLength(50, ErrorMessage = "Le nom ne doit pas dépasser 50 caractères.")] + [RegularExpression(@"^[a-z''-'\s]{1,50}$", ErrorMessage = "Seulement les caractères en minuscule sont acceptées.")] + public string Name { get; set; } + + [Required] + [Range(1, 64)] + public int StackSize { get; set; } + + [Required] + [Range(1, 125)] + public int MaxDurability { get; set; } + + public List EnchantCategories { get; set; } + + public List RepairWith { get; set; } + + [Required] + [Range(typeof(bool), "true", "true", ErrorMessage = "Vous devez accepter les conditions.")] + public bool AcceptCondition { get; set; } + + [Required(ErrorMessage = "L'image de l'item est obligatoire !")] + public byte[] ImageContent { get; set; } +} \ No newline at end of file diff --git a/BlazorProject/Pages/Add.razor b/BlazorProject/Pages/Add.razor new file mode 100644 index 0000000..842f899 --- /dev/null +++ b/BlazorProject/Pages/Add.razor @@ -0,0 +1,69 @@ +@page "/add" + +

Add

+ + + + + +

+ +

+

+ +

+

+ +

+

+ +

+

+ Enchant categories: +

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

+

+ Repair with: +

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

+

+ +

+

+ +

+ + +
\ No newline at end of file diff --git a/BlazorProject/Pages/Add.razor.cs b/BlazorProject/Pages/Add.razor.cs new file mode 100644 index 0000000..7311e5a --- /dev/null +++ b/BlazorProject/Pages/Add.razor.cs @@ -0,0 +1,125 @@ +using Blazored.LocalStorage; +using BlazorProject.Models; +using Microsoft.AspNetCore.Components; +using Microsoft.AspNetCore.Components.Forms; +using Microsoft.AspNetCore.Components.WebAssembly.Hosting; + +namespace BlazorProject.Pages; + +public partial class Add +{ + [Inject] + public NavigationManager NavigationManager { get; set; } + + [Inject] + public ILocalStorageService LocalStorage { get; set; } + + [Inject] + public IWebAssemblyHostEnvironment 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.BaseAddress}/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); + NavigationManager.NavigateTo("List.razor"); + } + + 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); + } + } +} \ No newline at end of file diff --git a/BlazorProject/Pages/BlazorRoute.razor b/BlazorProject/Pages/BlazorRoute.razor new file mode 100644 index 0000000..8215b17 --- /dev/null +++ b/BlazorProject/Pages/BlazorRoute.razor @@ -0,0 +1,4 @@ +@page "/BlazorRoute" +@page "/DifferentBlazorRoute" + +

Blazor routing

\ No newline at end of file diff --git a/BlazorProject/Pages/List.razor b/BlazorProject/Pages/List.razor index 27a26a8..25659f0 100644 --- a/BlazorProject/Pages/List.razor +++ b/BlazorProject/Pages/List.razor @@ -3,6 +3,11 @@

List

+
+ + Ajouter + +
+ + + @if (File.Exists($"{WebHostEnvironment.BaseAddress}/images/{context.Name}.png")) + { + @context.DisplayName + } + else + { + @context.DisplayName + } + + @@ -24,5 +41,5 @@ @(string.Join(", ", ((Item)context).RepairWith)) - + \ No newline at end of file diff --git a/BlazorProject/Pages/List.razor.cs b/BlazorProject/Pages/List.razor.cs index 993c62a..06c531b 100644 --- a/BlazorProject/Pages/List.razor.cs +++ b/BlazorProject/Pages/List.razor.cs @@ -1,7 +1,9 @@ using System.Net.Http.Json; +using Blazored.LocalStorage; using Blazorise.DataGrid; using BlazorProject.Models; using Microsoft.AspNetCore.Components; +using Microsoft.AspNetCore.Components.WebAssembly.Hosting; namespace BlazorProject.Pages; @@ -14,9 +16,34 @@ public partial class List [Inject] public HttpClient Http { get; set; } + [Inject] + public ILocalStorageService LocalStorage { get; set; } + + [Inject] + public IWebAssemblyHostEnvironment WebHostEnvironment { get; set; } + [Inject] public NavigationManager NavigationManager { get; set; } + protected override async Task OnAfterRenderAsync(bool firstRender) + { + // Do not treat this action if is not the first render + if (!firstRender) + { + return; + } + + var currentData = await LocalStorage.GetItemAsync("data"); + + // Check if data exist in the local storage + if (currentData == null) + { + // this code add in the local storage the fake data (we load the data sync for initialize the data before load the OnReadData method) + var originalData = await Http.GetFromJsonAsync($"{NavigationManager.BaseUri}sample-data/fake-data.json"); + await LocalStorage.SetItemAsync("data", originalData); + } + } + private async Task OnReadData(DataGridReadDataEventArgs e) { if (e.CancellationToken.IsCancellationRequested) @@ -25,12 +52,12 @@ public partial class List } // When you use a real API, we use this follow code - //var response = await Http.GetJsonAsync( $"http://my-api/api/data?page={e.Page}&pageSize={e.PageSize}" ); - var response = (await Http.GetFromJsonAsync($"{NavigationManager.BaseUri}sample-data/fake-data.json")).Skip((e.Page - 1) * e.PageSize).Take(e.PageSize).ToList(); + //var response = await Http.GetJsonAsync( $"http://my-api/api/data?page={e.Page}&pageSize={e.PageSize}" ); + var response = (await LocalStorage.GetItemAsync("data")).Skip((e.Page - 1) * e.PageSize).Take(e.PageSize).ToList(); if (!e.CancellationToken.IsCancellationRequested) { - totalItem = (await Http.GetFromJsonAsync>($"{NavigationManager.BaseUri}sample-data/fake-data.json")).Count; + totalItem = (await LocalStorage.GetItemAsync>("data")).Count; items = new List(response); // an actual data for the current page } } diff --git a/BlazorProject/Program.cs b/BlazorProject/Program.cs index d75cbac..bd3f936 100644 --- a/BlazorProject/Program.cs +++ b/BlazorProject/Program.cs @@ -1,3 +1,4 @@ +using Blazored.LocalStorage; using Blazorise; using Blazorise.Bootstrap; using Blazorise.Icons.FontAwesome; @@ -7,6 +8,7 @@ using BlazorProject; var builder = WebAssemblyHostBuilder.CreateDefault(args); +builder.Services.AddBlazoredLocalStorage(); builder.Services .AddBlazorise() .AddBootstrapProviders() diff --git a/BlazorProject/wwwroot/images/default.png b/BlazorProject/wwwroot/images/default.png new file mode 100644 index 0000000..a7446c9 Binary files /dev/null and b/BlazorProject/wwwroot/images/default.png differ diff --git a/BlazorProject/wwwroot/images/rick&morty.png b/BlazorProject/wwwroot/images/rick&morty.png new file mode 100644 index 0000000..cde1733 Binary files /dev/null and b/BlazorProject/wwwroot/images/rick&morty.png differ diff --git a/BlazorProject/wwwroot/images/vader.png b/BlazorProject/wwwroot/images/vader.png new file mode 100644 index 0000000..9f66e01 Binary files /dev/null and b/BlazorProject/wwwroot/images/vader.png differ