feat : supprimer admins
continuous-integration/drone/push Build is passing Details

pull/38/head
Maxence GUITARD 1 year ago
parent 4ad1d0aaaa
commit f54ea9ee80

@ -21,6 +21,7 @@
<DataGridColumn TItem="Administrator" Field="@nameof(Administrator.Id)" Caption="Action"> <DataGridColumn TItem="Administrator" Field="@nameof(Administrator.Id)" Caption="Action">
<DisplayTemplate> <DisplayTemplate>
<a href="editAdministrator/@(context.Id)" class="btn btn-primary"><i class="fa fa-edit"></i> Editer</a> <a href="editAdministrator/@(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>

@ -1,6 +1,12 @@
using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components;
using Blazorise.DataGrid; using Blazorise.DataGrid;
using Blazor.ViewClasses; using Blazor.ViewClasses;
using Blazor.Models;
using Blazored.LocalStorage;
using Blazored.Modal.Services;
using Blazor.Modals;
using Blazor.Services;
using Blazored.Modal;
namespace Blazor.Pages namespace Blazor.Pages
{ {
@ -13,9 +19,40 @@ namespace Blazor.Pages
[Inject] [Inject]
public HttpClient Http { get; set; } public HttpClient Http { get; set; }
[CascadingParameter]
public IModalService Modal { get; set; }
[Inject]
public IDataService DataService { get; set; }
[Inject]
public IWebHostEnvironment WebHostEnvironment { get; set; }
[Inject]
public ILocalStorageService LocalStorage { get; set; }
[Inject] [Inject]
public NavigationManager NavigationManager { get; set; } 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<AdministratorsModel[]>("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 = Http.GetFromJsonAsync<AdministratorsModel[]>($"trusting-panini.87-106-126-109.plesk.page/api/chapters").Result;
await LocalStorage.SetItemAsync("data", originalData);
}
}
private async Task OnReadData(DataGridReadDataEventArgs<Administrator> e) private async Task OnReadData(DataGridReadDataEventArgs<Administrator> e)
{ {
if (e.CancellationToken.IsCancellationRequested) if (e.CancellationToken.IsCancellationRequested)
@ -33,5 +70,24 @@ namespace Blazor.Pages
administrators = new List<Administrator>(response); // an actual data for the current page administrators = new List<Administrator>(response); // an actual data for the current page
} }
} }
private async void OnDelete(int id)
{
var parameters = new ModalParameters();
parameters.Add(nameof(Administrator.Id), id);
var modal = Modal.Show<DeleteConfirmation>("Delete Confirmation", parameters);
var result = modal.Result;
if (result.IsCanceled)
{
return;
}
await DataService.Delete(id);
// Reload the page
NavigationManager.NavigateTo("administrators", true);
}
} }
} }

@ -1,47 +0,0 @@
@page "/admins"
@using Blazor.Data
@inject WeatherForecastService ForecastService
<PageTitle>Weather forecast</PageTitle>
<h1>Weather forecast</h1>
<p>This component demonstrates fetching data from a service.</p>
@if (forecasts == null)
{
<p><em>Loading...</em></p>
}
else
{
<table class="table">
<thead>
<tr>
<th>Date</th>
<th>Temp. (C)</th>
<th>Temp. (F)</th>
<th>Summary</th>
</tr>
</thead>
<tbody>
@foreach (var forecast in forecasts)
{
<tr>
<td>@forecast.Date.ToShortDateString()</td>
<td>@forecast.TemperatureC</td>
<td>@forecast.TemperatureF</td>
<td>@forecast.Summary</td>
</tr>
}
</tbody>
</table>
}
@code {
private WeatherForecast[]? forecasts;
protected override async Task OnInitializedAsync()
{
forecasts = await ForecastService.GetForecastAsync(DateOnly.FromDateTime(DateTime.Now));
}
}

@ -1,61 +0,0 @@
using Blazor.Models;
using Blazored.LocalStorage;
using Blazorise.DataGrid;
using Microsoft.AspNetCore.Components;
namespace Blazor.Pages;
public partial class Admins
{
private List<AdministratorsModel> items;
private int totalItem;
[Inject]
public IWebHostEnvironment WebHostEnvironment { get; set; }
[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)
{
// Do not treat this action if is not the first render
if (!firstRender)
{
return;
}
var currentData = await LocalStorage.GetItemAsync<AdministratorsModel[]>("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 = Http.GetFromJsonAsync<AdministratorsModel[]>($"trusting-panini.87-106-126-109.plesk.page/api/chapters").Result;
await LocalStorage.SetItemAsync("data", originalData);
}
}
private async Task OnReadData(DataGridReadDataEventArgs<AdministratorsModel> e)
{
if (e.CancellationToken.IsCancellationRequested)
{
return;
}
// When you use a real API, we use this follow code
//var response = await Http.GetJsonAsync<Data[]>( $"http://my-api/api/data?page={e.Page}&pageSize={e.PageSize}" );
var response = (await LocalStorage.GetItemAsync<AdministratorsModel[]>("data")).Skip((e.Page - 1) * e.PageSize).Take(e.PageSize).ToList();
if (!e.CancellationToken.IsCancellationRequested)
{
totalItem = (await LocalStorage.GetItemAsync<List<AdministratorsModel>>("data")).Count;
items = new List<AdministratorsModel>(response); // an actual data for the current page
}
}
}

@ -25,5 +25,6 @@ namespace Blazor.Services
Task<List<Administrator>> ListAdmin(int currentPage, int pageSize); Task<List<Administrator>> ListAdmin(int currentPage, int pageSize);
Task Delete(int id); Task Delete(int id);
} }
} }

@ -25,7 +25,7 @@
</NavLink> </NavLink>
</div> </div>
<div class="nav-item px-3"> <div class="nav-item px-3">
<NavLink class="nav-link" href="chapters"> <NavLink class="nav-link" href="questions">
<span class="oi oi-plus" aria-hidden="true"></span> Questions <span class="oi oi-plus" aria-hidden="true"></span> Questions
</NavLink> </NavLink>
</div> </div>

Loading…
Cancel
Save