|
|
@@ -0,0 +1,59 @@
|
|
|
+import { Component } from '@angular/core';
|
|
|
+import { MatDialogRef, MAT_DIALOG_DATA } from '@angular/material/dialog';
|
|
|
+import { Subject } from 'rxjs';
|
|
|
+import { TracksApiService } from 'src/app/tracks-api.service';
|
|
|
+import { Track } from '../track';
|
|
|
+import { TrackSearchParams } from '../track-search-params';
|
|
|
+
|
|
|
+@Component({
|
|
|
+ selector: 'app-track-picker-dialog',
|
|
|
+ templateUrl: './track-picker-dialog.component.html',
|
|
|
+ styleUrls: ['./track-picker-dialog.component.scss']
|
|
|
+})
|
|
|
+export class TrackPickerDialogComponent {
|
|
|
+ private _tracks: Track[] = [];
|
|
|
+ public tracks: Subject<Track[]> = new Subject<Track[]>();
|
|
|
+ public hasMore: boolean = false;
|
|
|
+
|
|
|
+ public nameFilter: string = '';
|
|
|
+ private searchParams: TrackSearchParams = new TrackSearchParams();
|
|
|
+
|
|
|
+ constructor(
|
|
|
+ public dialogRef: MatDialogRef<TrackPickerDialogComponent>,
|
|
|
+ private readonly tracksApi: TracksApiService
|
|
|
+ ) {
|
|
|
+ this.updateTracks();
|
|
|
+ }
|
|
|
+
|
|
|
+ onCancelClick(): void {
|
|
|
+ this.dialogRef.close();
|
|
|
+ }
|
|
|
+
|
|
|
+ public onPick(track: Track | null): void {
|
|
|
+ this.dialogRef.close(track);
|
|
|
+ }
|
|
|
+
|
|
|
+ public search(event: SubmitEvent): void {
|
|
|
+ this.searchParams = new TrackSearchParams();
|
|
|
+ this.searchParams.filter = this.nameFilter ? this.nameFilter : undefined;
|
|
|
+ this.updateTracks();
|
|
|
+ }
|
|
|
+
|
|
|
+ public loadMore(): void {
|
|
|
+ this.searchParams.page++;
|
|
|
+ this.updateTracks();
|
|
|
+ }
|
|
|
+
|
|
|
+ private updateTracks(): void {
|
|
|
+ this.tracksApi.getTracks(this.searchParams)
|
|
|
+ .then(trackPage => {
|
|
|
+ if (this.searchParams.page === 0) {
|
|
|
+ this._tracks = trackPage.items;
|
|
|
+ } else {
|
|
|
+ this._tracks.push(...trackPage.items);
|
|
|
+ }
|
|
|
+ this.hasMore = trackPage.hasMore;
|
|
|
+ this.tracks.next(this._tracks);
|
|
|
+ });
|
|
|
+ }
|
|
|
+}
|