Files
DiunaBI/Frontend/src/app/modules/layers/layer-detail/layer-detail.component.ts

82 lines
3.3 KiB
TypeScript
Raw Normal View History

2023-08-22 21:13:47 +02:00
import { DatePipe, NgIf, DecimalPipe, JsonPipe } from '@angular/common';
2022-12-21 19:30:24 +01:00
import { HttpClient } from '@angular/common/http';
2023-08-22 21:29:10 +02:00
import { Component, OnInit, ViewChild } from '@angular/core';
2023-06-23 15:32:59 +02:00
import { UntypedFormGroup, UntypedFormBuilder, FormsModule, ReactiveFormsModule } from '@angular/forms';
2023-08-22 21:29:10 +02:00
import { MatSort, MatSortModule } from '@angular/material/sort';
2023-06-23 15:32:59 +02:00
import { MatTableDataSource, MatTableModule } from '@angular/material/table';
2023-01-08 16:57:21 +01:00
import { ActivatedRoute } from '@angular/router';
2023-09-18 19:56:02 +02:00
import { Layer, LayerType } from 'src/app/models/layer.model';
2023-01-06 11:10:58 +01:00
import { Record } from 'src/app/models/record.model';
2023-01-08 16:57:21 +01:00
import { NotificationsService } from 'src/app/services/notifications.service';
2023-01-07 12:53:04 +01:00
import { environment } from 'src/environments/environment';
2023-06-23 15:32:59 +02:00
import { MatInputModule } from '@angular/material/input';
import { MatFormFieldModule } from '@angular/material/form-field';
import { MatGridListModule } from '@angular/material/grid-list';
import { MatButtonModule } from '@angular/material/button';
import { MatCardModule } from '@angular/material/card';
2022-12-11 23:40:16 +01:00
@Component({
2023-08-22 18:32:17 +02:00
selector: 'diunabi-layer-detail',
2023-06-23 15:32:59 +02:00
templateUrl: './layer-detail.component.html',
styleUrls: ['./layer-detail.component.scss'],
standalone: true,
2023-08-23 17:30:25 +02:00
imports: [FormsModule, ReactiveFormsModule, MatCardModule,
2023-06-23 16:43:33 +02:00
MatButtonModule, MatGridListModule, MatFormFieldModule, MatInputModule,
2023-08-22 21:13:47 +02:00
NgIf, MatTableModule, MatSortModule, DecimalPipe, JsonPipe],
2023-06-23 16:43:33 +02:00
providers: [DatePipe]
2022-12-11 23:40:16 +01:00
})
2023-01-06 11:55:01 +01:00
export class LayerDetailComponent implements OnInit {
2022-12-11 23:40:16 +01:00
2022-12-21 19:30:24 +01:00
public form!: UntypedFormGroup;
2023-01-06 11:10:58 +01:00
public document!: Layer;
2022-12-21 19:30:24 +01:00
2023-08-20 13:05:10 +02:00
valueSum = 0;
2023-01-07 12:53:04 +01:00
displayedColumns = environment.views.layers.recordColumns.split("|");
2023-01-06 11:10:58 +01:00
dataSource!: MatTableDataSource<Record>;
2022-12-21 19:30:24 +01:00
@ViewChild(MatSort) sort!: MatSort;
constructor(
private fb$: UntypedFormBuilder,
private http$: HttpClient,
private route$: ActivatedRoute,
2023-01-08 16:57:21 +01:00
private datePipe: DatePipe,
private notifications$: NotificationsService
2023-08-22 21:13:47 +02:00
) {
this.form = Layer.getForm(this.fb$);
}
2022-12-21 19:30:24 +01:00
async ngOnInit() {
this.document = await this.load();
2023-01-06 11:10:58 +01:00
this.dataSource = new MatTableDataSource<Record>(this.document.records);
2022-12-21 19:30:24 +01:00
this.dataSource.sort = this.sort;
this.document.fillForm(this.form);
this.form.disable();
2022-12-27 22:22:21 +01:00
this.document.created = `${this.datePipe.transform(this.document.createdAt?.toDate(), 'short')}, ${this.document.createdBy?.userName}`;
2023-08-21 13:00:05 +02:00
this.valueSum = this.document.records.map(t => t.value1 || 0).reduce((acc, value) => acc + value, 0);
2023-09-18 19:56:02 +02:00
if (this.document.type === LayerType.Processed) {
this.displayedColumns = ['code', 'value1', 'value2', 'value3', 'value4',
'value5', 'value6', 'value7', 'value8', 'value9', 'value10', 'value11',
'value12', 'value13', 'value14', 'value15', 'value16', 'value17', 'value18',
'value19', 'value20', 'value21', 'value22', 'value23', 'value24', 'value25',
'value26', 'value27', 'value28', 'value29', 'value30', 'value31'];
}
2022-12-21 19:30:24 +01:00
}
2023-01-06 11:10:58 +01:00
private async load(): Promise<Layer> {
return await Layer.getById(this.route$.snapshot.paramMap.get('id') || "", this.http$);
2022-12-21 19:30:24 +01:00
}
2023-08-22 21:13:47 +02:00
2022-12-22 15:12:19 +01:00
async export() {
2023-01-06 11:10:58 +01:00
if (await Layer.exportToGoogleSheet(this.document.id || "", this.http$)) {
2023-01-08 16:57:21 +01:00
this.notifications$.add({
2023-01-12 14:47:38 +01:00
text: "The file was saved on Google Drive",
2023-01-08 16:57:21 +01:00
});
2022-12-22 15:12:19 +01:00
} else {
2023-01-08 16:57:21 +01:00
this.notifications$.add({
2023-01-12 14:47:38 +01:00
text: "Save failed!",
2023-01-08 16:57:21 +01:00
});
2022-12-22 15:12:19 +01:00
}
}
2022-12-11 23:40:16 +01:00
}