Skip to content

Commit

Permalink
Merge pull request #535 from Inxton/520-fix-an-issue-with-the-export
Browse files Browse the repository at this point in the history
520 fix an issue with the export
  • Loading branch information
PTKu authored Jan 22, 2025
2 parents 8930ba2 + 8a5d377 commit d1d033c
Show file tree
Hide file tree
Showing 13 changed files with 221 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<ProjectReference Include="..\..\..\..\data\src\AXOpen.Data.Blazor\axopen_data_blazor.csproj" />
<ProjectReference Include="..\..\..\..\data\src\repositories\Json\AXOpen.Data.Json.csproj" />
<ProjectReference Include="..\..\..\..\security\src\AXOpen.Security.Blazor\AXOpen.Security.Blazor.csproj" />
<ProjectReference Include="..\..\..\src\exporters\AXOpen.Data.Exporters.Excel\AXOpen.Data.Exporters.Excel.csproj" />
<ProjectReference Include="..\..\..\src\repositories\InMemory\AXOpen.Data.InMemory.csproj" />
<ProjectReference Include="..\..\..\src\repositories\MongoDb\AXOpen.Data.MongoDb.csproj" />
<ProjectReference Include="..\..\ix\axopen_data_app.csproj" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ else
{
<AuthorizeView Roles="@DataExchangeRoleNames.can_data_export">
<Authorized>
<button type="button" class="btn btn-primary ms-2" data-bs-toggle="modal" data-bs-target="#exportModal-@ViewGuid" @onclick='async () => { Vm.IsFileExported = false; await LoadCustomExportDataAsync(); await Vm.ExportDataAsync("wwwroot/Temp/" + ViewGuid + "/exportData.zip"); }'>@Localizer["Export"]</button>
<button type="button" class="btn btn-primary ms-2" data-bs-toggle="modal" data-bs-target="#exportModal-@ViewGuid" @onclick='async () => { await LoadCustomExportDataAsync() ;}'>@Localizer["Export"]</button>
</Authorized>
</AuthorizeView>

<AuthorizeView Roles="@DataExchangeRoleNames.can_data_import">
<Authorized>
<button type="button" class="btn btn-primary ms-2" data-bs-toggle="modal" data-bs-target="#importModal-@ViewGuid" @onclick="async () => { isFileImported = false; await LoadCustomExportDataAsync(); }">@Localizer["Import"]</button>
<button type="button" class="btn btn-primary ms-2" data-bs-toggle="modal" data-bs-target="#importModal-@ViewGuid" @onclick="async () => { await LoadCustomExportDataAsync(); }">@Localizer["Import"]</button>
</Authorized>
</AuthorizeView>
}
Expand Down Expand Up @@ -603,23 +603,51 @@ else
<div class="modal-content">
<div class="modal-header">
<h1 class="modal-title fs-5" id="exampleModalLabel">@Localizer["Export data"]</h1>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close" @onclick='() => ClearFiles("wwwroot/Temp/" + ViewGuid)'></button>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close" @onclick='() => ClearClientFiles()'></button>
</div>
<div class="modal-body">
@if (!Vm.IsFileExported)
@if (Vm.exportStatus == eOperationStatus.Busy)
{
<div class="spinner-border" role="status">
<span class="visually-hidden">Loading...</span>
</div>
}
else
@if (Vm.exportStatus == eOperationStatus.Done)
{
<a download href="/Temp/@ViewGuid/exportData.zip">@Localizer["Download"]</a>
<a download href="@ExportDownloadUrl">@Localizer["Download"]</a>
}

@if (Vm.exportStatus == eOperationStatus.Failed)
{
<span class="text-muted"> "Export failed"</span>
}

</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-toggle="modal" data-bs-target="#exportCustomModal-@ViewGuid">@Localizer["Customize"]</button>
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal" @onclick='() => ClearFiles("wwwroot/Temp/" + ViewGuid)'>@Localizer["Close"]</button>

@switch (Vm.ExportSet.ExportMode)
{
case eExportMode.First:
<span class="text-muted"> "The first @Vm.ExportSet.FirstNumber items will be exported to a @Vm.ExportSet.ExportFileType."</span>
break;
case eExportMode.Last:
<span class="text-muted">"The last @Vm.ExportSet.FirstNumber items will be exported to a @Vm.ExportSet.ExportFileType."</span>
break;
case eExportMode.Exact:
<span class="text-muted">"Items from @Vm.ExportSet.FirstNumber to @Vm.ExportSet.SecondNumber will be exported to a @Vm.ExportSet.ExportFileType."</span>
break;
}

