Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

En réalité c'est la suppression d'un utilisateur avec popup
blazor
Antoine JOURDAIN 1 year ago
parent 6954e4a4c1
commit 8824d51e9c

@ -1,4 +1,5 @@
<Router AppAssembly="@typeof(App).Assembly"> <CascadingBlazoredModal>
<Router AppAssembly="@typeof(App).Assembly">
<Found Context="routeData"> <Found Context="routeData">
<RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" /> <RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" />
<FocusOnNavigate RouteData="@routeData" Selector="h1" /> <FocusOnNavigate RouteData="@routeData" Selector="h1" />
@ -10,4 +11,4 @@
</LayoutView> </LayoutView>
</NotFound> </NotFound>
</Router> </Router>
</CascadingBlazoredModal>

@ -0,0 +1,10 @@
<div class="simple-form">
<p>
Are you sure you want to delete @item.Name ?
</p>
<button @onclick="ConfirmDelete" class="btn btn-primary">Delete</button>
<button @onclick="Cancel" class="btn btn-secondary">Cancel</button>
</div>

@ -0,0 +1,38 @@
using Microsoft.AspNetCore.Components;
using Blazored.Modal;
using Blazored.Modal.Services;
using adminBlazor.Services;
using adminBlazor.Models;
namespace adminBlazor.Modals
{
public partial class DeleteConfirmation
{
[CascadingParameter]
public BlazoredModalInstance ModalInstance { get; set; }
[Inject]
public IDataService DataService { get; set; }
[Parameter]
public int Id { get; set; }
private User item = new User();
protected override async Task OnInitializedAsync()
{
// Get the item
item = await DataService.GetById(Id);
}
void ConfirmDelete()
{
ModalInstance.CloseAsync(ModalResult.Ok(true));
}
void Cancel()
{
ModalInstance.CancelAsync();
}
}
}

@ -49,9 +49,9 @@
<DataGridColumn TItem="UserModel" Field="@nameof(UserModel.Id)" Caption="Modifier"> <DataGridColumn TItem="UserModel" Field="@nameof(UserModel.Id)" Caption="Modifier">
<DisplayTemplate> <DisplayTemplate>
<a href="EditUser/@(context.Id)" class="btn btn-primary"><i class="fa fa-edit"></i> Editer</a> <a href="EditUser/@(context.Id)" class="btn btn-primary"><i class="fa fa-edit"></i> Editer</a>
<button type="button" class="btn btn-primary" @onclick="() => OnDelete(context.Id)"><i class="fa fa-trash"></i> Supprimer</button>
</DisplayTemplate> </DisplayTemplate>
</DataGridColumn> </DataGridColumn>
</DataGrid> </DataGrid>

@ -6,6 +6,9 @@ using adminBlazor.Models;
using System.Net.Http; using System.Net.Http;
using System.Net.Http.Json; using System.Net.Http.Json;
using adminBlazor.Services; using adminBlazor.Services;
using Blazored.Modal.Services;
using Blazored.Modal;
using adminBlazor.Modals;
namespace adminBlazor.Pages namespace adminBlazor.Pages
{ {
@ -24,6 +27,12 @@ namespace adminBlazor.Pages
[Inject] [Inject]
public NavigationManager NavigationManager { get; set; } public NavigationManager NavigationManager { get; set; }
[CascadingParameter]
public IModalService Modal { get; set; }
[Inject]
public IDataService DataService { get; set; }
protected override async Task OnAfterRenderAsync(bool firstRender) protected override async Task OnAfterRenderAsync(bool firstRender)
{ {
@ -61,6 +70,25 @@ namespace adminBlazor.Pages
_users = new List<UserModel>(response); // an actual data for the current page _users = new List<UserModel>(response); // an actual data for the current page
} }
} }
private async void OnDelete(int id)
{
var parameters = new ModalParameters();
parameters.Add("Id", id);
var modal = Modal.Show<DeleteConfirmation>("Delete Confirmation", parameters);
var result = await modal.Result;
if (result.Cancelled)
{
return;
}
await DataService.Delete(id);
// Reload the page
NavigationManager.NavigateTo("list", true);
}
} }
} }

