Add device detection and PDF generation features; refactor AuthDialog and update print utilities

This commit is contained in:
masoodafar-web
2025-11-14 13:12:31 +03:30
parent e86cb7aa47
commit 230ba41113
12 changed files with 397 additions and 54 deletions
@@ -4,6 +4,7 @@ using FrontOffice.Main.Shared;
using FrontOffice.Main.Utilities;
using Google.Protobuf.WellKnownTypes;
using Microsoft.AspNetCore.Components;
using Microsoft.JSInterop;
using MudBlazor;
namespace FrontOffice.Main.Pages;
@@ -19,7 +20,12 @@ public partial class RegisterWizard
private bool _isSubmitting;
private bool _completed;
private AuthDialog _authDialog;
private MudStepper _mudStepper;
private UpdateUserRequest _updateUserRequest = new();
private bool _isAuthenticated;
[Inject] private AuthService AuthService { get; set; } = default!;
[Inject] private IJSRuntime Js { get; set; } = default!;
private string _nextButtonText => _activeStep switch
{
@@ -28,21 +34,56 @@ public partial class RegisterWizard
_ => _isSubmitting ? "در حال ارسال..." : "ثبت نهایی"
};
protected override void OnInitialized()
protected override async Task OnAfterRenderAsync(bool firstRender)
{
base.OnInitialized();
_isAuthenticated = await AuthService.IsAuthenticatedAsync();
if (_isAuthenticated && _activeStep == 0)
{
await _mudStepper.NextStepAsync();
_activeStep = 1;
await InvokeAsync(StateHasChanged);
}
else if (_isAuthenticated && _activeStep == 1)
{
try
{
var existUser = await UserContract.GetUserAsync(new Empty());
if (existUser != null && !string.IsNullOrEmpty(existUser.FirstName) && string.IsNullOrWhiteSpace(_model.FirstName))
{
_model.FirstName = existUser.FirstName;
}
if (existUser != null && !string.IsNullOrEmpty(existUser.LastName) && string.IsNullOrWhiteSpace(_model.LastName))
{
_model.LastName = existUser.LastName;
}
if (existUser != null && !string.IsNullOrEmpty(existUser.NationalCode) && string.IsNullOrWhiteSpace(_model.NationalCode))
{
_model.NationalCode = existUser.NationalCode;
}
}
catch (Exception e)
{
Console.WriteLine(e);
}
await InvokeAsync(StateHasChanged);
}
await base.OnAfterRenderAsync(firstRender);
}
private async Task GoBack(MudStepper mudStepper)
private async Task GoBack()
{
if (_activeStep == 0 || _isSubmitting)
if (_activeStep == 0 || _isSubmitting || (_isAuthenticated && _activeStep == 1))
return;
_activeStep--;
await mudStepper.PreviousStepAsync();
await _mudStepper.PreviousStepAsync();
}
private async Task GoNextAsync(MudStepper mudStepper)
private async Task GoNextAsync()
{
if (_isSubmitting)
return;
@@ -61,7 +102,6 @@ public partial class RegisterWizard
if (!verifyOtp)
return;
_activeStep = 1;
}
@@ -76,6 +116,7 @@ public partial class RegisterWizard
return;
_activeStep = 2;
await _mudStepper.NextStepAsync();
break;
case 2:
if (!await ValidateStepAsync(_stepThreeForm))
@@ -83,8 +124,6 @@ public partial class RegisterWizard
await SubmitAsync();
break;
}
await mudStepper.NextStepAsync();
}
private async Task<bool> ValidateStepAsync(MudForm? form)
@@ -111,16 +150,29 @@ public partial class RegisterWizard
}
}
private void DownloadContract()
private async void DownloadContract()
{
Navigation.NavigateTo("docs/sample-contract.txt", true);
// Example dynamic HTML (could be constructed based on user data)
var html = $"<h1 style='text-align:center'>قرارداد اولیه کاربر</h1><p>نام: {_model.FirstName ?? "-"} {_model.LastName ?? "-"}</p><p>کد ملی: {_model.NationalCode ?? "-"}</p><p>این یک نسخه نمونه است.</p>";
// Option A: trigger browser print dialog for PDF (client-side)
try
{
await Js.InvokeVoidAsync("printUtils.printHtml", html, new { title = "قرارداد نمونه" });
}
catch
{
// Fallback to server-side generation endpoint if JS fails
var encoded = Uri.EscapeDataString(html);
Navigation.NavigateTo($"contract/generate?html={encoded}&fileName=sample-contract", true);
}
}
private void OnPhoneVerified()
{
// Move to next step after phone verification success
// _activeStep = 1;
// StateHasChanged();
//StateHasChanged();
}
private async Task<bool> SavePersonalInfo()
@@ -145,7 +197,7 @@ public partial class RegisterWizard
Console.WriteLine(_updateUserRequest.FirstName);
Console.WriteLine(_updateUserRequest.LastName);
Console.WriteLine(_updateUserRequest.NationalCode);
await UserContract.UpdateUserAsync(request: _updateUserRequest);
Snackbar.Add("اطلاعات شخصی با موفقیت ذخیره شد.", Severity.Success);
return true;