import Service from '@ember/service' ; import { task } from 'ember-concurrency-decorators' ; import fetch from 'fetch' ; import ENV from 'client/config/environment' ; import SerializeTrack from 'client/src/serializers/track' ; import Track from 'client/src/pojo/track' ; export default class DataPlaylistsService extends Service { host : string = ENV . apiServer ; endpoint : string = '/api/spotify/v1/playlists/' ; title : string = '' ; description : string = '' ; image : any = null ; tracks : Track [ ] = [ ] ; /**
    * load a playlist from spotify
    */ load ( id ) { return this . _load . perform ( id ) ; } @ task * _load ( id ) { const response = yield fetch ( this . host + this . endpoint + id ) ; const { data } = yield response . json ( ) ; const tracksRaw = data . table . tracks . items ; const tracks = tracksRaw . map ( SerializeTrack ) . filter ( ( item ) => item . available ) ; this . setProperties ( { title : data . table . name , description : data . table . description , image : data . table . images [ 0 ] . url } ) ; this . tracks . clear ( ) ; this . tracks . pushObjects ( tracks ) ; return { title : this . title , image : this . image , description : this . description , tracks } ; } } declare module '@ember/service' { interface Registry { 'data/playlists' : DataPlaylistsService ; } }