Files
FrontOffice/src/FrontOffice.Main/Pages/Profile/Index.razor.cs

233 lines
7.6 KiB
C#
Raw Normal View History

2025-10-07 01:03:58 +03:30
using FluentValidation;
2025-10-12 18:16:59 +03:30
using FrontOffice.BFF.Package.Protobuf.Protos.Package;
using FrontOffice.BFF.User.Protobuf.Protos.User;
2025-10-07 01:03:58 +03:30
using FrontOffice.Main.Utilities;
using Microsoft.AspNetCore.Components;
using MudBlazor;
using System.ComponentModel.DataAnnotations;
using Severity = MudBlazor.Severity;
namespace FrontOffice.Main.Pages.Profile;
public partial class Index
{
2025-10-12 18:16:59 +03:30
[Inject] private UserContract.UserContractClient UserContract { get; set; } = default!;
private GetUserResponse _userProfile = new();
2025-10-07 01:03:58 +03:30
private PasswordChangeModel _passwordModel = new();
private AccountSettings _settings = new();
private MudForm? _personalForm;
private MudForm? _passwordForm;
private bool _isPersonalSaving;
private bool _isPasswordChanging;
private bool _isSettingsSaving;
private readonly UserProfileValidator _personalValidator = new();
private readonly PasswordChangeValidator _passwordValidator = new();
2025-10-12 18:16:59 +03:30
protected override async Task OnAfterRenderAsync(bool firstRender)
2025-10-07 01:03:58 +03:30
{
2025-10-12 18:16:59 +03:30
await base.OnAfterRenderAsync(firstRender);
if(firstRender)
{
await LoadUserProfile();
await LoadAccountSettings();
}
2025-10-07 01:03:58 +03:30
}
private async Task LoadUserProfile()
{
2025-10-12 18:16:59 +03:30
_userProfile = await UserContract.GetUserAsync(request: new());
StateHasChanged();
2025-10-07 01:03:58 +03:30
}
private async Task LoadAccountSettings()
{
// TODO: Load settings from API
_settings = new AccountSettings
{
EmailNotifications = true,
SmsNotifications = true,
PushNotifications = false,
ProfileVisibility = true,
ShowOnlineStatus = true,
Language = "fa",
Theme = "light"
};
}
private async Task SavePersonalInfo()
{
if (_personalForm is null) return;
await _personalForm.Validate();
if (!_personalForm.IsValid) return;
_isPersonalSaving = true;
try
{
// TODO: Save to API
await Task.Delay(1000); // Simulate API call
Snackbar.Add("اطلاعات شخصی با موفقیت ذخیره شد.", Severity.Success);
}
catch (Exception ex)
{
Snackbar.Add($"خطا در ذخیره اطلاعات: {ex.Message}", Severity.Error);
}
finally
{
_isPersonalSaving = false;
await InvokeAsync(StateHasChanged);
}
}
private void CancelPersonalChanges()
{
// TODO: Reset form to original values
Snackbar.Add("تغییرات لغو شد.", Severity.Info);
}
private async Task ChangePassword()
{
if (_passwordForm is null) return;
await _passwordForm.Validate();
if (!_passwordForm.IsValid) return;
if (_passwordModel.NewPassword != _passwordModel.ConfirmPassword)
{
Snackbar.Add("رمز عبور جدید و تکرار آن مطابقت ندارند.", Severity.Warning);
return;
}
_isPasswordChanging = true;
try
{
// TODO: Change password via API
await Task.Delay(1000); // Simulate API call
Snackbar.Add("رمز عبور با موفقیت تغییر یافت.", Severity.Success);
_passwordModel = new PasswordChangeModel();
}
catch (Exception ex)
{
Snackbar.Add($"خطا در تغییر رمز عبور: {ex.Message}", Severity.Error);
}
finally
{
_isPasswordChanging = false;
await InvokeAsync(StateHasChanged);
}
}
private void CancelPasswordChange()
{
_passwordModel = new PasswordChangeModel();
Snackbar.Add("تغییر رمز عبور لغو شد.", Severity.Info);
}
private async Task SaveSettings()
{
_isSettingsSaving = true;
try
{
// TODO: Save settings to API
await Task.Delay(1000); // Simulate API call
Snackbar.Add("تنظیمات با موفقیت ذخیره شد.", Severity.Success);
}
catch (Exception ex)
{
Snackbar.Add($"خطا در ذخیره تنظیمات: {ex.Message}", Severity.Error);
}
finally
{
_isSettingsSaving = false;
await InvokeAsync(StateHasChanged);
}
}
private void CancelSettingsChanges()
{
// TODO: Reset settings to original values
Snackbar.Add("تغییرات تنظیمات لغو شد.", Severity.Info);
}
public class UserProfile
{
[Required(ErrorMessage = "نام الزامی است")]
public string? FirstName { get; set; }
[Required(ErrorMessage = "نام خانوادگی الزامی است")]
public string? LastName { get; set; }
[Required(ErrorMessage = "ایمیل الزامی است")]
[EmailAddress(ErrorMessage = "فرمت ایمیل صحیح نیست")]
public string? Email { get; set; }
[Required(ErrorMessage = "شماره موبایل الزامی است")]
public string? PhoneNumber { get; set; }
public string? NationalCode { get; set; }
public string? BirthDate { get; set; }
public string? Address { get; set; }
// Read-only fields
public string? JoinDate { get; set; }
public string? LastLogin { get; set; }
public int TotalReferrals { get; set; }
public string? Level { get; set; }
}
public class PasswordChangeModel
{
[Required(ErrorMessage = "رمز عبور فعلی الزامی است")]
public string? CurrentPassword { get; set; }
[Required(ErrorMessage = "رمز عبور جدید الزامی است")]
[MinLength(8, ErrorMessage = "رمز عبور باید حداقل ۸ کاراکتر باشد")]
public string? NewPassword { get; set; }
[Required(ErrorMessage = "تکرار رمز عبور الزامی است")]
[Compare(nameof(NewPassword), ErrorMessage = "رمز عبور و تکرار آن مطابقت ندارند")]
public string? ConfirmPassword { get; set; }
}
public class AccountSettings
{
public bool EmailNotifications { get; set; }
public bool SmsNotifications { get; set; }
public bool PushNotifications { get; set; }
public bool ProfileVisibility { get; set; }
public bool ShowOnlineStatus { get; set; }
public string? Language { get; set; }
public string? Theme { get; set; }
}
public class UserProfileValidator : AbstractValidator<UserProfile>
{
public UserProfileValidator()
{
RuleFor(x => x.FirstName).NotEmpty().WithMessage("نام الزامی است");
RuleFor(x => x.LastName).NotEmpty().WithMessage("نام خانوادگی الزامی است");
RuleFor(x => x.Email).NotEmpty().EmailAddress().WithMessage("ایمیل معتبر نیست");
RuleFor(x => x.PhoneNumber).NotEmpty().WithMessage("شماره موبایل الزامی است");
}
}
public class PasswordChangeValidator : AbstractValidator<PasswordChangeModel>
{
public PasswordChangeValidator()
{
RuleFor(x => x.CurrentPassword).NotEmpty().WithMessage("رمز عبور فعلی الزامی است");
RuleFor(x => x.NewPassword).NotEmpty().MinimumLength(8).WithMessage("رمز عبور جدید باید حداقل ۸ کاراکتر باشد");
RuleFor(x => x.ConfirmPassword).NotEmpty().WithMessage("تکرار رمز عبور الزامی است");
}
}
}