global filter
This commit is contained in:
@@ -3,9 +3,9 @@ import {combineLatest} from 'rxjs';
|
||||
import {Show} from '../services/show';
|
||||
import {fade} from '../../../animations';
|
||||
import {ShowService} from '../services/show.service';
|
||||
import {FilterValues} from './filter/filter-values';
|
||||
import {ActivatedRoute, RouterLink} from '@angular/router';
|
||||
import {FilterValues} from './filter/filter-values'import {RouterLink} from '@angular/router';
|
||||
import {map, switchMap} from 'rxjs/operators';
|
||||
import {FilterStoreService} from '../../../services/filter-store.service';
|
||||
import {RoleDirective} from '../../../services/user/role.directive';
|
||||
import {ListHeaderComponent} from '../../../widget-modules/components/list-header/list-header.component';
|
||||
import {AsyncPipe} from '@angular/common';
|
||||
@@ -23,37 +23,20 @@ import {SortByPipe} from '../../../widget-modules/pipes/sort-by/sort-by.pipe';
|
||||
})
|
||||
export class ListComponent {
|
||||
private showService = inject(ShowService);
|
||||
private activatedRoute = inject(ActivatedRoute);
|
||||
private filterStore = inject(FilterStoreService);
|
||||
|
||||
public lastMonths$ = this.activatedRoute.queryParams.pipe(
|
||||
map(params => {
|
||||
const filterValues = params as FilterValues;
|
||||
if (!filterValues?.time) return 1;
|
||||
return +filterValues.time;
|
||||
})
|
||||
);
|
||||
public owner$ = this.activatedRoute.queryParams.pipe(
|
||||
map(params => {
|
||||
const filterValues = params as FilterValues;
|
||||
return filterValues?.owner;
|
||||
})
|
||||
);
|
||||
public showType$ = this.activatedRoute.queryParams.pipe(
|
||||
map(params => {
|
||||
const filterValues = params as FilterValues;
|
||||
return filterValues?.showType;
|
||||
})
|
||||
);
|
||||
public filter$ = this.filterStore.showFilter$;
|
||||
public lastMonths$ = this.filter$.pipe(map((filterValues: FilterValues) => filterValues.time || 1));
|
||||
public owner$ = this.filter$.pipe(map((filterValues: FilterValues) => filterValues.owner));
|
||||
public showType$ = this.filter$.pipe(map((filterValues: FilterValues) => filterValues.showType));
|
||||
public shows$ = this.showService.list$();
|
||||
public privateShows$ = this.showService.list$().pipe(map(show => show.filter(_ => !_.published)));
|
||||
public privateShows$ = combineLatest([this.shows$, this.filter$]).pipe(
|
||||
map(([shows, filter]) => shows.filter(show => !show.published).filter(show => this.matchesPrivateFilter(show, filter)))
|
||||
);
|
||||
public queriedPublicShows$ = this.lastMonths$.pipe(switchMap(lastMonths => this.showService.listPublicSince$(lastMonths)));
|
||||
public fallbackPublicShows$ = combineLatest([this.shows$, this.lastMonths$]).pipe(
|
||||
map(([shows, lastMonths]) => {
|
||||
const startDate = new Date();
|
||||
startDate.setHours(0, 0, 0, 0);
|
||||
startDate.setDate(startDate.getDate() - lastMonths * 30);
|
||||
|
||||
return shows.filter(show => show.published && !show.archived && show.date.toDate() >= startDate);
|
||||
return shows.filter(show => show.published && !show.archived).filter(show => this.matchesTimeFilter(show, lastMonths));
|
||||
})
|
||||
);
|
||||
public publicShows$ = combineLatest([this.queriedPublicShows$, this.fallbackPublicShows$, this.owner$, this.showType$]).pipe(
|
||||
@@ -65,4 +48,19 @@ export class ListComponent {
|
||||
);
|
||||
|
||||
public trackBy = (index: number, show: unknown) => (show as Show).id;
|
||||
|
||||
private matchesFilter(show: Show, filter: FilterValues): boolean {
|
||||
return this.matchesTimeFilter(show, filter.time || 1) && (!filter.owner || show.owner === filter.owner) && (!filter.showType || show.showType === filter.showType);
|
||||
}
|
||||
|
||||
private matchesPrivateFilter(show: Show, filter: FilterValues): boolean {
|
||||
return this.matchesTimeFilter(show, filter.time || 1) && (!filter.showType || show.showType === filter.showType);
|
||||
}
|
||||
|
||||
private matchesTimeFilter(show: Show, lastMonths: number): boolean {
|
||||
const startDate = new Date();
|
||||
startDate.setHours(0, 0, 0, 0);
|
||||
startDate.setDate(startDate.getDate() - lastMonths * 30);
|
||||
return show.date.toDate() >= startDate;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user