Created a period table for date ranges and made the recipes dependent on it instead of having arbitrary date ranges. This will enable easy copying into new months.
This commit is contained in:
@@ -11,13 +11,9 @@ import { Recipe } from '../recipe';
|
||||
export class RecipeListDatasource extends DataSource<Recipe> {
|
||||
public data: Recipe[];
|
||||
public filteredData: Recipe[];
|
||||
public validFrom: Date | null;
|
||||
public validTill: Date | null;
|
||||
public productGroup: string;
|
||||
|
||||
constructor(
|
||||
private readonly validFromFilter: Observable<Date | null>,
|
||||
private readonly validTillFilter: Observable<Date | null>,
|
||||
private readonly productGroupFilter: Observable<string>,
|
||||
private readonly dataObs: Observable<Recipe[]>,
|
||||
|
||||
@@ -27,15 +23,12 @@ export class RecipeListDatasource extends DataSource<Recipe> {
|
||||
super();
|
||||
this.data = [];
|
||||
this.filteredData = [];
|
||||
this.validFrom = null;
|
||||
this.validTill = null;
|
||||
this.productGroup = '';
|
||||
}
|
||||
|
||||
connect(): Observable<Recipe[]> {
|
||||
const dataMutations: (
|
||||
| Observable<Recipe[]>
|
||||
| Observable<Date | null>
|
||||
| Observable<string>
|
||||
| EventEmitter<PageEvent>
|
||||
| EventEmitter<Sort>
|
||||
@@ -45,16 +38,6 @@ export class RecipeListDatasource extends DataSource<Recipe> {
|
||||
this.data = x;
|
||||
}),
|
||||
),
|
||||
this.validFromFilter.pipe(
|
||||
tap((x) => {
|
||||
this.validFrom = x;
|
||||
}),
|
||||
),
|
||||
this.validTillFilter.pipe(
|
||||
tap((x) => {
|
||||
this.validTill = x;
|
||||
}),
|
||||
),
|
||||
this.productGroupFilter.pipe(
|
||||
tap((x) => {
|
||||
this.productGroup = x;
|
||||
@@ -69,7 +52,7 @@ export class RecipeListDatasource extends DataSource<Recipe> {
|
||||
}
|
||||
|
||||
return merge(...dataMutations).pipe(
|
||||
map(() => this.getFilteredData(this.data, this.productGroup, this.validFrom, this.validTill)),
|
||||
map(() => this.getFilteredData(this.data, this.productGroup)),
|
||||
tap((x: Recipe[]) => {
|
||||
if (this.paginator) {
|
||||
this.paginator.length = x.length;
|
||||
@@ -84,18 +67,8 @@ export class RecipeListDatasource extends DataSource<Recipe> {
|
||||
|
||||
disconnect() {}
|
||||
|
||||
private getFilteredData(
|
||||
data: Recipe[],
|
||||
productGroup: string,
|
||||
validFrom: Date | null,
|
||||
validTill: Date | null,
|
||||
): Recipe[] {
|
||||
return data
|
||||
.filter((x: Recipe) => productGroup === '' || x.notes === productGroup)
|
||||
.filter((x) => validFrom === null || validFrom <= moment(x.validFrom, 'DD-MMM-YYYY').toDate())
|
||||
.filter(
|
||||
(x) => validTill === null || validTill >= moment(x.validTill, 'DD-MMM-YYYY').toDate(),
|
||||
);
|
||||
private getFilteredData(data: Recipe[], productGroup: string): Recipe[] {
|
||||
return data.filter((x: Recipe) => productGroup === '' || x.notes === productGroup);
|
||||
}
|
||||
|
||||
private getPagedData(data: Recipe[]) {
|
||||
@@ -120,12 +93,6 @@ export class RecipeListDatasource extends DataSource<Recipe> {
|
||||
switch (sort.active) {
|
||||
case 'name':
|
||||
return compare(a.sku.name, b.sku.name, isAsc);
|
||||
case 'validity':
|
||||
if (isAsc) {
|
||||
return compareDate(a.validFrom, b.validFrom, isAsc);
|
||||
} else {
|
||||
return compareDate(a.validTill, b.validTill, isAsc);
|
||||
}
|
||||
case 'salePrice':
|
||||
return compare(a.salePrice, b.salePrice, isAsc);
|
||||
case 'costPrice':
|
||||
|
||||
Reference in New Issue
Block a user