2025-10-07 01:03:58 +03:30
|
|
|
|
using FluentValidation;
|
2025-10-12 18:16:59 +03:30
|
|
|
|
using FrontOffice.BFF.User.Protobuf.Protos.User;
|
2025-10-13 09:23:10 +03:30
|
|
|
|
using FrontOffice.BFF.User.Protobuf.Validator;
|
|
|
|
|
|
using FrontOffice.BFF.UserAddress.Protobuf.Protos.UserAddress;
|
|
|
|
|
|
using FrontOffice.BFF.UserAddress.Protobuf.Validator;
|
2025-10-13 10:30:22 +03:30
|
|
|
|
using FrontOffice.Main.Pages.Profile.Components;
|
2025-10-13 08:42:31 +03:30
|
|
|
|
using FrontOffice.Main.Utilities;
|
2025-10-12 23:00:59 +03:30
|
|
|
|
using Mapster;
|
2025-10-07 01:03:58 +03:30
|
|
|
|
using Microsoft.AspNetCore.Components;
|
2025-10-12 23:21:30 +03:30
|
|
|
|
using Microsoft.JSInterop;
|
2025-10-07 01:03:58 +03:30
|
|
|
|
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!;
|
2025-10-13 09:23:10 +03:30
|
|
|
|
[Inject] private UserAddressContract.UserAddressContractClient UserAddressContract { get; set; } = default!;
|
2025-10-12 18:16:59 +03:30
|
|
|
|
|
|
|
|
|
|
private GetUserResponse _userProfile = new();
|
2025-10-12 23:00:59 +03:30
|
|
|
|
private UpdateUserRequest _updateUserRequest = new();
|
2025-10-13 09:23:10 +03:30
|
|
|
|
private readonly UpdateUserRequestValidator _personalValidator = new();
|
2025-10-07 01:03:58 +03:30
|
|
|
|
|
|
|
|
|
|
private MudForm? _personalForm;
|
|
|
|
|
|
|
|
|
|
|
|
private bool _isPersonalSaving;
|
|
|
|
|
|
private bool _isSettingsSaving;
|
|
|
|
|
|
|
2025-10-12 23:21:30 +03:30
|
|
|
|
private string _copyMessage = string.Empty;
|
|
|
|
|
|
|
2025-10-13 08:42:31 +03:30
|
|
|
|
private DateTime? _date;
|
2025-10-13 09:23:10 +03:30
|
|
|
|
|
|
|
|
|
|
// Address management
|
|
|
|
|
|
private List<GetAllUserAddressByFilterResponseModel> _addresses = new();
|
|
|
|
|
|
private bool _isLoadingAddresses;
|
|
|
|
|
|
private CreateNewUserAddressRequest _newAddressRequest = new();
|
|
|
|
|
|
private UpdateUserAddressRequest _editAddressRequest = new();
|
|
|
|
|
|
private MudForm? _addressForm;
|
|
|
|
|
|
private readonly CreateNewUserAddressRequestValidator _addressValidator = 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);
|
2025-10-13 08:42:31 +03:30
|
|
|
|
if (firstRender)
|
2025-10-12 18:16:59 +03:30
|
|
|
|
{
|
|
|
|
|
|
await LoadUserProfile();
|
2025-10-13 09:23:10 +03:30
|
|
|
|
await LoadAddresses();
|
2025-10-12 18:16:59 +03:30
|
|
|
|
}
|
2025-10-07 01:03:58 +03:30
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private async Task LoadUserProfile()
|
|
|
|
|
|
{
|
2025-10-12 23:21:30 +03:30
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
_userProfile = await UserContract.GetUserAsync(request: new());
|
|
|
|
|
|
_updateUserRequest = _userProfile.Adapt<UpdateUserRequest>();
|
2025-10-13 08:42:31 +03:30
|
|
|
|
if (_userProfile.BirthDate != null)
|
|
|
|
|
|
_date = _userProfile.BirthDate.ToDateTime();
|
2025-10-12 23:21:30 +03:30
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
// Handle the case when user is not authenticated or API fails
|
|
|
|
|
|
_userProfile = new GetUserResponse();
|
|
|
|
|
|
}
|
2025-10-12 18:16:59 +03:30
|
|
|
|
StateHasChanged();
|
2025-10-07 01:03:58 +03:30
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private async Task SavePersonalInfo()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (_personalForm is null) return;
|
|
|
|
|
|
|
|
|
|
|
|
await _personalForm.Validate();
|
|
|
|
|
|
if (!_personalForm.IsValid) return;
|
|
|
|
|
|
|
|
|
|
|
|
_isPersonalSaving = true;
|
|
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
2025-10-13 08:42:31 +03:30
|
|
|
|
if (_date != null)
|
|
|
|
|
|
_updateUserRequest.BirthDate = _date.Value.DateTimeToTimestamp();
|
|
|
|
|
|
|
2025-10-12 23:00:59 +03:30
|
|
|
|
await UserContract.UpdateUserAsync(request: _updateUserRequest);
|
|
|
|
|
|
await LoadUserProfile();
|
2025-10-07 01:03:58 +03:30
|
|
|
|
Snackbar.Add("اطلاعات شخصی با موفقیت ذخیره شد.", Severity.Success);
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
Snackbar.Add($"خطا در ذخیره اطلاعات: {ex.Message}", Severity.Error);
|
|
|
|
|
|
}
|
|
|
|
|
|
finally
|
|
|
|
|
|
{
|
|
|
|
|
|
_isPersonalSaving = false;
|
|
|
|
|
|
await InvokeAsync(StateHasChanged);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private async Task SaveSettings()
|
|
|
|
|
|
{
|
|
|
|
|
|
_isSettingsSaving = true;
|
|
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
2025-10-13 08:42:31 +03:30
|
|
|
|
await SavePersonalInfo();
|
2025-10-07 01:03:58 +03:30
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
Snackbar.Add($"خطا در ذخیره تنظیمات: {ex.Message}", Severity.Error);
|
|
|
|
|
|
}
|
|
|
|
|
|
finally
|
|
|
|
|
|
{
|
|
|
|
|
|
_isSettingsSaving = false;
|
|
|
|
|
|
await InvokeAsync(StateHasChanged);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-12 23:21:30 +03:30
|
|
|
|
private async Task CopyReferralCode()
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
await JSRuntime.InvokeVoidAsync("navigator.clipboard.writeText", $"{Navigation.BaseUri}?ref={_userProfile.ReferralCode}");
|
|
|
|
|
|
_copyMessage = "کد دعوت کپی شد!";
|
|
|
|
|
|
Snackbar.Add("کد دعوت در کلیپبورد کپی شد.", Severity.Success);
|
|
|
|
|
|
|
|
|
|
|
|
// Clear message after 3 seconds
|
|
|
|
|
|
await Task.Delay(3000);
|
|
|
|
|
|
_copyMessage = string.Empty;
|
|
|
|
|
|
await InvokeAsync(StateHasChanged);
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
Snackbar.Add("خطا در کپی کردن کد دعوت.", Severity.Error);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private async Task ShareReferralCode()
|
|
|
|
|
|
{
|
2025-10-20 15:20:29 +03:30
|
|
|
|
var shareText = $"کد دعوت من در فرصت: {_userProfile.ReferralCode}\nبرای عضویت از این لینک استفاده کنید:";
|
2025-10-12 23:21:30 +03:30
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
// Try to use Web Share API if available
|
|
|
|
|
|
await JSRuntime.InvokeVoidAsync("navigator.share",
|
|
|
|
|
|
new
|
|
|
|
|
|
{
|
|
|
|
|
|
title = "کد دعوت فرصت",
|
|
|
|
|
|
text = shareText,
|
|
|
|
|
|
url = $"{Navigation.BaseUri}?ref={_userProfile.ReferralCode}"
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
catch
|
|
|
|
|
|
{
|
|
|
|
|
|
// Fallback: copy to clipboard
|
|
|
|
|
|
await JSRuntime.InvokeVoidAsync("navigator.clipboard.writeText", $"{Navigation.BaseUri}?ref={_userProfile.ReferralCode}");
|
|
|
|
|
|
Snackbar.Add("لینک دعوت در کلیپبورد کپی شد.", Severity.Success);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-13 09:23:10 +03:30
|
|
|
|
// Address management methods
|
|
|
|
|
|
private async Task LoadAddresses()
|
2025-10-07 01:03:58 +03:30
|
|
|
|
{
|
2025-10-13 09:23:10 +03:30
|
|
|
|
_isLoadingAddresses = true;
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
var response = await UserAddressContract.GetAllUserAddressByFilterAsync(request: new());
|
|
|
|
|
|
if (response?.Models?.Any() == true)
|
|
|
|
|
|
{
|
|
|
|
|
|
_addresses = response.Models.ToList();
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
_addresses = new List<GetAllUserAddressByFilterResponseModel>();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
Snackbar.Add($"خطا در بارگذاری آدرسها: {ex.Message}", Severity.Error);
|
|
|
|
|
|
_addresses = new List<GetAllUserAddressByFilterResponseModel>();
|
|
|
|
|
|
}
|
|
|
|
|
|
finally
|
|
|
|
|
|
{
|
|
|
|
|
|
_isLoadingAddresses = false;
|
|
|
|
|
|
await InvokeAsync(StateHasChanged);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-10-07 01:03:58 +03:30
|
|
|
|
|
2025-10-13 09:23:10 +03:30
|
|
|
|
private async Task OpenAddAddressDialog()
|
|
|
|
|
|
{
|
2025-10-13 10:30:22 +03:30
|
|
|
|
var dialog = await DialogService.ShowAsync<AddAddressDialog>("افزودن آدرس جدید");
|
2025-10-13 09:23:10 +03:30
|
|
|
|
var result = await dialog.Result;
|
2025-10-07 01:03:58 +03:30
|
|
|
|
|
2025-10-13 09:23:10 +03:30
|
|
|
|
if (!result.Canceled)
|
|
|
|
|
|
{
|
|
|
|
|
|
await LoadAddresses();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-10-07 01:03:58 +03:30
|
|
|
|
|
2025-10-13 09:23:10 +03:30
|
|
|
|
private async Task OpenEditAddressDialog(GetAllUserAddressByFilterResponseModel address)
|
|
|
|
|
|
{
|
2025-10-13 10:30:22 +03:30
|
|
|
|
var dialog = await DialogService.ShowAsync<EditAddressDialog>("ویرایش آدرس", new DialogParameters<EditAddressDialog>
|
2025-10-13 09:23:10 +03:30
|
|
|
|
{
|
2025-10-13 10:30:22 +03:30
|
|
|
|
{ x => x.Model, address }
|
2025-10-13 09:23:10 +03:30
|
|
|
|
});
|
|
|
|
|
|
var result = await dialog.Result;
|
2025-10-07 01:03:58 +03:30
|
|
|
|
|
2025-10-13 09:23:10 +03:30
|
|
|
|
if (!result.Canceled)
|
|
|
|
|
|
{
|
|
|
|
|
|
await LoadAddresses();
|
|
|
|
|
|
}
|
2025-10-07 01:03:58 +03:30
|
|
|
|
}
|
2025-10-13 09:23:10 +03:30
|
|
|
|
|
|
|
|
|
|
private async Task SetAsDefaultAddress(long addressId)
|
2025-10-07 01:03:58 +03:30
|
|
|
|
{
|
2025-10-13 09:23:10 +03:30
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
await UserAddressContract.SetAddressAsDefaultAsync(request: new()
|
|
|
|
|
|
{
|
|
|
|
|
|
Id = addressId
|
|
|
|
|
|
});
|
|
|
|
|
|
Snackbar.Add("آدرس پیشفرض با موفقیت تغییر کرد.", Severity.Success);
|
|
|
|
|
|
await LoadAddresses();
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
Snackbar.Add($"خطا در تغییر آدرس پیشفرض: {ex.Message}", Severity.Error);
|
|
|
|
|
|
}
|
2025-10-07 01:03:58 +03:30
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-13 09:23:10 +03:30
|
|
|
|
private async Task DeleteAddress(long addressId)
|
2025-10-07 01:03:58 +03:30
|
|
|
|
{
|
2025-10-13 09:23:10 +03:30
|
|
|
|
var result = await DialogService.ShowMessageBox(
|
|
|
|
|
|
"تأیید حذف",
|
|
|
|
|
|
"آیا از حذف این آدرس اطمینان دارید؟",
|
|
|
|
|
|
yesText: "حذف",
|
|
|
|
|
|
cancelText: "لغو");
|
|
|
|
|
|
|
|
|
|
|
|
if (result == true)
|
2025-10-07 01:03:58 +03:30
|
|
|
|
{
|
2025-10-13 09:23:10 +03:30
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
await UserAddressContract.DeleteUserAddressAsync(request: new()
|
|
|
|
|
|
{
|
|
|
|
|
|
Id = addressId
|
|
|
|
|
|
});
|
|
|
|
|
|
Snackbar.Add("آدرس با موفقیت حذف شد.", Severity.Success);
|
|
|
|
|
|
await LoadAddresses();
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
Snackbar.Add($"خطا در حذف آدرس: {ex.Message}", Severity.Error);
|
|
|
|
|
|
}
|
2025-10-07 01:03:58 +03:30
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|