diff --git a/Blazor/Blazor/App.razor b/Blazor/Blazor/App.razor index 623580d..54966a1 100644 --- a/Blazor/Blazor/App.razor +++ b/Blazor/Blazor/App.razor @@ -1,12 +1,14 @@ - - - - - - - Not found - - Sorry, there's nothing at this address. - - - + + + + + + + + Not found + + Sorry, there's nothing at this address. + + + + diff --git a/Blazor/Blazor/Blazor.csproj b/Blazor/Blazor/Blazor.csproj index 613ed31..05b5d69 100644 --- a/Blazor/Blazor/Blazor.csproj +++ b/Blazor/Blazor/Blazor.csproj @@ -8,6 +8,7 @@ + diff --git a/Blazor/Blazor/Factories/ItemFactory.cs b/Blazor/Blazor/Factories/ItemFactory.cs new file mode 100644 index 0000000..a46ec2d --- /dev/null +++ b/Blazor/Blazor/Factories/ItemFactory.cs @@ -0,0 +1,48 @@ +using Blazor.Models; + +namespace Blazor.Factories +{ + public static class ItemFactory + { + public static ItemModel ToModel(Item item, byte[] imageContent) + { + return new ItemModel + { + Id = item.Id, + DisplayName = item.DisplayName, + Name = item.Name, + RepairWith = item.RepairWith, + EnchantCategories = item.EnchantCategories, + MaxDurability = item.MaxDurability, + StackSize = item.StackSize, + ImageContent = imageContent + }; + } + + public static Item Create(ItemModel model) + { + return new Item + { + Id = model.Id, + DisplayName = model.DisplayName, + Name = model.Name, + RepairWith = model.RepairWith, + EnchantCategories = model.EnchantCategories, + MaxDurability = model.MaxDurability, + StackSize = model.StackSize, + CreatedDate = DateTime.Now + }; + } + + public static void Update(Item item, ItemModel model) + { + item.DisplayName = model.DisplayName; + item.Name = model.Name; + item.RepairWith = model.RepairWith; + item.EnchantCategories = model.EnchantCategories; + item.MaxDurability = model.MaxDurability; + item.StackSize = model.StackSize; + item.UpdatedDate = DateTime.Now; + } + } +} diff --git a/Blazor/Blazor/Modals/DeleteConfirmation.razor b/Blazor/Blazor/Modals/DeleteConfirmation.razor new file mode 100644 index 0000000..b565665 --- /dev/null +++ b/Blazor/Blazor/Modals/DeleteConfirmation.razor @@ -0,0 +1,10 @@ + + + + Are you sure you want to delete @item.DisplayName ? + + + Delete + + Cancel + \ No newline at end of file diff --git a/Blazor/Blazor/Modals/DeleteConfirmation.razor.cs b/Blazor/Blazor/Modals/DeleteConfirmation.razor.cs new file mode 100644 index 0000000..846860b --- /dev/null +++ b/Blazor/Blazor/Modals/DeleteConfirmation.razor.cs @@ -0,0 +1,38 @@ +using Blazor.Models; +using Blazor.Services; +using Blazored.Modal; +using Blazored.Modal.Services; +using Microsoft.AspNetCore.Components; + +namespace Blazor.Modals +{ + public partial class DeleteConfirmation + { + [CascadingParameter] + public BlazoredModalInstance ModalInstance { get; set; } + + [Inject] + public IDataService DataService { get; set; } + + [Parameter] + public int Id { get; set; } + + private Item item = new Item(); + + protected override async Task OnInitializedAsync() + { + // Get the item + item = await DataService.GetById(Id); + } + + void ConfirmDelete() + { + ModalInstance.CloseAsync(ModalResult.Ok(true)); + } + + void Cancel() + { + ModalInstance.CancelAsync(); + } + } +} diff --git a/Blazor/Blazor/Pages/Edit.razor.cs b/Blazor/Blazor/Pages/Edit.razor.cs index ec3f108..442d2b0 100644 --- a/Blazor/Blazor/Pages/Edit.razor.cs +++ b/Blazor/Blazor/Pages/Edit.razor.cs @@ -1,4 +1,5 @@ -using Blazor.Models; +using Blazor.Factories; +using Blazor.Models; using Blazor.Services; using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components.Forms; @@ -50,17 +51,7 @@ namespace Blazor.Pages } // 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 - }; + itemModel = ItemFactory.ToModel(item, fileContent); } private async void HandleValidSubmit() diff --git a/Blazor/Blazor/Pages/List.razor b/Blazor/Blazor/Pages/List.razor index 021548b..f0029b4 100644 --- a/Blazor/Blazor/Pages/List.razor +++ b/Blazor/Blazor/Pages/List.razor @@ -28,11 +28,6 @@ } - - - - Editer - @@ -48,4 +43,10 @@ + + + Editer + OnDelete(context.Id)"> Supprimer + + \ No newline at end of file diff --git a/Blazor/Blazor/Pages/List.razor.cs b/Blazor/Blazor/Pages/List.razor.cs index 5b31d3e..d8b686f 100644 --- a/Blazor/Blazor/Pages/List.razor.cs +++ b/Blazor/Blazor/Pages/List.razor.cs @@ -1,6 +1,9 @@ -using Blazor.Models; +using Blazor.Modals; +using Blazor.Models; using Blazor.Services; using Blazored.LocalStorage; +using Blazored.Modal; +using Blazored.Modal.Services; using Blazorise.DataGrid; using Microsoft.AspNetCore.Components; @@ -21,6 +24,9 @@ namespace Blazor.Pages [Inject] public NavigationManager NavigationManager { get; set; } + [CascadingParameter] + public IModalService Modal { get; set; } + private async Task OnReadData(DataGridReadDataEventArgs e) { if (e.CancellationToken.IsCancellationRequested) @@ -34,5 +40,24 @@ namespace Blazor.Pages totalItem = await DataService.Count(); } } + + private async void OnDelete(int id) + { + var parameters = new ModalParameters(); + parameters.Add(nameof(Item.Id), id); + + var modal = Modal.Show("Delete Confirmation", parameters); + var result = await modal.Result; + + if (result.Cancelled) + { + return; + } + + await DataService.Delete(id); + + // Reload the page + NavigationManager.NavigateTo("list", true); + } } } \ No newline at end of file diff --git a/Blazor/Blazor/Pages/_Layout.cshtml b/Blazor/Blazor/Pages/_Layout.cshtml index 6612268..839e817 100644 --- a/Blazor/Blazor/Pages/_Layout.cshtml +++ b/Blazor/Blazor/Pages/_Layout.cshtml @@ -11,6 +11,10 @@ + + + + @@ -28,10 +32,7 @@ + - - - -
Sorry, there's nothing at this address.
+ Are you sure you want to delete @item.DisplayName ? +