73 lines
2.1 KiB
TypeScript
73 lines
2.1 KiB
TypeScript
import { Component, OnInit, ViewChild } from '@angular/core';
|
|
import { ActivatedRoute, Params } from '@angular/router';
|
|
import { Location } from '@angular/common';
|
|
|
|
import { ElsService } from './../els.service';
|
|
import { Song } from './../object/song';
|
|
import { Artist } from './../object/artist';
|
|
import { SongTableComponent } from '../song-table/song-table.component';
|
|
|
|
@Component({
|
|
selector: 'app-artist',
|
|
templateUrl: './artist.component.html',
|
|
styleUrls: [ './../album/album.component.css', './../dashboard.component.css', './artist.component.css' ]
|
|
})
|
|
|
|
export class ArtistComponent implements OnInit {
|
|
// Interacte with table to set sortable
|
|
@ViewChild(SongTableComponent) songtable: SongTableComponent;
|
|
|
|
// Prevent useless data load + activate button in interface var
|
|
moreDataAvailable = true;
|
|
|
|
artistName = '';
|
|
songs: Array<Song> = [];
|
|
artist: Artist = new Artist();
|
|
lockLoadData = false;
|
|
|
|
constructor(
|
|
private elsService: ElsService,
|
|
private route: ActivatedRoute,
|
|
private location: Location
|
|
) { }
|
|
|
|
ngOnInit(): void {
|
|
this.route.params.subscribe((params: Params) => this.artistName = params['name']);
|
|
|
|
this.elsService.getArtist(this.artistName).subscribe(data => this.artist = data);
|
|
this.loadSongs();
|
|
}
|
|
|
|
// TODO Duplicate code!
|
|
loadSongs(): void {
|
|
if (this.lockLoadData) {
|
|
console.log('Loading data locked');
|
|
return;
|
|
}
|
|
|
|
if (!this.moreDataAvailable) {
|
|
return;
|
|
}
|
|
|
|
this.lockLoadData = true;
|
|
this.elsService.getArtistSongs(this.artistName, this.songs.length).subscribe(
|
|
data => {
|
|
this.moreDataAvailable = data.length === ElsService.DEFAULT_SIZE;
|
|
|
|
// Erase song array with result for first load, then add elements one by one
|
|
// instead use concat => concat will sort table at each load, very consuming! and not user friendly
|
|
if (this.songs.length === 0) {
|
|
this.songs = data;
|
|
} else {
|
|
this.songtable.setSortable(true);
|
|
data.forEach(song => {
|
|
this.songs.push(song);
|
|
});
|
|
}
|
|
console.log('Unlock load data');
|
|
this.lockLoadData = false;
|
|
}
|
|
);
|
|
}
|
|
}
|