+
+
+
\ No newline at end of file
diff --git a/BlazorTuto/Pages/Add.razor.cs b/BlazorTuto/Pages/Add.razor.cs
new file mode 100644
index 0000000..8e6c958
--- /dev/null
+++ b/BlazorTuto/Pages/Add.razor.cs
@@ -0,0 +1,121 @@
+using Blazored.LocalStorage;
+using BlazorTuto.Models;
+using Microsoft.AspNetCore.Components.Forms;
+using Microsoft.AspNetCore.Components;
+
+namespace BlazorTuto.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);
+ }
+ }
+ }
+}
diff --git a/BlazorTuto/Pages/List.razor b/BlazorTuto/Pages/List.razor
index b916cb8..90c6b61 100644
--- a/BlazorTuto/Pages/List.razor
+++ b/BlazorTuto/Pages/List.razor
@@ -1,34 +1,34 @@
@page "/list"
+@using BlazorTuto.Models
List
-@if (items != null)
-{
-
-
-
-
Id
-
Display Name
-
Stack Size
-
Maximum Durability
-
Enchant Categories
-
Repair With
-
Created Date
-
-
-
- @foreach (var item in items)
- {
-
-
@item.Id
-
@item.DisplayName
-
@item.StackSize
-
@item.MaxDurability
-
@(string.Join(", ", item.EnchantCategories))
-
@(string.Join(", ", item.RepairWith))
-
@item.CreatedDate.ToShortDateString()
-
- }
-
-
-}
\ No newline at end of file
+
+
+ Ajouter
+
+
+
+
+
+
+
+
+
+
+ @(string.Join(", ", ((Item)context).EnchantCategories))
+
+
+
+
+ @(string.Join(", ", ((Item)context).RepairWith))
+
+
+
+
\ No newline at end of file
diff --git a/BlazorTuto/Pages/List.razor.cs b/BlazorTuto/Pages/List.razor.cs
index f2827b3..0f0db7a 100644
--- a/BlazorTuto/Pages/List.razor.cs
+++ b/BlazorTuto/Pages/List.razor.cs
@@ -1,21 +1,60 @@
-using BlazorTuto.Models;
+using Blazored.LocalStorage;
+using Blazorise.DataGrid;
+using BlazorTuto.Models;
using Microsoft.AspNetCore.Components;
namespace BlazorTuto.Pages
{
- public partial class List
+ public partial class List
+ {
+ private List items;
+
+ private int totalItem;
+
+ [Inject]
+ public HttpClient Http { get; set; }
+
+ [Inject]
+ public ILocalStorageService LocalStorage { get; set; }
+
+ [Inject]
+ public NavigationManager NavigationManager { get; set; }
+
+ protected override async Task OnAfterRenderAsync(bool firstRender)
{
- private Item[] items;
+ // Do not treat this action if is not the first render
+ if (!firstRender)
+ {
+ return;
+ }
+
+ var currentData = await LocalStorage.GetItemAsync("data");
- [Inject]
- public HttpClient Http { get; set; }
+ // 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 = Http.GetFromJsonAsync($"{NavigationManager.BaseUri}fake-data.json").Result;
+ await LocalStorage.SetItemAsync("data", originalData);
+ }
+ }
+
+ private async Task OnReadData(DataGridReadDataEventArgs e)
+ {
+ if (e.CancellationToken.IsCancellationRequested)
+ {
+ return;
+ }
- [Inject]
- public NavigationManager NavigationManager { get; set; }
+ // 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 LocalStorage.GetItemAsync("data")).Skip((e.Page - 1) * e.PageSize).Take(e.PageSize).ToList();
- protected override async Task OnInitializedAsync()
+ if (!e.CancellationToken.IsCancellationRequested)
{
- items = await Http.GetFromJsonAsync($"{NavigationManager.BaseUri}fake-data.json");
+ totalItem = (await LocalStorage.GetItemAsync>("data")).Count;
+ items = new List(response); // an actual data for the current page
}
}
+ }
}
diff --git a/BlazorTuto/Pages/_Layout.cshtml b/BlazorTuto/Pages/_Layout.cshtml
index 7843c63..384459a 100644
--- a/BlazorTuto/Pages/_Layout.cshtml
+++ b/BlazorTuto/Pages/_Layout.cshtml
@@ -28,5 +28,10 @@
+
+
+
+
+