Refactor ReportViewer for reusability and dynamic updates

Refactored `ReportViewer.razor` to improve maintainability and
ensure dynamic updates to the `DxReportViewer` component.

- Added `@key` attribute bound to `ViewerKey` to trigger re-renders.
- Introduced `CreateReportInstance` and `CreateSignedReportInstance`
  helper methods to encapsulate report creation logic.
- Updated `OnInitializedAsync`, `ClearSignatureAsync`, and
  `ApplySignatureAsync` to use the new helper methods.
- Incremented `ViewerKey` in relevant methods to ensure proper
  re-rendering of the `DxReportViewer` component.
- Replaced `ApplySignatureToReport` with `CreateSignedReportInstance`.

These changes improve code modularity, readability, and ensure
the UI reflects the latest state of the report.
This commit is contained in:
2026-05-26 02:39:25 +02:00
parent 09cc639466
commit 367850fee5

View File

@@ -29,7 +29,7 @@
</div> </div>
@if(Report is not null) { @if(Report is not null) {
<DxReportViewer @ref="reportViewer" Report="Report" RootCssClasses="w-100 h-100" /> <DxReportViewer @key="ViewerKey" @ref="reportViewer" Report="Report" RootCssClasses="w-100 h-100" />
} }
@code { @code {
@@ -37,11 +37,10 @@
XtraReport? Report; XtraReport? Report;
bool SignatureApplied; bool SignatureApplied;
string? SignatureValidationMessage; string? SignatureValidationMessage;
int ViewerKey;
protected override async Task OnInitializedAsync() { protected override async Task OnInitializedAsync() {
Report = ReportStorage.TryGetReport("LargeDatasetReport", out var savedReport) Report = CreateReportInstance();
? savedReport
: PredefinedReports.ReportsFactory.GetReport("LargeDatasetReport");
await Task.CompletedTask; await Task.CompletedTask;
} }
@@ -55,12 +54,8 @@
await JSRuntime.InvokeVoidAsync("receiverSignature.clear", "receiver-signature-pad"); await JSRuntime.InvokeVoidAsync("receiverSignature.clear", "receiver-signature-pad");
SignatureApplied = false; SignatureApplied = false;
SignatureValidationMessage = null; SignatureValidationMessage = null;
Report = ReportStorage.TryGetReport("LargeDatasetReport", out var savedReport) Report = CreateReportInstance();
? savedReport ViewerKey++;
: PredefinedReports.ReportsFactory.GetReport("LargeDatasetReport");
if(reportViewer is not null)
await reportViewer.OpenReportAsync(Report);
} }
async Task ApplySignatureAsync() { async Task ApplySignatureAsync() {
@@ -73,11 +68,9 @@
} }
SignatureValidationMessage = null; SignatureValidationMessage = null;
ApplySignatureToReport(signatureDataUrl); Report = CreateSignedReportInstance(signatureDataUrl);
SignatureApplied = true; SignatureApplied = true;
ViewerKey++;
if(reportViewer is not null)
await reportViewer.OpenReportAsync(Report);
} }
async Task ExportSignedPdfAsync() { async Task ExportSignedPdfAsync() {
@@ -89,6 +82,18 @@
await reportViewer.ExportToAsync(ExportFormat.Pdf); await reportViewer.ExportToAsync(ExportFormat.Pdf);
} }
XtraReport CreateReportInstance() {
return ReportStorage.TryGetReport("LargeDatasetReport", out var savedReport)
? savedReport
: PredefinedReports.ReportsFactory.GetReport("LargeDatasetReport");
}
XtraReport CreateSignedReportInstance(string signatureDataUrl) {
var report = CreateReportInstance();
AddSignature(report, signatureDataUrl);
return report;
}
void ApplySignatureToReport(string signatureDataUrl) { void ApplySignatureToReport(string signatureDataUrl) {
Report ??= ReportStorage.TryGetReport("LargeDatasetReport", out var savedReport) Report ??= ReportStorage.TryGetReport("LargeDatasetReport", out var savedReport)
? savedReport ? savedReport