181 lines
5.4 KiB
Plaintext
181 lines
5.4 KiB
Plaintext
@page "/tests/add"
|
|
@page "/tests/{id:guid}"
|
|
@using Microsoft.EntityFrameworkCore
|
|
@using Microsoft.AspNetCore.Components
|
|
|
|
<PageTitle>@Title</PageTitle>
|
|
|
|
<h1>@Title</h1>
|
|
@if (Test != null)
|
|
{
|
|
<EditForm Model="@Test" OnValidSubmit="@HandleValidSubmit" class="row g-3 mb-3">
|
|
<DataAnnotationsValidator/>
|
|
<ValidationSummary/>
|
|
<div class="col-lg-6 row g-lg-3 g-md-2 g-1 h-100">
|
|
<div class="col-md-8 form-floating">
|
|
<InputText @bind-Value="Test.Name" id="inputName" class="form-control"/>
|
|
<label for="inputName">Name</label>
|
|
</div>
|
|
<div class="col-md-4 form-floating">
|
|
<InputText @bind-Value="Test.Abbreviation" id="inputAbbreviation" class="form-control"/>
|
|
<label for="inputAbbreviation">Abbreviation</label>
|
|
</div>
|
|
<div class="col-12 form-floating">
|
|
<InputText @bind-Value="Test.PubchemLink" id="inputPubchem" class="form-control"/>
|
|
<label for="inputPubchem">PubChem</label>
|
|
</div>
|
|
</div>
|
|
<div class="col-lg-6 row g-lg-3 g-md-2 g-1">
|
|
<h3>Synonyms</h3>
|
|
@foreach (var synonym in Test.Synonyms)
|
|
{
|
|
<div class="input-group">
|
|
<InputText @bind-Value="synonym.Name" class="form-control"/>
|
|
<button class="btn btn-outline-danger" @onclick="() => DeleteSynonym(synonym)" type="button">x</button>
|
|
</div>
|
|
}
|
|
<div class="input-group">
|
|
<input type="text" class="form-control" id="testNewSynonymInput" @bind="NewSynonym" @bind:event="oninput" @onkeydown="NewSynonymKeydown" @onblur="NewSynonymBlur"/>
|
|
</div>
|
|
</div>
|
|
<div class="col-12">
|
|
<button class="btn btn-primary">Save</button>
|
|
<button type="button" class="btn btn-danger float-end" @onclick="() => ShowDeleteModal = true">Delete</button>
|
|
</div>
|
|
</EditForm>
|
|
}
|
|
else
|
|
{
|
|
<h1>NOT FOUND</h1>
|
|
}
|
|
@if (ShowDeleteModal)
|
|
{
|
|
<div class="modal show" tabindex="-1" style="display: block;">
|
|
<div class="modal-dialog">
|
|
<div class="modal-content">
|
|
<div class="modal-header">
|
|
<h5 class="modal-title">Really delete this test?</h5>
|
|
<button type="button" class="btn-close" @onclick="() => ShowDeleteModal = false" aria-label="Close"></button>
|
|
</div>
|
|
<div class="modal-body">
|
|
<p>Deleted tests cannot be recovered, and all lab test offers for this test are deleted as well.</p>
|
|
</div>
|
|
<div class="modal-footer">
|
|
<button type="button" class="btn btn-secondary" @onclick="() => ShowDeleteModal = false">Abort</button>
|
|
<button type="button" class="btn btn-danger" @onclick="DeleteTest">Delete</button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div class="modal-backdrop fade show"></div>
|
|
}
|
|
|
|
|
|
|
|
|
|
@code {
|
|
|
|
[Inject]
|
|
public NavigationManager NavigationManager { get; set; } = null!;
|
|
|
|
[Inject]
|
|
public IDbContextFactory<AppDbContext> DbContextFactory { get; set; } = null!;
|
|
|
|
[Inject]
|
|
public IJSRuntime Js { get; set; } = null!;
|
|
|
|
|
|
[Parameter]
|
|
public Guid? Id { get; set; }
|
|
|
|
private Test? Test { get; set; }
|
|
|
|
private string Title => Id == null ? "Add test" : "Edit test";
|
|
|
|
private string NewSynonym { get; set; } = "";
|
|
|
|
private List<TestSynonym> NewSynonyms { get; set; } = new();
|
|
private List<TestSynonym> RemovedSynonyms { get; set; } = new();
|
|
|
|
private bool ShowDeleteModal { get; set; }
|
|
|
|
protected override void OnAfterRender(bool firstRender)
|
|
{
|
|
if (firstRender)
|
|
{
|
|
Js.InvokeVoidAsync("PreventEnterKey", "testNewSynonymInput");
|
|
}
|
|
}
|
|
|
|
protected override void OnInitialized()
|
|
{
|
|
base.OnInitialized();
|
|
using var ctx = DbContextFactory.CreateDbContext();
|
|
|
|
if (Id != null)
|
|
{
|
|
Test = ctx.Set<Test>()
|
|
.Include(x => x.Synonyms)
|
|
.First(x => x.Id == TestId.FromGuid(Id.Value));
|
|
}
|
|
else
|
|
{
|
|
Test = new Test();
|
|
}
|
|
}
|
|
|
|
private void HandleValidSubmit()
|
|
{
|
|
using var ctx = DbContextFactory.CreateDbContext();
|
|
if (Id == null)
|
|
{
|
|
ctx.Add(Test!);
|
|
}
|
|
else
|
|
{
|
|
ctx.Update(Test!);
|
|
}
|
|
ctx.AddRange(NewSynonyms);
|
|
ctx.RemoveRange(RemovedSynonyms);
|
|
ctx.SaveChanges();
|
|
NavigationManager.NavigateTo("/tests");
|
|
}
|
|
|
|
private void AddSynonym()
|
|
{
|
|
if (NewSynonym == "") return;
|
|
var synonym = new TestSynonym { Name = NewSynonym };
|
|
Test!.Synonyms.Add(synonym);
|
|
NewSynonyms.Add(synonym);
|
|
NewSynonym = "";
|
|
}
|
|
|
|
private void NewSynonymKeydown(KeyboardEventArgs e)
|
|
{
|
|
if (e.Code == "Enter")
|
|
{
|
|
AddSynonym();
|
|
}
|
|
}
|
|
|
|
private void NewSynonymBlur()
|
|
{
|
|
AddSynonym();
|
|
}
|
|
|
|
private void DeleteSynonym(TestSynonym synonym)
|
|
{
|
|
Test!.Synonyms.Remove(synonym);
|
|
RemovedSynonyms.Add(synonym);
|
|
}
|
|
|
|
private void DeleteTest()
|
|
{
|
|
using var ctx = DbContextFactory.CreateDbContext();
|
|
ctx.RemoveRange(Test!.TestOffers);
|
|
ctx.Remove(Test!);
|
|
ctx.SaveChanges();
|
|
NavigationManager.NavigateTo("/tests");
|
|
}
|
|
|
|
} |