53 lines
1.5 KiB
TypeScript
53 lines
1.5 KiB
TypeScript
import { DataSource } from '@angular/cdk/collections';
|
|
import { map, Observable, tap } from 'rxjs';
|
|
|
|
import { BillRow } from './bill-row';
|
|
import { Kot } from './kot';
|
|
|
|
export class BillsDataSource extends DataSource<BillRow> {
|
|
private data: Kot[] = [];
|
|
constructor(private readonly dataObs: Observable<Kot[]>) {
|
|
super();
|
|
}
|
|
|
|
connect(): Observable<BillRow[]> {
|
|
return this.dataObs.pipe(
|
|
tap((x) => {
|
|
this.data = x;
|
|
}),
|
|
map((d) =>
|
|
d.flatMap((k) => {
|
|
const kotRow: BillRow = new BillRow({ kind: 'kot', kot: k, id: k.id! });
|
|
const rows: BillRow[] = [];
|
|
for (const inv of k.inventories) {
|
|
if (!inv.id) {
|
|
inv.clientId = inv.clientId || crypto.randomUUID();
|
|
}
|
|
rows.push(new BillRow({ kind: 'inv', kot: k, inv, id: `${k.id}-${inv.id || inv.clientId}` }));
|
|
if (inv.type === 'bundle') {
|
|
for (const child of inv.children) {
|
|
if (!child.id) {
|
|
child.clientId = child.clientId || crypto.randomUUID();
|
|
}
|
|
child.parentId = inv.id || inv.clientId;
|
|
rows.push(
|
|
new BillRow({
|
|
kind: 'child',
|
|
kot: k,
|
|
parent: inv,
|
|
inv: child,
|
|
id: `${k.id}-${child.id || child.clientId}`,
|
|
}),
|
|
);
|
|
}
|
|
}
|
|
}
|
|
return [kotRow, ...rows];
|
|
}),
|
|
),
|
|
);
|
|
}
|
|
|
|
disconnect() {}
|
|
}
|