-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(dh): file upload for RSM-012 (#3872)
Co-authored-by: Morten Hansen <mimo@mimo-design.com>
- Loading branch information
Showing
11 changed files
with
188 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
apps/dh/api-dh/source/DataHub.WebApi/GraphQL/Types/Calculation/CalculationsExtension.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// Copyright 2020 Energinet DataHub A/S | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License2"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
namespace Energinet.DataHub.WebApi.GraphQL.Types.Calculation; | ||
|
||
[ExtendObjectType("CalculationsConnection")] | ||
public class CalculationsExtension | ||
{ | ||
public string? GetCapacitySettlementsUploadUrl( | ||
[Service] IHttpContextAccessor httpContextAccessor, | ||
[Service] LinkGenerator linkGenerator) => | ||
linkGenerator.GetUriByAction( | ||
httpContextAccessor.HttpContext!, | ||
"ImportCapacitySettlements", | ||
"Dh2Bridge"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,7 @@ query GetCalculations( | |
startCursor | ||
endCursor | ||
} | ||
capacitySettlementsUploadUrl | ||
nodes { | ||
id | ||
state | ||
|
111 changes: 111 additions & 0 deletions
111
.../feature-calculations/src/lib/file-uploader/dh-capacity-settlements-uploader.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
//#region License | ||
/** | ||
* @license | ||
* Copyright 2020 Energinet DataHub A/S | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License2"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
//#endregion | ||
import { Component, ElementRef, inject, input, viewChild } from '@angular/core'; | ||
import { HttpClient } from '@angular/common/http'; | ||
import { translate } from '@ngneat/transloco'; | ||
import { tapResponse } from '@ngrx/operators'; | ||
|
||
import { WattToastService } from '@energinet-datahub/watt/toast'; | ||
|
||
const jsonExt = '.json'; | ||
const jsonMimeType = 'application/json'; | ||
|
||
@Component({ | ||
selector: 'dh-capacity-settlements-uploader', | ||
styles: [ | ||
` | ||
.upload-input { | ||
display: none; | ||
} | ||
`, | ||
], | ||
template: `<input | ||
type="file" | ||
class="upload-input" | ||
[accept]="jsonExt" | ||
(change)="onFileSelected(uploadInput.files)" | ||
#uploadInput | ||
/>`, | ||
}) | ||
export class DhCapacitySettlementsUploaderComponent { | ||
private readonly httpClient = inject(HttpClient); | ||
private readonly toastService = inject(WattToastService); | ||
|
||
private uploadInput = viewChild.required<ElementRef<HTMLInputElement>>('uploadInput'); | ||
|
||
uploadUrl = input.required<string>(); | ||
|
||
jsonExt = jsonExt; | ||
|
||
selectFile(): void { | ||
this.uploadInput().nativeElement.click(); | ||
} | ||
|
||
onFileSelected(files: FileList | null): void { | ||
if (files == null) { | ||
return; | ||
} | ||
|
||
const file = files[0]; | ||
|
||
if (file.type === jsonMimeType) { | ||
return this.startUpload(file); | ||
} | ||
} | ||
|
||
private startUpload(file: File): void { | ||
this.toastService.open({ | ||
type: 'loading', | ||
message: translate('wholesale.calculations.capacitySettlements.uploadInProgress'), | ||
}); | ||
|
||
const formData = new FormData(); | ||
formData.append('jsonFile', file); | ||
|
||
this.httpClient | ||
.post(this.uploadUrl(), formData) | ||
.pipe( | ||
tapResponse( | ||
() => this.onUploadSuccessFn(), | ||
() => this.onUploadErrorFn() | ||
) | ||
) | ||
.subscribe(); | ||
} | ||
|
||
private onUploadSuccessFn = () => { | ||
const message = translate('wholesale.calculations.capacitySettlements.uploadSuccess'); | ||
|
||
this.toastService.open({ type: 'success', message }); | ||
|
||
this.resetUploadInput(); | ||
}; | ||
|
||
private onUploadErrorFn = () => { | ||
const message = translate('wholesale.calculations.capacitySettlements.uploadError'); | ||
|
||
this.toastService.open({ type: 'danger', message }); | ||
|
||
this.resetUploadInput(); | ||
}; | ||
|
||
private resetUploadInput(): void { | ||
this.uploadInput().nativeElement.value = ''; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters