feat: new track class

This commit is contained in:
Mateusz Pstrucha committed 2021-09-11 12:24:30 +02:00
1 parent 3457b6e308
commit 3597095275
1 file changed
+53 -3
+53 -3
View File
@@ -2,10 +2,60 @@ import ky from 'ky'
var tracks = {}
class Track {
constructor(tdata) {
Object.assign(this, tdata)
}
get listen() {
return `https://open.spotify.com/track/${this.id}`
}
get share() {
return `https://radio-rolnik.mtps.pl/utwor/${this.id}`
}
setVote(value) {
return new Promise((resolve, reject) => {
ky.get(`/api/vote/${this.id}/${value}`)
.json()
.then((ok) => {
if (ok) {
this.votes.set = value
}
resolve(ok)
})
.catch((err) => reject(err))
})
}
getVotes() {
return new Promise((resolve, reject) => {
getVotes(this.id).then((votes) => {
this.votes = votes
resolve(votes)
})
})
}
}
function getVotes(id) {
return new Promise((resolve, reject) => {
ky.get(`/api/track/${id}?votes`)
.json()
.then((votes) => {
resolve(votes)
})
.catch((err) => reject(err))
})
}
function get(id) {
return new Promise((resolve, reject) => {
if (typeof tracks[id] === 'undefined') {
download(id).then((track) => resolve(track))
download(id)
.then((track) => resolve(track))
.catch((err) => reject(err))
} else resolve(tracks[id])
})
}
@@ -21,8 +71,8 @@ function download(id) {
ky.get('/api/track/' + id)
.json()
.then((track) => {
tracks[track.id] = track
resolve(track)
tracks[track.id] = new Track(track)
resolve(tracks[track.id])
})
.catch((err) => reject(err))
})