Advertisement
Guest User

Untitled

a guest
Sep 18th, 2019
421
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.53 KB | None | 0 0
  1. import React, {Component} from 'react';
  2. import logo from './logo.svg';
  3. import './App.css';
  4. import Spotify from 'spotify-web-api-js';
  5.  
  6. const spotifyWebApi = new Spotify();
  7.  
  8. class App extends Component{
  9. constructor(){
  10. super();
  11. const params = this.getHashParams();
  12. this.state ={
  13. loggedIn: params.access_token ? true : false,
  14. nowPlaying: {
  15. name: 'Not Checked',
  16. image: '',
  17. },
  18. data: resoponseArray.map(item => {description: item.description, timeM: item.timeM}),
  19. playlists:{
  20. name: 'Not Checked',
  21. id: 'Not Checked'
  22. }
  23. }
  24. if (params.access_token){
  25. spotifyWebApi.setAccessToken(params.access_token);
  26. }
  27. }
  28. /**
  29. * Obtains parameters from the hash of the URL
  30. * @return Object
  31. */
  32. getHashParams() {
  33. var hashParams = {};
  34. var e, r = /([^&;=]+)=?([^&;]*)/g,
  35. q = window.location.hash.substring(1);
  36. while ( e = r.exec(q)) {
  37. hashParams[e[1]] = decodeURIComponent(e[2]);
  38. }
  39. return hashParams;
  40. }
  41.  
  42. getNowPlaying(){
  43. spotifyWebApi.getMyCurrentPlaybackState()
  44. .then((response) => {
  45. this.setState({
  46. nowPlaying: {
  47. name:response.item.name,
  48. image: response.item.album.images[0].url
  49. }
  50. })
  51. })
  52. }
  53.  
  54. getUserPlaylists(){
  55. spotifyWebApi.getUserPlaylists('escher8')
  56. .then((response) => {
  57. console.log('Playlists', response);
  58.  
  59. this.setState({
  60. playlists: {
  61. name: response.items[0].name,
  62. id: response.items[0].id
  63. }
  64. })
  65.  
  66. spotifyWebApi.getPlaylist('7K0UB4wqdztK4jc3nrw9an')
  67. .then((response) => {
  68. console.log('PlaylIST', response);
  69. })
  70. })
  71. }
  72.  
  73. render(){
  74. return (
  75. <div className="App">
  76. <a href = "http://localhost:8888">
  77. <button>Login with Spotify</button>
  78. </a>
  79.  
  80. <div> Now Playing: {this.state.nowPlaying.name}</div>
  81. <div>
  82. <img src={this.state.nowPlaying.image } style={{width:100}}/>
  83. </div>
  84. <button onClick={() => this.getNowPlaying()}>
  85. Check Now Playing
  86. </button>
  87.  
  88. <div> Playlists: {this.state.playlists.name} (id: {this.state.playlists.id})</div>
  89. <button onClick={() => this.getUserPlaylists()}>
  90. Get Playlists
  91. </button>
  92.  
  93. </div>
  94. );
  95. }
  96. }
  97.  
  98. export default App;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement