-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathApp.jsx
149 lines (129 loc) · 3.15 KB
/
App.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import './App.css'
import { FormControl, InputGroup, Container, Button, Card, Row } from 'react-bootstrap'
import { useState, useEffect } from 'react'
const clientId = import.meta.env.VITE_CLIENT_ID;
const clientSecret = import.meta.env.VITE_CLIENT_SECRET;
function App() {
const [searchInput, setSearchInput] = useState("")
const [accessToken, setAccessToken] = useState("")
const [albums, setAlbums] = useState([])
useEffect(() => {
var authParams = {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: 'grant_type=client_credentials&client_id=' + clientId + '&client_secret=' + clientSecret,
}
fetch('https://accounts.spotify.com/api/token', authParams)
.then(result => result.json())
.then(data => {
setAccessToken(data.access_token)
})}, [])
async function search() {
var artistParams = {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + accessToken,
},
}
// Get Artist
var artistID = await fetch('https://api.spotify.com/v1/search?q=' + searchInput + '&type=artist', artistParams)
.then(result => result.json())
.then(data => {
return data.artists.items[0].id
})
// Get Artist Albums
await fetch('https://api.spotify.com/v1/artists/' + artistID + '/albums?include_groups=album&market=US&limit=50', artistParams)
.then(result => result.json())
.then(data => {
setAlbums(data.items)
})
}
return (
<>
<Container style={{
marginBottom: '30px',
}}>
<InputGroup>
<FormControl
placeholder="Search For Artist"
type='input'
aria-label="Search for an Artist"
onKeyDown={event => {
if (event.key === "Enter") {
search()
}
}}
onChange={event => setSearchInput(event.target.value)}
style={{
width: '300px',
height: '35px',
borderWidth: '0px',
borderStyle: 'solid',
borderRadius: '5px',
marginRight: '10px',
paddingLeft: '10px'
}}
/>
<Button onClick={search}>
Search
</Button>
</InputGroup>
</Container>
<Container>
<Row style={{
display: 'flex',
flexDirection: 'row',
flexWrap: 'wrap',
justifyContent: 'space-around',
alignContent: 'center',
}}>
{albums.map((album) => {
return(
<Card key={album.id} style={{
backgroundColor: 'white',
margin: '10px',
borderRadius: '5px',
marginBottom: '30px',
}} >
<Card.Img
width={200}
src={album.images[0].url}
style={{
borderRadius: '4%',
}} />
<Card.Body>
<Card.Title style={{
whiteSpace: 'wrap',
fontWeight: 'bold',
maxWidth: '200px',
fontSize: '18px',
marginTop: '10px',
color: 'black'
}}>{album.name}</Card.Title>
<Card.Text style={{
color: 'black'
}}>
Release Date: <br/> {album.release_date}
</Card.Text>
<Button href={album.external_urls.spotify} style={{
backgroundColor: 'black',
color: 'white',
fontWeight: 'bold',
fontSize: '15px',
borderRadius: '5px',
padding: '10px',
}}>Album Link
</Button>
</Card.Body>
</Card>
)
})}
</Row>
</Container>
</>
)
}
export default App