<button type="button" class="btn btn-secondary" data-bs-toggle="modal" data-bs-target="#exportCustomModal-@ViewGuid">@Localizer["Customize export options"]</button>

<button type="button" class="btn btn-primary" @onclick='async () => {
ClearClientFiles();
await Vm.ExportDataAsync(ExportPath);
}'>
<strong>@Localizer["Export"]</strong>
</button>

<button type="button" class="btn btn-secondary" data-bs-dismiss="modal" @onclick='() => ClearClientFiles()'>@Localizer["Close"]</button>
</div>
</div>
</div>
Expand All @@ -630,12 +658,32 @@ else
<div class="modal-content">
<div class="modal-header">
<h1 class="modal-title fs-5" id="exampleModalLabel">@Localizer["Import data"]</h1>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close" @onclick="async () => await SaveCustomExportDataAsync()"></button>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close" @onclick="async () => {Vm.importStatus = eOperationStatus.Ready; await SaveCustomExportDataAsync();}"></button>
</div>
<div class="modal-body">
<div class="mb-3">

<label for="formFile" class="form-label">@Localizer["File input"]</label>

<InputFile @key=@(_inputFileId) class="form-control col-10" id="formFile" accept=".zip" OnChange="@LoadFile" />

@if (Vm.importStatus == eOperationStatus.Busy)
{
<div class="spinner-border" role="status">
<span class="visually-hidden">Loading...</span>
</div>
}

@if (Vm.importStatus == eOperationStatus.Done)
{
<span class="text-muted"> "Import successed"</span>
}

@if (Vm.importStatus == eOperationStatus.Failed)
{
<span class="text-muted"> "Import failed"</span>
}

</div>
</div>
<div class="modal-footer">
Expand All @@ -651,14 +699,16 @@ else
<input type="text" class="form-control" placeholder="Separator" aria-label="Separator" @bind="Vm.ExportSet.Separator">
</div>
</div>
@if (!isFileImported && isFileImporting)
{
<div class="spinner-border" role="status">
<span class="visually-hidden">Loading...</span>
</div>
}
<button type="button" class="btn btn-primary @(!isFileImported || isFileImporting ? "disabled" : "")" data-bs-dismiss="modal" @onclick='async () => { await Vm.ImportDataAsync("wwwroot/Temp/" + ViewGuid + "/importData.zip"); isFileImported = false; _inputFileId = Guid.NewGuid().ToString(); SaveCustomExportDataAsync(); }'>@Localizer["Import"]</button>
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal" @onclick="async () => await SaveCustomExportDataAsync()">@Localizer["Close"]</button>

<button type="button" class="btn btn-primary @(_fileLoadingStatus != eOperationStatus.Done ? "disabled" : "")"
@onclick='async () => {
Vm.importStatus = eOperationStatus.Busy;
await Vm.ImportDataAsync(ImportPath);
_inputFileId = Guid.NewGuid().ToString();
_fileLoadingStatus = eOperationStatus.Ready;}'>
@Localizer["Import"]
</button>
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal" @onclick="async () => {Vm.importStatus = eOperationStatus.Ready; await SaveCustomExportDataAsync();}">@Localizer["Close"]</button>
</div>
</div>
</div>
Expand All @@ -669,7 +719,10 @@ else
<div class="modal-content">
<div class="modal-header">
<h1 class="modal-title fs-5" id="exampleModalLabel">@Localizer["Export data"]</h1>
<button type="button" class="btn-close" data-bs-toggle="modal" data-bs-target="#exportModal-@ViewGuid" aria-label="Close" @onclick='async () => { Vm.IsFileExported = false; await Vm.ExportDataAsync("wwwroot/Temp/" + ViewGuid + "/exportData.zip"); SaveCustomExportDataAsync(); }'></button>
<button type="button" class="btn-close" data-bs-toggle="modal" data-bs-target="#exportModal-@ViewGuid" aria-label="Close"
@onclick='async () => {
await SaveCustomExportDataAsync(); }'>
</button>
</div>
<div class="modal-body">
<div class="accordion">
Expand Down Expand Up @@ -759,7 +812,11 @@ else
</div>
</div>
}
<button type="button" class="btn btn-primary ms-2" data-bs-toggle="modal" data-bs-target="#exportModal-@ViewGuid" @onclick='async () => { Vm.IsFileExported = false; await Vm.ExportDataAsync("wwwroot/Temp/" + ViewGuid + "/exportData.zip"); SaveCustomExportDataAsync(); }'>@Localizer["Back"]</button>
<button type="button" class="btn btn-primary ms-2" data-bs-toggle="modal" data-bs-target="#exportModal-@ViewGuid"
@onclick='async () => {
await SaveCustomExportDataAsync(); }'>
@Localizer["Back"]
</button>
</div>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,58 @@ public partial class DataExchangeView : ComponentBase, IDisposable
private ProtectedLocalStorage ProtectedLocalStore { get; set; }

