Patrick BRUGIERE 1 year ago
commit a51f6227d8

@ -13,10 +13,10 @@ namespace adminBlazor.Factories
{ {
return new VocabularyListModel return new VocabularyListModel
{ {
id = voc.id, Id = voc.Id,
name = voc.name, Name = voc.Name,
image = voc.image, Image = voc.Image,
aut = voc.aut Aut = voc.Aut
}; };
} }
@ -24,19 +24,19 @@ namespace adminBlazor.Factories
{ {
return new VocabularyList return new VocabularyList
{ {
id = voc.id, Id = voc.Id,
name = voc.name, Name = voc.Name,
image = voc.image, Image = voc.Image,
aut = voc.aut Aut = voc.Aut
}; };
} }
public static void Update(VocabularyList item, VocabularyListModel voc) public static void Update(VocabularyList item, VocabularyListModel voc)
{ {
if (!string.IsNullOrEmpty(voc.name)) if (!string.IsNullOrEmpty(voc.Name))
item.name = voc.name; item.Name = voc.Name;
if (!string.IsNullOrEmpty(voc.image)) if (!string.IsNullOrEmpty(voc.Image))
item.image = voc.image; item.Image = voc.Image;
} }
} }

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

@ -6,7 +6,7 @@ using adminBlazor.Models;
namespace adminBlazor.Modals namespace adminBlazor.Modals
{ {
public partial class DeleteConfirmation public partial class UserDeleteConfirmation
{ {
[CascadingParameter] [CascadingParameter]
public BlazoredModalInstance ModalInstance { get; set; } public BlazoredModalInstance ModalInstance { get; set; }

@ -0,0 +1,10 @@
<div class="simple-form">
<p>
Are you sure you want to delete vocabulary list : @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 VocListDeleteConfirmation
{
[CascadingParameter]
public BlazoredModalInstance ModalInstance { get; set; }
[Inject]
public IVocListService VocListService { get; set; }
[Parameter]
public int Id { get; set; }
private VocabularyList item = new VocabularyList();
protected override async Task OnInitializedAsync()
{
// Get the item
item = await VocListService.GetById(Id);
}
void ConfirmDelete()
{
ModalInstance.CloseAsync(ModalResult.Ok(true));
}
void Cancel()
{
ModalInstance.CancelAsync();
}
}
}

@ -3,10 +3,10 @@ namespace adminBlazor.Models
{ {
public class VocabularyList public class VocabularyList
{ {
public int id; public int Id { get; set; }
public string name; public string Name { get; set; }
public string image; public string Image { get; set; }
public int? aut; public int? Aut { get; set; }
} }
} }

@ -6,13 +6,13 @@ namespace adminBlazor.Models
public class VocabularyListModel public class VocabularyListModel
{ {
[Required] [Required]
public int id { get; set; } public int Id { get; set; }
public string name { get; set; } public string Name { get; set; }
public string image { get; set; } public string Image { get; set; }
public int? aut { get; set; } public int? Aut { get; set; }
} }
} }

@ -11,14 +11,16 @@
<p> <p>
<label for="name"> <label for="name">
Name: Name:
<InputText id="name" @bind-Value="voc.name" /> <InputText id="name" @bind-Value="voc.Name" />
<ValidationMessage For="@(() => voc.Name)" />
</label> </label>
</p> </p>
<p> <p>
<label for="image"> <label for="image">
Image: Image:
<InputText id="image" @bind-Value="voc.image" /> <InputText id="image" @bind-Value="voc.Image" />
<ValidationMessage For="@(() => voc.Image)" />
</label> </label>
</p> </p>

@ -81,7 +81,7 @@ namespace adminBlazor.Pages
var parameters = new ModalParameters(); var parameters = new ModalParameters();
parameters.Add("Id", id); parameters.Add("Id", id);
var modal = Modal.Show<DeleteConfirmation>("Delete Confirmation", parameters); var modal = Modal.Show<UserDeleteConfirmation>("Delete Confirmation", parameters);
var result = await modal.Result; var result = await modal.Result;
if (result.Cancelled) if (result.Cancelled)

@ -16,16 +16,16 @@
ShowPager ShowPager
Responsive> Responsive>
<DataGridColumn TItem="VocabularyList" Field="@nameof(VocabularyList.id)" Caption="id" /> <DataGridColumn TItem="VocabularyList" Field="@nameof(VocabularyList.Id)" Caption="id" />
<DataGridColumn TItem="VocabularyList" Field="@nameof(VocabularyList.name)" Caption="Name" /> <DataGridColumn TItem="VocabularyList" Field="@nameof(VocabularyList.Name)" Caption="Name" />
<DataGridColumn TItem="VocabularyList" Field="@nameof(VocabularyList.image)" Caption="Image" /> <DataGridColumn TItem="VocabularyList" Field="@nameof(VocabularyList.Image)" Caption="Image" />
<DataGridColumn TItem="VocabularyList" Field="@nameof(VocabularyList.aut)" Caption="Author ID"> <DataGridColumn TItem="VocabularyList" Field="@nameof(VocabularyList.Aut)" Caption="Author ID">
<DisplayTemplate> <DisplayTemplate>
@if (context is VocabularyList voc) @if (context is VocabularyList voc)
{ {
@if (voc.aut != null) @if (voc.Aut != null)
{ {
@voc.aut @voc.Aut
} }
else else
{ {
@ -35,9 +35,10 @@
</DisplayTemplate> </DisplayTemplate>
</DataGridColumn> </DataGridColumn>
<DataGridColumn TItem="VocabularyList" Field="@nameof(VocabularyList.id)" Caption="Modifier"> <DataGridColumn TItem="VocabularyList" Field="@nameof(VocabularyList.Id)" Caption="Modifier">
<DisplayTemplate> <DisplayTemplate>
<a href="editVoc/@(context.id)" class="btn btn-primary"><i class="fa fa-edit"></i> Editer</a> <a href="editVoc/@(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>

@ -31,7 +31,7 @@ namespace adminBlazor.Pages
public IVocListService VocListService { get; set; } public IVocListService VocListService { get; set; }
protected async Task OnAfterRenderAsync(bool firstRender) /*protected async Task OnAfterRenderAsync(bool firstRender)
{ {
// Do not treat this action if is not the first render // Do not treat this action if is not the first render
if (firstRender) if (firstRender)
@ -48,7 +48,7 @@ namespace adminBlazor.Pages
var originalData = Http.GetFromJsonAsync<VocabularyList[]>($"{NavigationManager.BaseUri}voc.json").Result; var originalData = Http.GetFromJsonAsync<VocabularyList[]>($"{NavigationManager.BaseUri}voc.json").Result;
await LocalStorage.SetItemAsync("voc", originalData); await LocalStorage.SetItemAsync("voc", originalData);
} }
} }*/
private async Task OnReadData(DataGridReadDataEventArgs<VocabularyList> e) private async Task OnReadData(DataGridReadDataEventArgs<VocabularyList> e)
{ {
@ -59,7 +59,7 @@ namespace adminBlazor.Pages
// When you use a real API, we use this follow code // 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 Http.GetJsonAsync<Data[]>( $"http://my-api/api/data?page={e.Page}&pageSize={e.PageSize}" );
//var response = (await LocalStorage.GetItemAsync<User[]>("data")).Skip((e.Page - 1) * e.PageSize).Take(e.PageSize).ToList(); //var response = (await LocalStorage.GetItemAsync<VocabularyList[]>("voc")).Skip((e.Page - 1) * e.PageSize).Take(e.PageSize).ToList();
if (!e.CancellationToken.IsCancellationRequested) if (!e.CancellationToken.IsCancellationRequested)
{ {
@ -67,5 +67,24 @@ namespace adminBlazor.Pages
totalVocList = await VocListService.Count();// an actual data for the current page totalVocList = await VocListService.Count();// 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<VocListDeleteConfirmation>("Delete Confirmation", parameters);
var result = await modal.Result;
if (result.Cancelled)
{
return;
}
await VocListService.Delete(id);
// Reload the page
NavigationManager.NavigateTo("voc", true);
}
} }
} }

@ -34,7 +34,7 @@ namespace adminBlazor.Services
{ {
var currentList = await _localStorage.GetItemAsync<List<VocabularyList>>("voc"); var currentList = await _localStorage.GetItemAsync<List<VocabularyList>>("voc");
model.id = currentList.Max(s => s.id) + 1; model.Id = currentList.Max(s => s.Id) + 1;
currentList.Add(VocListFactory.Create(model)); currentList.Add(VocListFactory.Create(model));
@ -76,17 +76,56 @@ namespace adminBlazor.Services
{ {
var currentLists = await _localStorage.GetItemAsync<List<VocabularyList>>("voc"); var currentLists = await _localStorage.GetItemAsync<List<VocabularyList>>("voc");
return new VocabularyList(); var list = currentLists.FirstOrDefault(w => w.Id == id);
if (list == null)
{
throw new Exception($"Unable to found the item with ID: {id}");
}
return list;
} }
public Task Update(int id, VocabularyListModel model) public async Task Update(int id, VocabularyListModel model)
{ {
throw new NotImplementedException(); // Get the current data
var currentList = await _localStorage.GetItemAsync<List<VocabularyList>>("voc");
var voc = currentList.FirstOrDefault(w => w.Id == id);
if (voc == null)
{
throw new Exception($"Unable to found the item with ID: {id}");
}
// Save the image
//
VocListFactory.Update(voc, model);
// Modify the content of the item
// Save the data
await _localStorage.SetItemAsync("voc", currentList);
} }
public Task Delete(int id) public async Task Delete(int id)
{ {
throw new NotImplementedException(); // Get the current data
var currentList = await _localStorage.GetItemAsync<List<VocabularyList>>("voc");
// Get the item int the list
var voc = currentList.FirstOrDefault(w => w.Id == id);
// Delete item in
currentList.Remove(voc);
// Delete the image
// Save the data
await _localStorage.SetItemAsync("voc", currentList);
} }
} }
} }

Loading…
Cancel
Save