Add code to delete document companies.

This commit is contained in:
2020-05-24 21:00:41 +01:00
parent 14fe17f04b
commit e0ee49c6c2
5 changed files with 94 additions and 45 deletions

View File

@@ -1,33 +0,0 @@
@model Marechai.ViewModels.DocumentCompanyViewModel
@{
ViewData["Title"] = "Delete";
}
<h1>Delete</h1>
<h3>Are you sure you want to delete this?</h3>
<div>
<h4>Document company</h4>
<hr />
<dl class="row">
<dt class="col-sm-2">
@Html.DisplayNameFor(model => model.Name)
</dt>
<dd class="col-sm-10">
@Html.DisplayFor(model => model.Name)
</dd>
<dt class="col-sm-2">
@Html.DisplayNameFor(model => model.Company)
</dt>
<dd class="col-sm-10">
<a asp-action="Details" asp-route-id="@Model.CompanyId" asp-controller="Companies">
@Html.DisplayFor(modelItem => Model.Company)</a>
</dd>
</dl>
<form asp-action="Delete">
<input type="hidden" asp-for="Id" />
<input class="btn btn-danger" type="submit" value="Delete" />
<a asp-action="Index" class="btn btn-secondary">
Back to List
</a>
</form>
</div>

View File

@@ -77,21 +77,26 @@
<span class="btn btn-secondary">
@L["Edit"]
</span>
<span class="btn btn-danger">
@L["Delete"]
</span>
<Button Color="Color.Danger" Clicked="() => {ShowModal(item.Id);}">@L["Delete"]</Button>
</td>
</tr>
}
</tbody>
</table>
@code
{
List<DocumentCompanyViewModel> _companies;
protected override async Task OnInitializedAsync()
{
_companies = await Service.GetAsync();
}
}
<Modal @ref="_frmDelete" IsCentered="true" Closing="@ModalClosing">
<ModalBackdrop/>
<ModalContent Centered="true">
<ModalHeader>
<ModalTitle>@L["Delete company"]</ModalTitle>
<CloseButton Clicked="@HideModal"/>
</ModalHeader>
<ModalBody>
<Text>@string.Format(@L["Are you sure you want to delete the company {0}?"], _currentCompany?.Name)</Text>
</ModalBody>
<ModalFooter>
<Button Color="Color.Primary" Clicked="@HideModal" Disabled="@_deleteInProgress">@L["Cancel"]</Button>
<Button Color="Color.Danger" Clicked="@ConfirmDelete" Disabled="@_deleteInProgress">@L["Delete"]</Button>
</ModalFooter>
</ModalContent>
</Modal>

View File

@@ -0,0 +1,53 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Blazorise;
using Marechai.ViewModels;
namespace Marechai.Pages.Admin
{
public partial class DocumentCompanies
{
List<DocumentCompanyViewModel> _companies;
DocumentCompanyViewModel _currentCompany;
bool _deleteInProgress;
Modal _frmDelete;
protected override async Task OnInitializedAsync() => _companies = await Service.GetAsync();
void ShowModal(int itemId)
{
_currentCompany = _companies.FirstOrDefault(n => n.Id == itemId);
_frmDelete.Show();
}
void HideModal() => _frmDelete.Hide();
async void ConfirmDelete()
{
if(_currentCompany is null)
return;
_deleteInProgress = true;
_companies = null;
// Yield thread to let UI to update
await Task.Yield();
await Service.DeleteAsync(_currentCompany.Id);
_companies = await Service.GetAsync();
_deleteInProgress = false;
_frmDelete.Hide();
// Yield thread to let UI to update
await Task.Yield();
// Tell we finished loading
StateHasChanged();
}
void ModalClosing(ModalClosingEventArgs obj) => _currentCompany = null;
}
}

View File

@@ -150,4 +150,16 @@
<value>Eliminar</value>
<comment>Delete</comment>
</data>
<data name="Delete company" xml:space="preserve">
<value>Eliminar compañía</value>
<comment>Delete company</comment>
</data>
<data name="Are you sure you want to delete the company {0}?" xml:space="preserve">
<value>¿Está seguro de que desea borrar la compañía {0}?</value>
<comment>{0} company name</comment>
</data>
<data name="Cancel" xml:space="preserve">
<value>Cancelar</value>
<comment>Cancel</comment>
</data>
</root>

View File

@@ -21,5 +21,17 @@ namespace Marechai.Services
Company = d.Company.Name,
CompanyId = d.CompanyId
}).ToListAsync();
public async Task DeleteAsync(int id)
{
DocumentCompany item = await _context.DocumentCompanies.FindAsync(id);
if(item is null)
return;
_context.DocumentCompanies.Remove(item);
await _context.SaveChangesAsync();
}
}
}