import { DatePipe, NgIf, DecimalPipe, JsonPipe } from '@angular/common'; import { HttpClient } from '@angular/common/http'; import { Component, OnInit, ViewChild } from '@angular/core'; import { UntypedFormGroup, UntypedFormBuilder, FormsModule, ReactiveFormsModule } from '@angular/forms'; import { MatSort, MatSortModule } from '@angular/material/sort'; import { MatTableDataSource, MatTableModule } from '@angular/material/table'; import { ActivatedRoute } from '@angular/router'; import { Layer, LayerType } from 'src/app/models/layer.model'; import { Record } from 'src/app/models/record.model'; import { NotificationsService } from 'src/app/services/notifications.service'; import { environment } from 'src/environments/environment'; 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'; @Component({ selector: 'diunabi-layer-detail', templateUrl: './layer-detail.component.html', styleUrls: ['./layer-detail.component.scss'], standalone: true, imports: [FormsModule, ReactiveFormsModule, MatCardModule, MatButtonModule, MatGridListModule, MatFormFieldModule, MatInputModule, NgIf, MatTableModule, MatSortModule, DecimalPipe, JsonPipe], providers: [DatePipe] }) export class LayerDetailComponent implements OnInit { public form!: UntypedFormGroup; public document!: Layer; valueSum = 0; displayedColumns = environment.views.layers.recordColumns.split("|"); dataSource!: MatTableDataSource; @ViewChild(MatSort) sort!: MatSort; constructor( private fb$: UntypedFormBuilder, private http$: HttpClient, private route$: ActivatedRoute, private datePipe: DatePipe, private notifications$: NotificationsService ) { this.form = Layer.getForm(this.fb$); } async ngOnInit() { this.document = await this.load(); this.dataSource = new MatTableDataSource(this.document.records); this.dataSource.sort = this.sort; this.document.fillForm(this.form); this.form.disable(); this.document.created = `${this.datePipe.transform(this.document.createdAt?.toDate(), 'short')}, ${this.document.createdBy?.userName}`; this.valueSum = this.document.records.map(t => t.value1 || 0).reduce((acc, value) => acc + value, 0); 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']; } } private async load(): Promise { return await Layer.getById(this.route$.snapshot.paramMap.get('id') || "", this.http$); } async export() { if (await Layer.exportToGoogleSheet(this.document.id || "", this.http$)) { this.notifications$.add({ text: "The file was saved on Google Drive", }); } else { this.notifications$.add({ text: "Save failed!", }); } } }