Leandro Hernan Rojas 0da0d7c829
All checks were successful
CI/CD Pipeline / Build and Deploy with Docker Compose (push) Successful in 5m1s
CustomerForm A1
2025-04-10 00:08:01 -03:00

106 lines
3.2 KiB
Plaintext

@page "/sales/customerform"
@page "/sales/customerform/{CustomerId:int}"
@inject HttpClient _httpClient
@inject NavigationManager Navigation
@inject IToastService toastService
@inject AuthenticationStateProvider authenticationStateProvider
<EditForm Model="@customer" OnValidSubmit="@HandleValidSubmit">
<DataAnnotationsValidator />
<ValidationSummary />
<div class="row">
<div class="col-sm-6">
<div class="form-group">
<label for="Name">Razón Social:</label>
<InputText id="Name" @bind-Value="customer.Name" class="form-control" />
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<label for="AccountTypeId">Tipo de Cuenta:</label>
<InputSelect id="AccountTypeId" @bind-Value="customer.AccounttypesId" class="form-control">
@foreach (var type in accountTypes)
{
<option value="@type.Id">@type.Name</option>
}
</InputSelect>
</div>
</div>
</div>
<!-- Acá van documentos y direcciones (lo agregamos más adelante) -->
<div class="row mt-3">
<div class="col">
<button type="submit" class="btn btn-primary">Guardar</button>
<button type="button" class="btn btn-secondary" @onclick="Cancel">Volver</button>
</div>
</div>
</EditForm>
@code {
[Parameter]
public int? CustomerId { get; set; }
private ECustomer customer { get; set; } = new();
private List<EAccountType> accountTypes = new();
private string returnUrl = "/sales/customers";
protected override async Task OnInitializedAsync()
{
await LoadAccountTypes();
if (CustomerId.HasValue)
{
// Cargar datos del cliente existente desde la API
customer = await _httpClient.GetFromJsonAsync<ECustomer>($"/api/Customer/GetById/{CustomerId.Value}") ?? new();
}
}
private async Task LoadAccountTypes()
{
var result = await _httpClient.GetFromJsonAsync<List<EAccountType>>("/api/AccountType/GetAll");
accountTypes = result ?? new();
}
private async Task HandleValidSubmit()
{
try
{
HttpResponseMessage response;
if (CustomerId.HasValue)
{
response = await _httpClient.PutAsJsonAsync("/api/Customer/Update", customer);
}
else
{
response = await _httpClient.PostAsJsonAsync("/api/Customer/Create", customer);
}
if (response.IsSuccessStatusCode)
{
toastService.ShowSuccess("Cliente guardado exitosamente");
Navigation.NavigateTo(returnUrl);
}
else
{
var error = await response.Content.ReadAsStringAsync();
toastService.ShowError($"Error: {error}");
}
}
catch (Exception ex)
{
toastService.ShowError($"Error: {ex.Message}");
}
}
private void Cancel()
{
Navigation.NavigateTo(returnUrl);
}
}