Refactor report handling and improve async operations

Refactored the `Report` property to `_report` with nullable support
and updated `EnvelopeKey` to use `init` for immutability.
Made `CreateReport` asynchronous, returning `Task<XtraReport>`,
and removed redundant `BasePdfBytes` property. Simplified predefined
report fetching by removing `ReportStorage.TryGetReport`. Improved
error handling for missing or invalid `pdfBytes` in `CreateReport`.
Made minor formatting and structural improvements for clarity.
This commit is contained in:
2026-06-11 14:11:56 +02:00
parent c99511de29
commit 88b196ed6d

View File

@@ -12,47 +12,37 @@
<link href="_content/DevExpress.Blazor.Reporting.Viewer/css/dx-blazor-reporting-components.bs5.css" rel="stylesheet" /> <link href="_content/DevExpress.Blazor.Reporting.Viewer/css/dx-blazor-reporting-components.bs5.css" rel="stylesheet" />
@if (Report is not null) { @if (_report is not null) {
<DxReportViewer Report="Report" RootCssClasses="w-100 h-100" Zoom="1.3" /> <DxReportViewer Report="_report" RootCssClasses="w-100 h-100" Zoom="1.3" />
} }
@code { @code {
[Parameter] public string EnvelopeKey { get; set; } = string.Empty; [Parameter] public string EnvelopeKey { get; init; } = null!;
XtraReport Report { get; set; } = null!; XtraReport? _report = null;
byte[] BasePdfBytes { get; set; } = null!; protected override async Task OnInitializedAsync()
{
protected override async Task OnInitializedAsync() { _report = await CreateReport();
if (!AppOptions.Value.UsePredefinedReports) {
try {
var pdfBytes = await DocumentService.GetDocumentAsync(EnvelopeKey);
if (pdfBytes is { Length: > 0 })
BasePdfBytes = pdfBytes;
else
throw new InvalidOperationException($"No PDF bytes found for EnvelopeKey: {EnvelopeKey}");
} catch (HttpRequestException ex) {
throw new InvalidOperationException($"Failed to load document for EnvelopeKey: {EnvelopeKey}", ex);
}
}
Report = CreateReport();
} }
XtraReport CreateReport() async Task<XtraReport> CreateReport()
{ {
if (AppOptions.Value.UsePredefinedReports) if (AppOptions.Value.UsePredefinedReports)
{ {
return ReportStorage.TryGetReport("LargeDatasetReport", out var savedReport) return PredefinedReports.ReportsFactory.GetReport("LargeDatasetReport");
? savedReport
: PredefinedReports.ReportsFactory.GetReport("LargeDatasetReport");
} }
else else
{ {
var pdfBytes = await DocumentService.GetDocumentAsync(EnvelopeKey);
if (pdfBytes is null || pdfBytes.Length == 0)
throw new InvalidOperationException($"No PDF bytes found for EnvelopeKey: {EnvelopeKey}");
var report = new XtraReport(); var report = new XtraReport();
var detail = new DevExpress.XtraReports.UI.DetailBand(); var detail = new DevExpress.XtraReports.UI.DetailBand();
report.Bands.Add(detail); report.Bands.Add(detail);
detail.Controls.Add(new DevExpress.XtraReports.UI.XRPdfContent { Source = BasePdfBytes, GenerateOwnPages = true }); detail.Controls.Add(new DevExpress.XtraReports.UI.XRPdfContent { Source = pdfBytes, GenerateOwnPages = true });
return report; return report;
} }
} }