InfiniteScroll and Layer list filter

This commit is contained in:
Michał Zieliński
2023-10-20 13:56:40 +02:00
parent a358dc75cf
commit a96ca6a795
9 changed files with 227 additions and 201 deletions

View File

@@ -1,5 +1,5 @@
import { HttpClient } from '@angular/common/http';
import { Component, HostListener, OnInit } from '@angular/core';
import { Component, OnInit } from '@angular/core';
import { MatSortModule } from '@angular/material/sort';
import { MatTableModule } from '@angular/material/table';
import { Layer, LayerType } from 'src/app/models/layer.model';
@@ -12,7 +12,9 @@ import { MatSelectModule } from '@angular/material/select';
import { FormsModule } from '@angular/forms';
import { MatChipsModule} from '@angular/material/chips';
import { MatIconModule } from '@angular/material/icon';
import { NgFor } from '@angular/common';
import { KeyValuePipe, NgFor } from '@angular/common';
import { ScrollEndDirective } from 'src/app/directives/scroll-end.directive';
import { Subject, debounceTime, distinctUntilChanged } from 'rxjs';
@Component({
selector: 'diunabi-layers-list',
@@ -21,7 +23,7 @@ import { NgFor } from '@angular/common';
standalone: true,
imports: [MatGridListModule, MatButtonModule, RouterLink, MatFormFieldModule,
MatInputModule, MatTableModule, MatSortModule, MatSelectModule, FormsModule,
MatChipsModule, MatIconModule, NgFor]
MatChipsModule, MatIconModule, NgFor, ScrollEndDirective, KeyValuePipe]
})
export class LayersListComponent implements OnInit {
displayedColumns = ['number', 'name', 'type'];
@@ -33,51 +35,36 @@ export class LayersListComponent implements OnInit {
end: number = this.limit + this.start;
loadingInProgress = false;
type = "";
codes: string[] = [];
type: LayerType | '' = '';
name: string = '';
nameUpdate = new Subject<string>();
constructor(
private _http: HttpClient,
) { }
//@HostListener('document:scroll', ['$event'])
// eslint-disable-next-line @typescript-eslint/no-explicit-any
public async onScroll(event: any) {
console.log('on scrool', event);
if (event.target.offsetHeight + event.target.scrollTop >= event.target.scrollHeight - 1 && !this.loadingInProgress) {
this.loadingInProgress = true;
this.dataSource = await Layer.getList(this._http, this.start, this.limit, this.codes);
this.start = this.end;
this.end = this.limit + this.start;
setTimeout(() => {
this.loadingInProgress = false;
}, 500);
}
}
async ngOnInit() {
this.dataSource = await Layer.getList(this._http, this.start, this.limit, this.codes);
this.nameUpdate.pipe(
debounceTime(400),
distinctUntilChanged())
.subscribe(() => {
this.loadList();
});
await this.loadList();
}
async loadList() {
this.start = 0;
this.end = this.limit;
this.dataSource = await Layer.getList(this._http, this.start, this.limit, this.name, this.type);
}
async loadMore() {
this.start = this.end;
this.end = this.limit + this.start;
}
async removeCode(code: string) {
const index = this.codes.indexOf(code);
if (index >= 0) {
this.start = 0;
this.end = this.limit + this.start;
this.codes.splice(index, 1);
this.dataSource = await Layer.getList(this._http, this.start, this.limit, this.codes);
}
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
async addCode(event: any) {
const value = (event.target.value || '').trim();
if (value) {
this.start = 0;
this.end = this.limit + this.start;
this.codes.push(value);
this.dataSource = await Layer.getList(this._http, this.start, this.limit, this.codes);
}
event.target.value = '';
this.end += this.limit;
this.dataSource = this.dataSource.concat(
await Layer.getList(this._http, this.start, this.limit, this.name, this.type)
);
}
}