VanillaWulf / jam

0 stars 0 forks source link

Formatting #1

Open Default4152 opened 6 years ago

Default4152 commented 6 years ago

Simplify - I recommend using a style guide to format your code in a way that improves the readability and organization.

We can make improvements to the indentation, semicolon usage and spacing by following a javascript style guide like this one from airbnb: https://github.com/airbnb/javascript

For example, Spotify.js can benefit from formatting changes:

//import client from '../secret/secret.js';
let accessToken;
const clientId = '';
//const clientId = client.getClient();
/*const clientId = process.env.SPOTIFY_SECRET;*/
const redirectUri = 'http://localhost:3000/';

let expiresIn;

const Spotify = {

  getAccessToken() {
    if (accessToken) {
      return accessToken;
    }
    const urlAccessToken = window.location.href.match(/access_token=([^&]*)/);
    const urlExpiresIn = window.location.href.match(/expires_in=([^&]*)/);
    if (urlAccessToken && urlExpiresIn) {
      accessToken = urlAccessToken[1];
      expiresIn = urlExpiresIn[1];
      window.setTimeout(() => accessToken = '', expiresIn * 1000);
      window.history.pushState('Access Token', null, '/');
      console.log('get token success');
    } else {
      window.location.href = `https://accounts.spotify.com/authorize?client_id=${clientId}&response_type=token&scope=playlist-modify-private&redirect_uri=${redirectUri}`;
    }

  },

  search(term) {
    return fetch(`https://api.spotify.com/v1/search?type=track&q=${term}`, {
        headers: {
          Authorization: `Bearer ${accessToken}`
        }
      })
      .then(response => response.json())
      .then(jsonResponse => {
        if (jsonResponse.tracks) {
          return jsonResponse.tracks.items.map(function (track) {
            return {
              id: track.id,
              name: track.name,
              uri: track.uri,
              album: track.album.name,
              artist: track.artists[0].name
            }
          })
        } else {
          return [];
        }
      });
  },

  handleResponse(response) {
    if (response.ok) {
      return response.json();
    }
    throw new Error('Request failed!');
  },

  savePlaylist(name, trackUri) {
    {
      return fetch('https://api.spotify.com/v1/me', {
          headers: {
            Authorization: `Bearer ${accessToken}`
          }
        })
        .then(response => response.json())
        .then(jsonResponse => {
          let userId = jsonResponse.id;
          return this.createPlaylist(userId, name, trackUri);
        });
    }

  },

  createPlaylist(userId, playlistName, playlistTracks) {
    return fetch(`https://api.spotify.com/v1/users/${userId}/playlists/`, {
        headers: {
          Authorization: `Bearer ${accessToken}`
        },
        method: 'POST',
        body: JSON.stringify({
          name: playlistName,
          public: false
        })
      })
      .then(response => this.handleResponse(response))
      .then(jsonResponse => {
        let playlistId = jsonResponse.id;
        return this.saveTrack(userId, playlistId, playlistTracks);
      });
  },

  saveTrack(userId, playlistId, playlistTracks) {
    return fetch(`https://api.spotify.com/v1/users/${userId}/playlists/${playlistId}/tracks`, {
        headers: {
          Authorization: `Bearer ${accessToken}`
        },
        method: 'POST',
        body: JSON.stringify(playlistTracks)
      })
      .then(response => this.handleResponse(response))
      .then(jsonResponse => {
        return jsonResponse.snapshot_id;
      });
  }
}

export default Spotify;
VanillaWulf commented 6 years ago

got it, will fix in next commit