276 lines
11 KiB
Plaintext
276 lines
11 KiB
Plaintext
@page "/profile"
|
|
@attribute [Authorize]
|
|
@layout MainLayout
|
|
@inject HeimdallApiClient Api
|
|
@inject AuthState Auth
|
|
@inject ISnackbar Snackbar
|
|
@inject HeimdallAuthStateProvider AuthProvider
|
|
|
|
<MudText Typo="Typo.h4" Class="mb-6">
|
|
<MudIcon Icon="@Icons.Material.Filled.AccountCircle" Class="mr-2" />
|
|
Mon profil
|
|
</MudText>
|
|
|
|
@if (_loading)
|
|
{
|
|
<MudProgressCircular Indeterminate="true" />
|
|
}
|
|
else if (_error)
|
|
{
|
|
<MudAlert Severity="Severity.Error" Class="mb-4" Variant="Variant.Filled">
|
|
<MudText>Erreur lors du chargement du profil.</MudText>
|
|
<MudButton Variant="Variant.Text" Color="Color.Inherit" OnClick="@LoadProfile" Class="mt-2">
|
|
<MudIcon Icon="@Icons.Material.Filled.Refresh" Class="mr-1" Size="Size.Small" /> Réessayer
|
|
</MudButton>
|
|
</MudAlert>
|
|
}
|
|
else if (_profile is not null)
|
|
{
|
|
@* ─── Section 1: Informations personnelles ─── *@
|
|
<MudPaper Elevation="2" Class="pa-6 mb-6" Style="border-radius: 12px;">
|
|
<MudText Typo="Typo.h5" Class="mb-4">
|
|
<MudIcon Icon="@Icons.Material.Filled.Person" Class="mr-1" Size="Size.Small" />
|
|
Informations personnelles
|
|
</MudText>
|
|
|
|
@if (!string.IsNullOrEmpty(_profileError))
|
|
{
|
|
<MudAlert Severity="Severity.Error" Class="mb-4">@_profileError</MudAlert>
|
|
}
|
|
|
|
<form @onsubmit="SaveProfile">
|
|
<MudTextField @bind-Value="_displayName" Label="Nom d'affichage" Variant="Variant.Outlined"
|
|
Class="mb-4" Required="true" Immediate="true" />
|
|
|
|
<MudText Typo="Typo.body2" Color="Color.Secondary" Class="mb-1">Email</MudText>
|
|
<MudText Typo="Typo.body1" Class="mb-4">@_profile.Email</MudText>
|
|
|
|
<MudText Typo="Typo.body2" Class="mb-2">Thème</MudText>
|
|
<MudSelect @bind-Value="_theme" Label="Thème" Variant="Variant.Outlined" Class="mb-4">
|
|
<MudSelectItem Value="@("Auto")">Auto</MudSelectItem>
|
|
<MudSelectItem Value="@("Light")">Light</MudSelectItem>
|
|
<MudSelectItem Value="@("Dark")">Dark</MudSelectItem>
|
|
</MudSelect>
|
|
|
|
<MudText Typo="Typo.body2" Class="mb-2">Langue</MudText>
|
|
<MudSelect @bind-Value="_language" Label="Langue" Variant="Variant.Outlined" Class="mb-4">
|
|
<MudSelectItem Value="@("fr")">Français</MudSelectItem>
|
|
<MudSelectItem Value="@("en")">English</MudSelectItem>
|
|
</MudSelect>
|
|
|
|
<MudButton Variant="Variant.Filled" Color="Color.Primary" ButtonType="ButtonType.Submit"
|
|
Disabled="@_saving" FullWidth="true">
|
|
@if (_saving) { <MudProgressCircular Size="Size.Small" Class="mr-1" /> }
|
|
<MudIcon Icon="@Icons.Material.Filled.Save" Class="mr-1" /> Enregistrer
|
|
</MudButton>
|
|
</form>
|
|
</MudPaper>
|
|
|
|
@* ─── Section 2: Statistiques du compte ─── *@
|
|
<MudPaper Elevation="2" Class="pa-6 mb-6" Style="border-radius: 12px;">
|
|
<MudText Typo="Typo.h5" Class="mb-4">
|
|
<MudIcon Icon="@Icons.Material.Filled.BarChart" Class="mr-1" Size="Size.Small" />
|
|
Statistiques du compte
|
|
</MudText>
|
|
|
|
<MudGrid>
|
|
<MudItem xs="12" sm="6">
|
|
<MudText Typo="Typo.body2" Color="Color.Secondary">Date d'inscription</MudText>
|
|
<MudText Typo="Typo.body1" Class="mb-3">@_profile.CreatedAt.ToString("g")</MudText>
|
|
</MudItem>
|
|
<MudItem xs="12" sm="6">
|
|
<MudText Typo="Typo.body2" Color="Color.Secondary">Email confirmé</MudText>
|
|
<MudText Typo="Typo.body1" Class="mb-3">
|
|
<MudIcon Icon="@(_profile.EmailConfirmed ? Icons.Material.Filled.CheckCircle : Icons.Material.Filled.Cancel)"
|
|
Color="@(_profile.EmailConfirmed ? Color.Success : Color.Default)" Size="Size.Small" />
|
|
@(_profile.EmailConfirmed ? "Oui" : "Non")
|
|
</MudText>
|
|
</MudItem>
|
|
<MudItem xs="12" sm="6">
|
|
<MudText Typo="Typo.body2" Color="Color.Secondary">2FA</MudText>
|
|
<MudText Typo="Typo.body1" Class="mb-3">
|
|
<MudIcon Icon="@(_profile.TwoFactorEnabled ? Icons.Material.Filled.CheckCircle : Icons.Material.Filled.Cancel)"
|
|
Color="@(_profile.TwoFactorEnabled ? Color.Success : Color.Default)" Size="Size.Small" />
|
|
@(_profile.TwoFactorEnabled ? "Activé" : "Désactivé")
|
|
</MudText>
|
|
</MudItem>
|
|
<MudItem xs="12" sm="6">
|
|
<MudText Typo="Typo.body2" Color="Color.Secondary">Rôles</MudText>
|
|
<MudText Typo="Typo.body1" Class="mb-3">
|
|
@if (_profile.Roles is not null && _profile.Roles.Count > 0)
|
|
{
|
|
foreach (var r in _profile.Roles)
|
|
{
|
|
<MudText Typo="Typo.body2" Class="d-inline mr-1"
|
|
Color="@(r=="Admin"?Color.Tertiary:r=="Supervisor"?Color.Secondary:Color.Primary)">
|
|
@r
|
|
</MudText>
|
|
}
|
|
}
|
|
else
|
|
{
|
|
<MudText Typo="Typo.body2" Color="Color.Secondary">Aucun rôle</MudText>
|
|
}
|
|
</MudText>
|
|
</MudItem>
|
|
</MudGrid>
|
|
</MudPaper>
|
|
|
|
@* ─── Section 3: Changer le mot de passe ─── *@
|
|
<MudPaper Elevation="2" Class="pa-6 mb-6" Style="border-radius: 12px;">
|
|
<MudText Typo="Typo.h5" Class="mb-4">
|
|
<MudIcon Icon="@Icons.Material.Filled.Lock" Class="mr-1" Size="Size.Small" />
|
|
Changer le mot de passe
|
|
</MudText>
|
|
|
|
@if (!string.IsNullOrEmpty(_passwordError))
|
|
{
|
|
<MudAlert Severity="Severity.Error" Class="mb-4">@_passwordError</MudAlert>
|
|
}
|
|
|
|
<form @onsubmit="ChangePassword">
|
|
<MudTextField @bind-Value="_currentPassword" Label="Mot de passe actuel" Variant="Variant.Outlined"
|
|
InputType="InputType.Password" Class="mb-4" Required="true" Immediate="true"
|
|
Autocomplete="current-password" />
|
|
<MudTextField @bind-Value="_newPassword" Label="Nouveau mot de passe" Variant="Variant.Outlined"
|
|
InputType="InputType.Password" Class="mb-4" Required="true" Immediate="true"
|
|
HelperText="Minimum 12 caractères"
|
|
Autocomplete="new-password" />
|
|
<MudTextField @bind-Value="_confirmPassword" Label="Confirmer le mot de passe" Variant="Variant.Outlined"
|
|
InputType="InputType.Password" Class="mb-4" Required="true" Immediate="true"
|
|
Autocomplete="new-password" />
|
|
|
|
<MudButton Variant="Variant.Filled" Color="Color.Warning" ButtonType="ButtonType.Submit"
|
|
Disabled="@_changingPassword" FullWidth="true">
|
|
@if (_changingPassword) { <MudProgressCircular Size="Size.Small" Class="mr-1" /> }
|
|
<MudIcon Icon="@Icons.Material.Filled.LockReset" Class="mr-1" /> Changer le mot de passe
|
|
</MudButton>
|
|
</form>
|
|
</MudPaper>
|
|
}
|
|
|
|
@code {
|
|
private UserItem? _profile;
|
|
private bool _loading = true;
|
|
private bool _error;
|
|
private bool _saving;
|
|
private bool _changingPassword;
|
|
|
|
// Section 1 — Informations personnelles
|
|
private string _displayName = "";
|
|
private string _theme = "Auto";
|
|
private string _language = "fr";
|
|
private string _profileError = "";
|
|
|
|
// Section 3 — Changer le mot de passe
|
|
private string _currentPassword = "";
|
|
private string _newPassword = "";
|
|
private string _confirmPassword = "";
|
|
private string _passwordError = "";
|
|
|
|
protected override async Task OnInitializedAsync() => await LoadProfile();
|
|
|
|
private async Task LoadProfile()
|
|
{
|
|
_loading = true;
|
|
_error = false;
|
|
try
|
|
{
|
|
var result = await Api.GetMyProfileAsync();
|
|
if (result is not null)
|
|
{
|
|
_profile = result;
|
|
_displayName = result.DisplayName;
|
|
_theme = result.Theme ?? "Auto";
|
|
_language = result.Language;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_error = true;
|
|
Snackbar.Add($"Erreur : {ex.Message}", Severity.Error);
|
|
}
|
|
finally { _loading = false; }
|
|
}
|
|
|
|
// ─── Section 1: Enregistrer le profil ─────────────────────────
|
|
|
|
private async Task SaveProfile()
|
|
{
|
|
if (string.IsNullOrWhiteSpace(_displayName))
|
|
{
|
|
_profileError = "Le nom d'affichage est requis";
|
|
return;
|
|
}
|
|
|
|
_saving = true;
|
|
_profileError = "";
|
|
try
|
|
{
|
|
var success = await Api.UpdateMyProfileAsync(_displayName.Trim(), _theme, _language);
|
|
if (success)
|
|
{
|
|
Snackbar.Add("Profil mis à jour avec succès", Severity.Success);
|
|
}
|
|
else
|
|
{
|
|
_profileError = "Erreur lors de la mise à jour du profil";
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_profileError = $"Erreur : {ex.Message}";
|
|
}
|
|
finally { _saving = false; }
|
|
}
|
|
|
|
// ─── Section 3: Changer le mot de passe ───────────────────────
|
|
|
|
private async Task ChangePassword()
|
|
{
|
|
if (string.IsNullOrWhiteSpace(_currentPassword))
|
|
{
|
|
_passwordError = "Le mot de passe actuel est requis";
|
|
return;
|
|
}
|
|
if (string.IsNullOrWhiteSpace(_newPassword))
|
|
{
|
|
_passwordError = "Le nouveau mot de passe est requis";
|
|
return;
|
|
}
|
|
if (_newPassword.Length < 12)
|
|
{
|
|
_passwordError = "Le nouveau mot de passe doit contenir au moins 12 caractères";
|
|
return;
|
|
}
|
|
if (_newPassword != _confirmPassword)
|
|
{
|
|
_passwordError = "Les mots de passe ne correspondent pas";
|
|
return;
|
|
}
|
|
|
|
_changingPassword = true;
|
|
_passwordError = "";
|
|
try
|
|
{
|
|
var success = await Api.ChangePasswordAsync(_currentPassword, _newPassword);
|
|
if (success)
|
|
{
|
|
_currentPassword = "";
|
|
_newPassword = "";
|
|
_confirmPassword = "";
|
|
Snackbar.Add("Mot de passe changé avec succès", Severity.Success);
|
|
}
|
|
else
|
|
{
|
|
_passwordError = "Erreur lors du changement de mot de passe. Vérifiez votre mot de passe actuel.";
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_passwordError = $"Erreur : {ex.Message}";
|
|
}
|
|
finally { _changingPassword = false; }
|
|
}
|
|
}
|