Files
Heimdall/src/Heimdall.Web/Components/Pages/Profile.razor
T
Arnaud Dartois 468b8e72aa feat(web): formulaires, validation & UX writing — messages métier (Fixes #39)
- UiErrors : messages métier centralisés (LoadFailed/SaveFailed/DeleteFailed/
  OperationFailed) — plus aucun ex.Message brut dans l'UI (48 remplacés sur
  19 pages), les détails techniques restent dans les logs.
- UiFormat : formatage centralisé des statuts (Online → En ligne, Offline →
  Hors ligne, Disabled → Désactivée…) et types d'événements ; appliqué aux
  pages Agents (liste + détail) qui affichaient encore les valeurs brutes.
- catch (Exception ex) devenus inutiles → catch simple (46 nettoyés).
- Boutons = verbes, validation métier déjà en place (Le nom est requis…)
  couverte par les tests existants.
- Tests : adaptés au message métier (Register) et au statut traduit
  (Agents). 182 tests Web verts.
2026-08-10 23:33:06 +02:00

386 lines
16 KiB
Plaintext

@page "/profile"
@attribute [Authorize]
@layout MainLayout
@inject HeimdallApiClient Api
@inject AuthState Auth
@inject ISnackbar Snackbar
@inject HeimdallAuthStateProvider AuthProvider
<MudText Typo="Typo.h1" Class="mb-6">
<i class="fas fa-user-circle mr-2"></i>
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>
@* ─── Section 4: Notifications ─── *@
<MudPaper Elevation="2" Class="pa-6 mb-6" Style="border-radius: 12px;">
<MudText Typo="Typo.h5" Class="mb-4">
<MudIcon Icon="@Icons.Material.Filled.Notifications" Class="mr-1" Size="Size.Small" />
Notifications
</MudText>
@if (!string.IsNullOrEmpty(_notifError))
{
<MudAlert Severity="Severity.Error" Class="mb-4">@_notifError</MudAlert>
}
<MudAlert Severity="Severity.Info" Class="mb-4" Variant="Variant.Outlined">
<MudText Typo="Typo.body2">
Les notifications push utilisent <strong>ntfy</strong> (serveur auto-hébergé).
Votre topic personnel est <code>heimdall_@_profile.Id</code>.
Installez l'app ntfy et abonnez-vous à ce topic pour recevoir les notifications.
</MudText>
</MudAlert>
<MudForm @ref="_notifForm">
<MudText Typo="Typo.subtitle2" Class="mb-2 mt-4">Canal Email</MudText>
<MudSwitch @bind-Value="_emailEnabled" Label="Notifications par email" Color="Color.Primary" Class="mb-2" />
<MudTextField @bind-Value="_emailAddress" Label="Adresse email de notification"
Variant="Variant.Outlined" Class="mb-4" Disabled="@(!_emailEnabled)"
Placeholder="notification@exemple.com" />
<MudText Typo="Typo.subtitle2" Class="mb-2 mt-4">Canal Push (ntfy)</MudText>
<MudSwitch @bind-Value="_ntfyEnabled" Label="Notifications push (ntfy)" Color="Color.Secondary" Class="mb-2" />
<MudTextField @bind-Value="_ntfyUrl" Label="URL du serveur ntfy (optionnel)"
Variant="Variant.Outlined" Class="mb-4" Disabled="@(!_ntfyEnabled)"
Placeholder="http://ntfy:80 (défaut)" HelperText="Laisser vide pour utiliser le serveur global" />
<MudText Typo="Typo.subtitle2" Class="mb-2 mt-4">Résumés</MudText>
<MudSwitch @bind-Value="_dailyDigest" Label="Résumé quotidien (8h)" Color="Color.Tertiary" Class="mb-2" />
<MudSwitch @bind-Value="_weeklyDigest" Label="Résumé hebdomadaire (lundi 8h)" Color="Color.Tertiary" Class="mb-4" />
<MudButton Variant="Variant.Filled" Color="Color.Primary" OnClick="@SaveNotificationConfig"
Disabled="@_savingNotif" FullWidth="true">
@if (_savingNotif) { <MudProgressCircular Size="Size.Small" Class="mr-1" /> }
<MudIcon Icon="@Icons.Material.Filled.Save" Class="mr-1" /> Enregistrer
</MudButton>
</MudForm>
</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();
await LoadNotificationConfig();
}
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 {
_error = true;
Snackbar.Add(UiErrors.OperationFailed(), 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 {
_profileError = UiErrors.OperationFailed();
}
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 {
_passwordError = UiErrors.OperationFailed();
}
finally { _changingPassword = false; }
}
// ─── Section 4: Notifications ─────────────────────────────────
private NotificationConfigItem? _notifConfig;
private MudForm? _notifForm;
private bool _emailEnabled;
private string? _emailAddress;
private bool _ntfyEnabled;
private string? _ntfyUrl;
private bool _dailyDigest;
private bool _weeklyDigest;
private bool _savingNotif;
private string _notifError = "";
private async Task LoadNotificationConfig()
{
try
{
_notifConfig = await Api.GetNotificationConfigAsync();
if (_notifConfig is not null)
{
_emailEnabled = _notifConfig.EmailEnabled;
_emailAddress = _notifConfig.EmailAddress;
_ntfyEnabled = _notifConfig.NtfyEnabled;
_ntfyUrl = _notifConfig.NtfyUrl;
_dailyDigest = _notifConfig.DailyDigest;
_weeklyDigest = _notifConfig.WeeklyDigest;
}
}
catch (Exception ex)
{
_notifError = $"Erreur chargement config notifications : {ex.Message}";
}
}
private async Task SaveNotificationConfig()
{
_savingNotif = true;
_notifError = "";
try
{
var request = new UpdateNotificationConfigRequest(
_emailEnabled,
string.IsNullOrWhiteSpace(_emailAddress) ? null : _emailAddress.Trim(),
_ntfyEnabled,
string.IsNullOrWhiteSpace(_ntfyUrl) ? null : _ntfyUrl.Trim(),
_dailyDigest,
_weeklyDigest,
null // SubscribedEvents — à implémenter dans une version future
);
var success = await Api.UpdateNotificationConfigAsync(request);
if (success)
{
Snackbar.Add("Configuration notifications mise à jour", Severity.Success);
}
else
{
_notifError = "Erreur lors de la mise à jour";
}
}
catch {
_notifError = UiErrors.OperationFailed();
}
finally { _savingNotif = false; }
}
}