92 lines
2.9 KiB
Plaintext
92 lines
2.9 KiB
Plaintext
@page "/contacts/edit/{id:int}"
|
|
@page "/contacts/new"
|
|
@inject HttpClient Http
|
|
@inject NavigationManager NavigationManager
|
|
|
|
@if (Id is not null)
|
|
{
|
|
<h3>Edit @contact.NickName</h3>
|
|
}
|
|
else
|
|
{
|
|
<h3>Create a new contact</h3>
|
|
}
|
|
|
|
<div>
|
|
<EditForm Model="contact" OnValidSubmit="HandleSubmit">
|
|
<DataAnnotationsValidator />
|
|
<SfTextBox CssClass="e-outLine" Placeholder="First Name"
|
|
FloatLabelType="@FloatLabelType.Auto" @bind-Value="contact.FirstName" />
|
|
<SfTextBox CssClass="e-outLine" Placeholder="Last Name"
|
|
FloatLabelType="@FloatLabelType.Auto" @bind-Value="contact.LastName" />
|
|
<SfTextBox CssClass="e-outLine" Placeholder="Nick Name"
|
|
FloatLabelType="@FloatLabelType.Auto" @bind-Value="contact.NickName" />
|
|
<ValidationMessage For="(() => contact.NickName)" />
|
|
<SfTextBox CssClass="e-outLine" Placeholder="Place"
|
|
FloatLabelType="@FloatLabelType.Auto" @bind-Value="contact.Place" />
|
|
<SfDatePicker TValue="DateTime?" Placeholder="Date of Birth"
|
|
FloatLabelType="@FloatLabelType.Auto" @bind-Value="contact.DateOfBirth"
|
|
Format="yyyy-MM-dd"/>
|
|
<hr />
|
|
<SfButton CssClass="e-primary" IconCss="e-icons e-save" Content="Save" type="submit" />
|
|
</EditForm>
|
|
</div>
|
|
<p>
|
|
<SfMessage Severity="MessageSeverity.Success" Visible="showSuccessMessage">
|
|
Contact saved successfully.
|
|
</SfMessage>
|
|
<SfMessage Severity="MessageSeverity.Error" Visible="showErrorMessage">
|
|
Oops, something went wrong.
|
|
</SfMessage>
|
|
</p>
|
|
@code {
|
|
[Parameter]
|
|
public int? Id { get; set; }
|
|
|
|
public Contact contact { get; set; } = new Contact();
|
|
|
|
bool showSuccessMessage = false;
|
|
bool showErrorMessage = false;
|
|
|
|
protected override async Task OnInitializedAsync()
|
|
{
|
|
if(Id is not null)
|
|
{
|
|
var result = await Http.GetAsync($"api/contacts/{Id}");
|
|
if (result.IsSuccessStatusCode)
|
|
{
|
|
var jsonResult = await result.Content.ReadFromJsonAsync<Contact>();
|
|
if (jsonResult is not null)
|
|
contact = jsonResult;
|
|
}
|
|
else
|
|
{
|
|
NavigationManager.NavigateTo("/contacts/new");
|
|
}
|
|
}
|
|
}
|
|
|
|
async Task HandleSubmit()
|
|
{
|
|
HttpResponseMessage result;
|
|
if (Id is not null)
|
|
result = await Http.PutAsJsonAsync($"api/contacts/{contact.Id}", contact);
|
|
else
|
|
result = await Http.PostAsJsonAsync($"api/contacts/", contact);
|
|
|
|
if (result.IsSuccessStatusCode)
|
|
{
|
|
var jsonResult = await result.Content.ReadFromJsonAsync<Contact>();
|
|
if(jsonResult is not null)
|
|
{
|
|
contact = jsonResult;
|
|
showSuccessMessage = true;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
showErrorMessage = true;
|
|
}
|
|
}
|
|
}
|