Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 | 2x 2x 2x 2x 2x 2x 6x 6x 6x 6x 6x 6x 1x 1x 2x 2x 8x 2x 2x 14x 14x 2x 2x 2x 2x 2x 2x 2x 2x 6x 6x 2x 2x 2x 1x 1x 2x 1x 1x 1x | import {
ChangeDetectionStrategy,
ChangeDetectorRef,
Component,
DestroyRef,
ElementRef,
OnInit,
ViewChild,
inject,
} from '@angular/core';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
import { FormBuilder, FormGroup } from '@angular/forms';
import { CostCenterBase } from 'ish-core/models/cost-center/cost-center.model';
import { CsvImportData, CsvImportHandler, CsvImportStatus } from 'ish-core/utils/csv/csv.import-handler';
import { OrganizationManagementFacade } from '../../../facades/organization-management.facade';
@Component({
selector: 'ish-cost-center-csv-import',
templateUrl: './cost-center-csv-import.component.html',
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class CostCenterCsvImportComponent implements OnInit {
csvForm: FormGroup;
status: CsvImportStatus = 'Default';
// not-dead-code
parsedCostCenters: CostCenterBase[];
// not-dead-code
costCenterHeaders = [
'costCenterId',
'name',
'budgetCurrency',
'budgetValue',
'budgetPeriod',
'costCenterOwnerLogin',
'active',
];
private readonly destroyRef = inject(DestroyRef);
@ViewChild('fileInput', { static: false }) fileInput: ElementRef<HTMLInputElement>;
constructor(
private organizationManagementFacade: OrganizationManagementFacade,
private fb: FormBuilder,
private cdRef: ChangeDetectorRef
) {}
ngOnInit() {
this.parsedCostCenters = [];
this.csvForm = this.fb.group({
csvFile: [undefined],
});
}
onFileChange(event: Event) {
const input = event.target as HTMLInputElement;
const file = input.files?.[0];
Iif (!file) {
return;
}
CsvImportHandler.processCsvFile(file, this.costCenterHeaders)
.pipe(takeUntilDestroyed(this.destroyRef))
.subscribe({
next: fileContent => {
this.parsedCostCenters = this.parseCsvData(fileContent);
this.status = 'Valid';
this.cdRef.markForCheck();
},
error: error => {
this.status = error;
this.parsedCostCenters = [];
this.cdRef.markForCheck();
},
});
}
// not-dead-code
parseCsvData(csvData: CsvImportData): CostCenterBase[] {
Iif (!csvData) {
return [];
}
return csvData.data.map(line => {
const values = line.split(',').map(v => v.trim());
const obj: Partial<CostCenterBase> = {};
let budgetCurrency: string | undefined;
let budgetValue: number | undefined;
let costCenterOwnerLogin: string | undefined;
csvData.headers.forEach((header, index) => {
const value = values[index] !== undefined ? values[index] : '';
switch (header) {
case 'budgetCurrency':
budgetCurrency = value;
break;
case 'budgetValue':
budgetValue = value.trim() !== '' ? +value : undefined;
break;
case 'costCenterOwnerLogin':
costCenterOwnerLogin = value;
break;
case 'active':
obj.active = value.trim() !== '' ? value?.toLowerCase() === 'true' : undefined;
break;
default:
(obj as Record<string, string>)[header] = value;
break;
}
});
obj.budget = {
type: 'Money',
value: budgetValue,
currency: budgetCurrency,
};
obj.costCenterOwner = {
login: costCenterOwnerLogin,
};
return obj as CostCenterBase;
});
}
resetInput() {
this.parsedCostCenters = [];
this.status = 'Default';
}
submitCostCenters() {
if (this.parsedCostCenters.length === 0) {
return;
}
this.organizationManagementFacade.addCostCentersFromCsv(this.parsedCostCenters);
}
get isCsvDisabled() {
return this.status !== 'Valid';
}
}
|