2019-12-13 15:53:32 -05:00
|
|
|
import React, { useState, useEffect } from 'react'
|
2019-12-14 18:05:19 -05:00
|
|
|
import SpotifyWebApi from 'spotify-web-api-js'
|
|
|
|
import Artist from '../components/Artist'
|
2019-12-15 18:49:30 -05:00
|
|
|
import history from '../history.js'
|
2019-12-14 18:05:19 -05:00
|
|
|
import './ArtistSearch.scss'
|
|
|
|
|
|
|
|
const spotifyApi = new SpotifyWebApi()
|
2019-12-13 15:53:32 -05:00
|
|
|
|
2019-12-15 18:49:30 -05:00
|
|
|
function ArtistSearch({ props }) {
|
2019-12-14 12:32:28 -05:00
|
|
|
let [search, setSearch] = useState(null)
|
2019-12-14 18:05:19 -05:00
|
|
|
let [artists, setArtists] = useState(null)
|
2019-12-15 23:10:25 -05:00
|
|
|
let [token, setToken] = useState(null)
|
2019-12-13 15:53:32 -05:00
|
|
|
|
2019-12-15 23:10:25 -05:00
|
|
|
// Retrieve access token from url
|
2019-12-13 15:53:32 -05:00
|
|
|
useEffect(() => {
|
|
|
|
const search = window.location.search
|
|
|
|
const params = new URLSearchParams(search)
|
2019-12-14 18:05:19 -05:00
|
|
|
const token = params.get('access_token')
|
|
|
|
|
2019-12-15 23:10:25 -05:00
|
|
|
setToken(token)
|
2019-12-14 18:05:19 -05:00
|
|
|
spotifyApi.setAccessToken(token)
|
2019-12-13 15:53:32 -05:00
|
|
|
}, [])
|
|
|
|
|
2019-12-15 23:10:25 -05:00
|
|
|
// Search artists
|
2019-12-14 12:32:28 -05:00
|
|
|
useEffect(() => {
|
2019-12-14 18:05:19 -05:00
|
|
|
if (search && search.length > 4) {
|
|
|
|
spotifyApi.searchArtists(search)
|
|
|
|
.then(function(data) {
|
|
|
|
setArtists(data.artists.items)
|
|
|
|
}, function(err) {
|
|
|
|
console.error(err);
|
|
|
|
})
|
|
|
|
}
|
2019-12-14 12:32:28 -05:00
|
|
|
}, [search])
|
|
|
|
|
2019-12-15 23:10:25 -05:00
|
|
|
// Load search term from props
|
2019-12-15 18:49:30 -05:00
|
|
|
useEffect(() => {
|
|
|
|
setSearch(props.searchTerm)
|
|
|
|
}, [props.searchTerm])
|
|
|
|
|
2019-12-15 23:10:25 -05:00
|
|
|
// Save token in Router state
|
|
|
|
useEffect(() => {
|
|
|
|
props.tokenCb(token)
|
|
|
|
}, [props, token])
|
|
|
|
|
|
|
|
// Save search term
|
2019-12-14 12:32:28 -05:00
|
|
|
const handleChange = e => {
|
|
|
|
setSearch(e.currentTarget.value)
|
2019-12-15 18:49:30 -05:00
|
|
|
props.searchCb(e.currentTarget.value)
|
|
|
|
}
|
|
|
|
|
2019-12-15 23:10:25 -05:00
|
|
|
// Save artist id in Router state
|
2019-12-15 18:49:30 -05:00
|
|
|
const albumsCallback = id => {
|
|
|
|
props.idCb(id)
|
|
|
|
history.push('/albums')
|
2019-12-14 12:32:28 -05:00
|
|
|
}
|
|
|
|
|
2019-12-13 15:53:32 -05:00
|
|
|
return (
|
|
|
|
<div className="artist-search">
|
|
|
|
<h1>Artist search</h1>
|
|
|
|
|
2019-12-14 12:32:28 -05:00
|
|
|
<input placeholder='Search for an artist' onChange={handleChange} />
|
2019-12-14 18:05:19 -05:00
|
|
|
|
|
|
|
{artists && (
|
|
|
|
<div className='artists'>
|
|
|
|
<ul className='list'>
|
|
|
|
{artists.map((artist, index) => (
|
|
|
|
<li className='list-item' key={index}>
|
2019-12-15 18:49:30 -05:00
|
|
|
<Artist props={artist} callback={albumsCallback} />
|
2019-12-14 18:05:19 -05:00
|
|
|
</li>
|
|
|
|
))}
|
|
|
|
</ul>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
|
2019-12-13 15:53:32 -05:00
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export default ArtistSearch
|