song list filter
This commit is contained in:
@@ -6,6 +6,7 @@ import {combineLatest, Observable} from 'rxjs';
|
||||
import {fade} from '../../../animations';
|
||||
import {ActivatedRoute} from '@angular/router';
|
||||
import {filterSong} from '../../../services/filter.helper';
|
||||
import {FilterValues} from './filter/filter-values';
|
||||
|
||||
@Component({
|
||||
selector: 'app-songs',
|
||||
@@ -16,6 +17,7 @@ import {filterSong} from '../../../services/filter.helper';
|
||||
export class SongListComponent implements OnInit {
|
||||
|
||||
public songs$: Observable<Song[]>;
|
||||
public anyFilterActive = false;
|
||||
|
||||
constructor(private songService: SongService, private activatedRoute: ActivatedRoute) {
|
||||
}
|
||||
@@ -23,7 +25,7 @@ export class SongListComponent implements OnInit {
|
||||
ngOnInit() {
|
||||
const filter$ = this.activatedRoute.queryParams.pipe(
|
||||
debounceTime(300),
|
||||
map(_ => _.q)
|
||||
map(_ => _ as FilterValues)
|
||||
);
|
||||
|
||||
const songs$ = this.songService.list$().pipe(
|
||||
@@ -31,7 +33,35 @@ export class SongListComponent implements OnInit {
|
||||
);
|
||||
|
||||
this.songs$ = combineLatest([filter$, songs$]).pipe(
|
||||
map(_ => _[1].filter(song => filterSong(song, _[0])))
|
||||
map(_ => {
|
||||
let songs = _[1];
|
||||
let filter = _[0];
|
||||
this.anyFilterActive = this.checkIfFilterActive(filter);
|
||||
return songs.filter(song => this.filter(song, filter));
|
||||
})
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
private filter(song: Song, filter: FilterValues): boolean {
|
||||
let baseFilter = filterSong(song, filter.q);
|
||||
baseFilter = baseFilter && (!filter.type || filter.type === song.type);
|
||||
baseFilter = baseFilter && (!filter.legalType || filter.legalType === song.legalType);
|
||||
baseFilter = baseFilter && (!filter.flag || this.checkFlag(filter.flag, song.flags));
|
||||
|
||||
return baseFilter;
|
||||
}
|
||||
|
||||
private checkIfFilterActive(filter: FilterValues): boolean {
|
||||
return !!filter.q || !!filter.type || !!filter.legalType || !!filter.flag;
|
||||
}
|
||||
|
||||
private checkFlag(flag: string, flags: string) {
|
||||
if (!flags) return false;
|
||||
|
||||
const flagStrings = flags.split(';');
|
||||
if (flagStrings.length === 0) return false;
|
||||
|
||||
return flagStrings.indexOf(flag) !== -1;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user