Skip to content

Commit

Permalink
[#24] change fileItem DataSource structure & support getting FileItems
Browse files Browse the repository at this point in the history
  • Loading branch information
philipphoeninger committed Oct 28, 2024
1 parent cf8b618 commit a232bff
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 30 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { DataSource } from '@angular/cdk/collections';
import { FileItem } from '../file-item.model';
import { FileItem } from '../models/file-item.model';
import { MatPaginator } from '@angular/material/paginator';
import { MatSort } from '@angular/material/sort';
import { Observable, merge, BehaviorSubject } from 'rxjs';
Expand All @@ -8,11 +8,20 @@ import { compare } from '../helper/compare.function';
import { FileItemsApiService } from '../fileItems-api.service';

export class FileItemsDataSource extends DataSource<FileItem> {
data: BehaviorSubject<FileItem[]> = new BehaviorSubject<FileItem[]>([]);
paginator: MatPaginator | undefined;
sort: MatSort | undefined;
dataStream: BehaviorSubject<FileItem[]> = new BehaviorSubject<FileItem[]>([]);

constructor(private fileItemsApiService: FileItemsApiService) {
set data(d: FileItem[]) {
this.dataStream.next(d);
}
get data(): FileItem[] {
return this.dataStream.value;
}

constructor(
private paginator: MatPaginator,
private sort: MatSort,
private fileItemsApiService: FileItemsApiService,
) {
super();
}

Expand All @@ -21,30 +30,37 @@ export class FileItemsDataSource extends DataSource<FileItem> {
* @returns A stream of the items to be rendered.
*/
connect(): Observable<FileItem[]> {
if (this.paginator && this.sort) {
return merge(
this.fileItemsApiService.getFileItems(),
this.paginator.page,
this.sort.sortChange,
).pipe(
map((fileItems) => {
if (fileItems instanceof Array) {
this.data.next(fileItems);
return this.getPagedData(this.getSortedData([...fileItems]));
} else {
return this.getPagedData(this.getSortedData([...this.data.value]));
}
}),
);
} else {
throw Error(
'Please set the paginator and sort on the data source before connecting.',
);
}
const dataMutations = [
this.dataStream,
this.paginator.page,
this.sort.sortChange,
];

this.paginator.length = this.data.length;

return merge(...dataMutations).pipe(
map(() => {
return this.getPagedData(this.getSortedData([...this.data]));
}),
);
}

disconnect(): void {}

public getFileItems(fileItem?: FileItem): Observable<FileItem[]> {
return this.fileItemsApiService.getFileItems(fileItem).pipe(
map((result) => {
if (result instanceof Array) {
this.dataStream.next(result);
return this.getPagedData(this.getSortedData([...result]));
} else {
this.dataStream.next([result]);
return [result];
}
}),
);
}

private getPagedData(data: FileItem[]): FileItem[] {
if (this.paginator) {
const startIndex = this.paginator.pageIndex * this.paginator.pageSize;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FileItem } from '../file-item.model';
import { FileItem } from '../models/file-item.model';

export let MOCK_FILE_ITEMS: FileItem[] = [
new FileItem(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { IFileTypeGroup } from '../file-type-group.interface';
import { EnFileType } from '../file-type.enum';
import { IFileTypeGroup } from '../models/file-type-group.interface';
import { EnFileType } from '../models/file-type.enum';

export let MOCK_FILE_TYPE_GROUPS: IFileTypeGroup[] = [
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { EnTimeSpan } from '../time-span.enum';
import { ITimeSpan } from '../time-span.interface';
import { EnTimeSpan } from '../models/time-span.enum';
import { ITimeSpan } from '../models/time-span.interface';

export let MOCK_TIME_SPANS: ITimeSpan[] = [
{ value: EnTimeSpan.Today, label: 'Today' },
Expand Down

0 comments on commit a232bff

Please sign in to comment.