private Guid ViewGuid { get; } = Guid.NewGuid();

private string _inputFileId = Guid.NewGuid().ToString();

private eOperationStatus _fileLoadingStatus = eOperationStatus.Ready;


private string _ClientFolder = string.Empty;

public string ClientFolder
{
get
{
if (string.IsNullOrEmpty(_ClientFolder)) _ClientFolder = "wwwroot/Temp/" + ViewGuid;
return _ClientFolder;
}
}

private string _ExportPath = string.Empty;

public string ExportPath
{
get
{
if (string.IsNullOrEmpty(_ExportPath)) _ExportPath = ClientFolder + "/exportData.zip";
return _ExportPath;
}
}

private string _ImportPath = string.Empty;

public string ImportPath
{
get
{
if (string.IsNullOrEmpty(_ImportPath)) _ImportPath = ClientFolder + "/importData.zip";
return _ImportPath;
}
}

private string _ExportDownloadUrl = string.Empty;

public string ExportDownloadUrl
{
get
{
if (string.IsNullOrEmpty(_ExportDownloadUrl)) _ExportDownloadUrl = "/Temp/" + ViewGuid + "/exportData.zip";
return _ExportDownloadUrl;
}
}

private string Create { get; set; } = "";

private bool isFileImported { get; set; } = false;
private bool isFileImporting { get; set; } = false;

private int MaxPage =>
(int)(Vm.FilteredCount % Vm.Limit == 0 ? Vm.FilteredCount / Vm.Limit - 1 : Vm.FilteredCount / Vm.Limit);
Expand Down Expand Up @@ -138,28 +186,29 @@ protected override async Task OnInitializedAsync()
Vm.StateHasChangedDelegate = StateHasChanged;
}

private string _inputFileId = Guid.NewGuid().ToString();

private async Task LoadFile(InputFileChangeEventArgs e)
{
isFileImported = false;
isFileImporting = true;
_fileLoadingStatus = eOperationStatus.Busy;

try
{
Directory.CreateDirectory("wwwroot/Temp/" + ViewGuid);
if (!Directory.Exists(ClientFolder))
Directory.CreateDirectory(ClientFolder);

Console.WriteLine( $"willl be imported to {ImportPath}");
await using FileStream fs = new(ImportPath, FileMode.Create);

await using FileStream fs = new("wwwroot/Temp/" + ViewGuid + "/importData.zip", FileMode.Create);
await e.File.OpenReadStream().CopyToAsync(fs);

isFileImported = true;
_fileLoadingStatus = eOperationStatus.Done;
}
catch (Exception ex)
{
_alertDialogService.AddAlertDialog(eAlertType.Danger, "Error!", ex.Message, 10);
_fileLoadingStatus = eOperationStatus.Failed;
}

isFileImporting = false;
}

private void ClearFiles(string path)
Expand All @@ -168,6 +217,14 @@ private void ClearFiles(string path)
Directory.Delete(path, true);
}

private void ClearClientFiles()
{
ClearFiles(ClientFolder);
Vm.exportStatus = eOperationStatus.Ready; // reset export status
Vm.importStatus = eOperationStatus.Ready;
this._fileLoadingStatus = eOperationStatus.Ready;
}

public async Task SaveCustomExportDataAsync()
{
await ProtectedLocalStore.SetAsync(Vm.DataExchange.ToString(), Vm.ExportSet);
Expand All @@ -180,9 +237,11 @@ public async Task LoadCustomExportDataAsync()
{
Vm.ExportSet = result.Value;
}

StateHasChanged();
}


protected void ReloadRecordAfterEditWithoutModal()
{
if (this.ModalDataView) return; // make a sense when is not modal window
Expand Down
Loading

0 comments on commit d1d033c

Please sign in to comment.