159 lines
5.7 KiB
Plaintext
159 lines
5.7 KiB
Plaintext
@page "/userform/edit/{Id}"
|
|
|
|
@using System.Net.Http.Headers;
|
|
@using System.Text.Json;
|
|
@using Microsoft.AspNetCore.Components.Forms
|
|
|
|
@inject HttpClient _httpClient
|
|
@inject NavigationManager Navigation
|
|
@inject IToastService toastService
|
|
@inject AuthenticationStateProvider authenticationStateProvider
|
|
|
|
<h1>Editar Usuario</h1>
|
|
@if (user is not null)
|
|
{
|
|
<EditForm Model="@user" OnSubmit="UpdateUser">
|
|
<div class="form-group">
|
|
<label for="Name">Id del Rol</label>
|
|
<div class="col-md-4">
|
|
<InputText disabled="1" @bind-Value="user.Id" id="Id" class="form-control" />
|
|
</div>
|
|
</div>
|
|
<div class="form-group">
|
|
<label for="Username">Nombre de usuario:</label>
|
|
<div class="col-md-4">
|
|
<InputText id="Username" @bind-Value="user.UserName" class="form-control" />
|
|
</div>
|
|
</div>
|
|
<div class="form-group">
|
|
<label for="Username">Correo electrónico:</label>
|
|
<div class="col-md-4">
|
|
<InputText id="Email" @bind-Value="user.Email" class="form-control" />
|
|
</div>
|
|
</div>
|
|
<div class="form-group">
|
|
<label for="TwoFactorEnabled">Autenticación de dos factores:</label>
|
|
<div class="col-md-4">
|
|
<InputCheckbox id="TwoFactorEnabled" @bind-Value="user.TwoFactorEnabled"/>
|
|
</div>
|
|
</div>
|
|
<div class="form-group">
|
|
<label for="LockoutEnabled">Bloqueo de cuenta:</label>
|
|
<div class="col-md-4">
|
|
<InputCheckbox id="LockoutEnabled" @bind-Value="user.LockoutEnabled"/>
|
|
</div>
|
|
</div>
|
|
<div class="form-group">
|
|
<button type="submit" class="btn btn-primary">Guardar</button>
|
|
<button type="button" class="btn btn-secondary" @onclick="Cancel">Cancelar</button>
|
|
</div>
|
|
</EditForm>
|
|
}
|
|
|
|
@code {
|
|
[Parameter]
|
|
public string id { get; set; } = string.Empty;
|
|
private UserUpdate? user;
|
|
|
|
protected override async Task OnInitializedAsync()
|
|
{
|
|
|
|
if (id is not null)
|
|
{
|
|
user = await GetUser(id);
|
|
}
|
|
}
|
|
public async Task<UserUpdate?> GetUser(string userId)
|
|
{
|
|
var customAuthStateProvider = (CustomAuthorizationProvider)authenticationStateProvider;
|
|
var token = await customAuthStateProvider.GetTokenData();
|
|
if (!string.IsNullOrWhiteSpace(token.token))
|
|
{
|
|
_httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token.token);
|
|
try
|
|
{
|
|
var response = await _httpClient.GetAsync("/api/Account/GetUserById/" + userId);
|
|
|
|
if (response.IsSuccessStatusCode)
|
|
{
|
|
var jsonResponse = await response.Content.ReadAsStringAsync();
|
|
var options = new JsonSerializerOptions
|
|
{
|
|
PropertyNameCaseInsensitive = true
|
|
};
|
|
var deserializedUser = JsonSerializer.Deserialize<User>(jsonResponse, options);
|
|
User user = deserializedUser ?? new User();
|
|
|
|
// user = JsonSerializer.Deserialize<User>(jsonResponse, options);
|
|
UserUpdate require = new UserUpdate();
|
|
require.Id = user.Id;
|
|
require.UserName = user.UserName;
|
|
require.Email = user.Email;
|
|
require.LockoutEnabled = user.LockoutEnabled;
|
|
require.TwoFactorEnabled = user.TwoFactorEnabled;
|
|
return require;
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
return null;
|
|
}
|
|
};
|
|
return null;
|
|
}
|
|
|
|
private async Task UpdateUser()
|
|
{
|
|
try
|
|
{
|
|
var userJson = JsonSerializer.Serialize(user);
|
|
var content = new StringContent(userJson, System.Text.Encoding.UTF8, "application/json");
|
|
var response = await _httpClient.PutAsync("/api/Account/UpdateUser/" + id, content);
|
|
if (response.IsSuccessStatusCode)
|
|
{
|
|
toastService.ShowSuccess("Usuario actualizado exitosamente");
|
|
Navigation.NavigateTo("/users");
|
|
}
|
|
else
|
|
{
|
|
var errorMessage = await response.Content.ReadAsStringAsync();
|
|
toastService.ShowError(errorMessage);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
toastService.ShowError(ex.Message);
|
|
}
|
|
}
|
|
public void Cancel()
|
|
{
|
|
Navigation.NavigateTo("/users");
|
|
}
|
|
public class User
|
|
{
|
|
public string Id { get; set; } = string.Empty;
|
|
public string UserName { get; set; } = string.Empty;
|
|
public string NormalizedUserName { get; set; } = string.Empty;
|
|
public string Email { get; set; } = string.Empty;
|
|
public string NormalizedEmail { get; set; } = string.Empty;
|
|
public bool EmailConfirmed { get; set; }
|
|
public string PasswordHash { get; set; } = string.Empty;
|
|
public string SecurityStamp { get; set; } = string.Empty;
|
|
public string? PhoneNumber { get; set; }
|
|
public bool PhoneNumberConfirmed { get; set; }
|
|
public bool TwoFactorEnabled { get; set; }
|
|
public DateTimeOffset? LockoutEnd { get; set; }
|
|
public bool LockoutEnabled { get; set; }
|
|
public int AccessFailedCount { get; set; }
|
|
}
|
|
public class UserUpdate
|
|
{
|
|
public string Id { get; set; } = string.Empty;
|
|
public string UserName { get; set; } = string.Empty;
|
|
public string Email { get; set; } = string.Empty;
|
|
public bool TwoFactorEnabled { get; set; }
|
|
public bool LockoutEnabled { get; set; }
|
|
}
|
|
|
|
}
|