Update MudBlazor integration, improve captcha handling, and upgrade project dependencies

This commit is contained in:
masoodafar-web
2025-11-14 09:32:19 +03:30
parent cce59612fa
commit 07ea8f0f47
15 changed files with 456 additions and 395 deletions
@@ -1,4 +1,8 @@
using System.ComponentModel.DataAnnotations;
using FrontOffice.BFF.User.Protobuf.Protos.User;
using FrontOffice.Main.Shared;
using FrontOffice.Main.Utilities;
using Google.Protobuf.WellKnownTypes;
using Microsoft.AspNetCore.Components;
using MudBlazor;
@@ -6,14 +10,16 @@ namespace FrontOffice.Main.Pages;
public partial class RegisterWizard
{
[Inject] private UserContract.UserContractClient UserContract { get; set; } = default!;
private readonly RegistrationModel _model = new();
private MudForm? _stepOneForm;
private MudForm? _stepTwoForm;
private MudForm? _stepThreeForm;
private int _activeStep;
private bool _isSubmitting;
private bool _completed;
private string _captchaCode = string.Empty;
private AuthDialog _authDialog;
private UpdateUserRequest _updateUserRequest = new();
private string _nextButtonText => _activeStep switch
{
@@ -25,25 +31,18 @@ public partial class RegisterWizard
protected override void OnInitialized()
{
base.OnInitialized();
GenerateCaptcha();
}
private void GenerateCaptcha()
{
var random = Guid.NewGuid().ToString("N")[..6].ToUpperInvariant();
_captchaCode = random;
_model.CaptchaInput = string.Empty;
}
private void GoBack()
private async Task GoBack(MudStepper mudStepper)
{
if (_activeStep == 0 || _isSubmitting)
return;
_activeStep--;
await mudStepper.PreviousStepAsync();
}
private async Task GoNextAsync()
private async Task GoNextAsync(MudStepper mudStepper)
{
if (_isSubmitting)
return;
@@ -51,18 +50,31 @@ public partial class RegisterWizard
switch (_activeStep)
{
case 0:
if (!await ValidateStepAsync(_stepOneForm))
return;
if (!string.Equals(_model.CaptchaInput?.Trim(), _captchaCode, StringComparison.OrdinalIgnoreCase))
if (_authDialog._currentStep == AuthDialog.AuthStep.Phone)
{
Snackbar.Add("کد کپچا صحیح نیست.", Severity.Warning);
await _authDialog.SendOtpAsync();
return;
}
_activeStep = 1;
else
{
var verifyOtp = await _authDialog.VerifyOtpAsync();
if (!verifyOtp)
return;
_activeStep = 1;
}
break;
case 1:
if (!await ValidateStepAsync(_stepTwoForm))
return;
var saveResult = await SavePersonalInfo();
if (!saveResult)
return;
_activeStep = 2;
break;
case 2:
@@ -71,6 +83,8 @@ public partial class RegisterWizard
await SubmitAsync();
break;
}
await mudStepper.NextStepAsync();
}
private async Task<bool> ValidateStepAsync(MudForm? form)
@@ -102,15 +116,49 @@ public partial class RegisterWizard
Navigation.NavigateTo("docs/sample-contract.txt", true);
}
private void OnPhoneVerified()
{
// Move to next step after phone verification success
// _activeStep = 1;
// StateHasChanged();
}
private async Task<bool> SavePersonalInfo()
{
if (_stepTwoForm is null) return false;
await _stepTwoForm.Validate();
if (!_stepTwoForm.IsValid) return false;
try
{
// _updateUserRequest.AvatarPath="test";
// _updateUserRequest.BirthDate=Timestamp.FromDateTime(DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Utc));
// _updateUserRequest.EmailNotifications = true;
// _updateUserRequest.PushNotifications = true;
// _updateUserRequest.SmsNotifications = true;
//
_updateUserRequest.FirstName = _model.FirstName;
_updateUserRequest.LastName = _model.LastName;
_updateUserRequest.NationalCode = _model.NationalCode.PersianToEnglish();
await UserContract.UpdateUserAsync(request: _updateUserRequest);
Snackbar.Add("اطلاعات شخصی با موفقیت ذخیره شد.", Severity.Success);
return true;
}
catch (Exception ex)
{
Snackbar.Add($"خطا در ذخیره اطلاعات: {ex.Message}", Severity.Error);
return false;
}
finally
{
await InvokeAsync(StateHasChanged);
}
}
private sealed class RegistrationModel
{
[Required(ErrorMessage = "شماره موبایل الزامی است.")]
[RegularExpression(@"^09\d{9}$", ErrorMessage = "شماره موبایل معتبر نیست.")]
public string? MobileNumber { get; set; }
[Required(ErrorMessage = "کد کپچا را وارد کنید.")]
public string? CaptchaInput { get; set; }
[Required(ErrorMessage = "نام الزامی است.")]
[StringLength(50, ErrorMessage = "حداکثر ۵۰ کاراکتر")]
public string? FirstName { get; set; }
@@ -120,10 +168,10 @@ public partial class RegisterWizard
public string? LastName { get; set; }
[Required(ErrorMessage = "کد ملی الزامی است.")]
[RegularExpression(@"^\d{10}$", ErrorMessage = "کدملی باید ۱۰ رقم باشد.")]
[RegularExpression("^\\d{10}$", ErrorMessage = "کدملی باید ۱۰ رقم باشد.")]
public string? NationalCode { get; set; }
[Range(typeof(bool), "true", "true", ErrorMessage = "برای ادامه باید قوانین را تایید کنید.")]
public bool AcceptTerms { get; set; }
}
}
}