63 lines
2.2 KiB
TypeScript
63 lines
2.2 KiB
TypeScript
import { DatePipe } from '@angular/common';
|
|
import { HttpClient } from '@angular/common/http';
|
|
import { Component, OnInit, ViewChild } from '@angular/core';
|
|
import { UntypedFormGroup, UntypedFormBuilder } from '@angular/forms';
|
|
import { MatPaginator } from '@angular/material/paginator';
|
|
import { MatSnackBar } from '@angular/material/snack-bar';
|
|
import { MatSort, MatSortable } from '@angular/material/sort';
|
|
import { MatTableDataSource } from '@angular/material/table';
|
|
import { Router, ActivatedRoute } from '@angular/router';
|
|
import { AuthService } from 'src/app/auth/auth.service';
|
|
import { Layer } from 'src/app/models/layer.model';
|
|
import { Record } from 'src/app/models/record.model';
|
|
|
|
@Component({
|
|
selector: 'app-layer-detail',
|
|
templateUrl: './layer-detail.component.html',
|
|
styleUrls: ['./layer-detail.component.scss']
|
|
})
|
|
export class LayerDetailComponent implements OnInit {
|
|
|
|
public form!: UntypedFormGroup;
|
|
public document!: Layer;
|
|
|
|
displayedColumns = ['code', 'value'];
|
|
dataSource!: MatTableDataSource<Record>;
|
|
|
|
@ViewChild(MatSort) sort!: MatSort;
|
|
|
|
constructor(
|
|
private fb$: UntypedFormBuilder,
|
|
private router$: Router,
|
|
private http$: HttpClient,
|
|
private route$: ActivatedRoute,
|
|
private auth$: AuthService,
|
|
private snackBar: MatSnackBar,
|
|
private datePipe: DatePipe
|
|
) { }
|
|
|
|
async ngOnInit() {
|
|
this.form = Layer.getForm(this.fb$);
|
|
this.document = await this.load();
|
|
this.dataSource = new MatTableDataSource<Record>(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.dataSource.sort.sort({ id: 'code', start: 'desc' } as MatSortable);
|
|
}
|
|
private async load(): Promise<Layer> {
|
|
return await Layer.getById(this.route$.snapshot.paramMap.get('id') || "", this.http$);
|
|
}
|
|
trackByUid(index : number, item : Record) {
|
|
return item.id;
|
|
}
|
|
async export() {
|
|
if (await Layer.exportToGoogleSheet(this.document.id || "", this.http$)) {
|
|
this.snackBar.open("Plik został zapisany na dysku Google", "OK");
|
|
} else {
|
|
this.snackBar.open("Zapis się nie udał.", "OK");
|
|
}
|
|
}
|
|
}
|