savePlaylist(playlistName, trackUris) {
    if (!playlistName || !trackUris) {
      return;
    }
    accessToken = Spotify.getAccessToken();
    let userId;
    return fetch('https://api.spotify.com/v1/me', {
      headers: { Authorization: `Bearer ${accessToken}` },
    })
      .then((response) => response.json())
      .then((jsonResponse) => {
        userId = jsonResponse.id;

        return fetch(`https://api.spotify.com/v1/users/${userId}/playlists`, {
          headers: { Authorization: `Bearer ${accessToken}` },
          method: 'POST',
          body: JSON.stringify({ name: playlistName }),
        })
          .then(
            (response) => {
              if (response.ok) {
                return response.json();
              }
              throw new Error('Request Failed');
            },
            (networkError) => console.log(networkError.message)
          )
          .then((jsonResponse) => {
            let playlistId = jsonResponse.id;
            return fetch(
              `https://api.spotify.com/v1/users/${userId}/playlists/${playlistId}/tracks`,
              {
                headers: { Authorization: `Bearer ${accessToken}` },
                method: 'POST',
                body: JSON.stringify({ uris: trackUris }),
              }
            )
              .then(
                (response) => {
                  if (response.ok) {
                    return response.json();
                  }
                  throw new Error('Request Failed');
                },
                (networkError) => console.log(networkError.message)
              )
              .then((jsonResponse) => {
                playlistId = jsonResponse.id;
              });
          });
      });
  }
//Takes our Saved Playlist and exports it to the users personal Spotify.
Takes our Saved Playlist and exports it to the users personal Spotify.
Takes our Saved Playlist and exports it to the users personal Spotify .
savePlaylist(playlistName, trackUris) {
    if (!playlistName || !trackUris) {
      return;
    }
    accessToken = Spotify.getAccessToken();
    let userId;
    return fetch('https://api.spotify.com/v1/me', {
      headers: { Authorization: `Bearer ${accessToken}` },
    })
      .then((response) => response.json())
      .then((jsonResponse) => {
        userId = jsonResponse.id;
        return fetch(`https://api.spotify.com/v1/users/${userId}/playlists`, {
          headers: { Authorization: `Bearer ${accessToken}` },
          method: 'POST',
          body: JSON.stringify({ name: playlistName }),
        })
          .then(
            (response) => {
              if (response.ok) {
                return response.json();
              }
              throw new Error('Request Failed');
            },
            (networkError) => console.log(networkError.message)
          )
          .then((jsonResponse) => {
            let playlistId = jsonResponse.id;
            return fetch(
              `https://api.spotify.com/v1/users/${userId}/playlists/${playlistId}/tracks`,
              {
                headers: { Authorization: `Bearer ${accessToken}` },
                method: 'POST',
                body: JSON.stringify({ uris: trackUris }),
              }
            )
              .then(
                (response) => {
                  if (response.ok) {
                    return response.json();
                  }
                  throw new Error('Request Failed');
                },
                (networkError) => console.log(networkError.message)
              )
              .then((jsonResponse) => {
                playlistId = jsonResponse.id;
              });
          });
      });
  }
savePlaylist ( playlistName , trackUris ) { if ( ! playlistName || ! trackUris ) { return ; } accessToken = Spotify . getAccessToken ( ) ; let userId ; return fetch ( 'https://api.spotify.com/v1/me' , { headers : { Authorization : ` ${ accessToken } ` } , } ) . then ( ( response ) => response . json ( ) ) . then ( ( jsonResponse ) => { userId = jsonResponse . id ; return fetch ( ` ${ userId } ` , { headers : { Authorization : ` ${ accessToken } ` } , method : 'POST' , body : JSON . stringify ( { name : playlistName } ) , } ) . then ( ( response ) => { if ( response . ok ) { return response . json ( ) ; } throw new Error ( 'Request Failed' ) ; } , ( networkError ) => console . log ( networkError . message ) ) . then ( ( jsonResponse ) => { let playlistId = jsonResponse . id ; return fetch ( ` ${ userId } ${ playlistId } ` , { headers : { Authorization : ` ${ accessToken } ` } , method : 'POST' , body : JSON . stringify ( { uris : trackUris } ) , } ) . then ( ( response ) => { if ( response . ok ) { return response . json ( ) ; } throw new Error ( 'Request Failed' ) ; } , ( networkError ) => console . log ( networkError . message ) ) . then ( ( jsonResponse ) => { playlistId = jsonResponse . id ; } ) ; } ) ; } ) ; }
Takes our Saved Playlist and exports it to the users personal Spotify.
Takes our Saved Playlist and exports it to the users personal Spotify .