@ -13,6 +13,7 @@
<link href="css/site.css" rel="stylesheet" /> <link href="css/site.css" rel="stylesheet" />
<link href="adminBlazor.styles.css" rel="stylesheet" /> <link href="adminBlazor.styles.css" rel="stylesheet" />
<link rel="icon" type="image/png" href="favicon.png" /> <link rel="icon" type="image/png" href="favicon.png" />
<link href="_content/Blazored.Modal/blazored-modal.css" rel="stylesheet" />
<component type="typeof(HeadOutlet)" render-mode="ServerPrerendered" /> <component type="typeof(HeadOutlet)" render-mode="ServerPrerendered" />
</head> </head>
<body> <body>
@ -30,6 +31,7 @@
</div> </div>
<script src="_framework/blazor.server.js"></script> <script src="_framework/blazor.server.js"></script>
<script src="_content/Blazored.Modal/blazored.modal.js"></script>
</body> </body>
</html> </html>

@ -6,6 +6,7 @@ using Blazorise.Bootstrap;
using Blazorise.Icons.FontAwesome; using Blazorise.Icons.FontAwesome;
using Blazored.LocalStorage; using Blazored.LocalStorage;
using adminBlazor.Services; using adminBlazor.Services;
using Blazored.Modal;
var builder = WebApplication.CreateBuilder(args); var builder = WebApplication.CreateBuilder(args);
builder.Services.AddScoped<IDataService, DataLocalService>(); builder.Services.AddScoped<IDataService, DataLocalService>();
@ -16,7 +17,7 @@ builder.Services.AddSingleton<WeatherForecastService>();
builder.Services.AddHttpClient(); builder.Services.AddHttpClient();
builder.Services.AddBlazoredLocalStorage(); builder.Services.AddBlazoredLocalStorage();
builder.Services.AddBlazoredModal();
builder.Services builder.Services

@ -104,6 +104,30 @@ namespace adminBlazor.Services
// Modify the content of the item // Modify the content of the item
// Save the data
await _localStorage.SetItemAsync("data", currentData);
}
public async Task Delete(int id)
{
// Get the current data
var currentData = await _localStorage.GetItemAsync<List<User>>("data");
// Get the item int the list
var item = currentData.FirstOrDefault(w => w.Id == id);
// Delete item in
currentData.Remove(item);
// Delete the image
var imagePathInfo = new DirectoryInfo($"{_webHostEnvironment.WebRootPath}/images");
var fileName = new FileInfo($"{imagePathInfo}/{item.Name}.png");
if (fileName.Exists)
{
File.Delete(fileName.FullName);
}
// Save the data // Save the data
await _localStorage.SetItemAsync("data", currentData); await _localStorage.SetItemAsync("data", currentData);
} }

@ -16,6 +16,8 @@ namespace adminBlazor.Services
Task<User> GetById(int id); Task<User> GetById(int id);
Task Update(int id, UserModel model); Task Update(int id, UserModel model);
Task Delete(int id);
} }
} }

@ -10,3 +10,5 @@
@using adminBlazor.Shared @using adminBlazor.Shared
@using Blazorise.DataGrid @using Blazorise.DataGrid
@using adminBlazor.Services @using adminBlazor.Services
@using Blazored.Modal
@using Blazored.Modal.Services

@ -13,5 +13,9 @@
<PackageReference Include="Blazorise.Bootstrap" Version="1.4.0" /> <PackageReference Include="Blazorise.Bootstrap" Version="1.4.0" />
<PackageReference Include="Blazorise.DataGrid" Version="1.4.0" /> <PackageReference Include="Blazorise.DataGrid" Version="1.4.0" />
<PackageReference Include="Blazorise.Icons.FontAwesome" Version="1.4.0" /> <PackageReference Include="Blazorise.Icons.FontAwesome" Version="1.4.0" />
<PackageReference Include="Blazored.Modal" Version="7.1.0" />
</ItemGroup>
<ItemGroup>
<Content Remove="Modals\DeleteConfirmation.cshtml" />
</ItemGroup> </ItemGroup>
</Project> </Project>

Loading…
Cancel
Save