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

76 lines
3.0 KiB
TypeScript
Raw Normal View History

2023-06-23 15:32:59 +02:00
import { DatePipe, NgIf, DecimalPipe } from '@angular/common';
2022-12-21 19:30:24 +01:00
import { HttpClient } from '@angular/common/http';
2023-01-06 11:55:01 +01:00
import { Component, OnInit, ViewChild } from '@angular/core';
2023-06-23 15:32:59 +02:00
import { UntypedFormGroup, UntypedFormBuilder, FormsModule, ReactiveFormsModule } from '@angular/forms';
import { MatSort, MatSortable, MatSortModule } from '@angular/material/sort';
import { MatTableDataSource, MatTableModule } from '@angular/material/table';
2023-01-08 16:57:21 +01:00
import { ActivatedRoute } from '@angular/router';
2022-12-21 19:30:24 +01:00
import { AuthService } from 'src/app/auth/auth.service';
2023-01-06 11:10:58 +01:00
import { Layer } from 'src/app/models/layer.model';
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 { MatToolbarModule } from '@angular/material/toolbar';
import { MatCardModule } from '@angular/material/card';
2022-12-11 23:40:16 +01:00
@Component({
2023-06-23 15:32:59 +02:00
selector: 'app-layer-detail',
templateUrl: './layer-detail.component.html',
styleUrls: ['./layer-detail.component.scss'],
standalone: true,
2023-06-23 16:43:33 +02:00
imports: [FormsModule, ReactiveFormsModule, MatCardModule, MatToolbarModule,
MatButtonModule, MatGridListModule, MatFormFieldModule, MatInputModule,
NgIf, MatTableModule, MatSortModule, DecimalPipe],
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-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
2022-12-21 19:30:24 +01:00
) { }
async ngOnInit() {
2023-01-06 11:10:58 +01:00
this.form = Layer.getForm(this.fb$);
2022-12-21 19:30:24 +01:00
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}`;
this.dataSource.sort.sort({ id: 'code', start: 'desc' } as MatSortable);
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-01-06 11:10:58 +01:00
trackByUid(index : number, item : Record) {
2022-12-21 19:30:24 +01:00
return item.id;
}
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
}