Skip to content

Commit

Permalink
Adds 204 status
Browse files Browse the repository at this point in the history
  • Loading branch information
Cher Stewart committed Feb 26, 2019
1 parent ca70eee commit 412ebdb
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 25 deletions.
17 changes: 7 additions & 10 deletions api/index.js
Expand Up @@ -145,20 +145,17 @@ app.get('/spotify/now-playing/', async (req, res) => {
}
}
)
if (res.status !== 204) {
setLastPlayed(access_token, response.data)
res.send(response.data)
} else {
const reply = await callStorage('get', 'last_played')
res.send({ data: { item: reply, is_playing: false } })
}
const { data } = response
setLastPlayed(access_token, data)
const reply = await callStorage('get', 'last_played')
res.send({ item: JSON.parse(reply), is_playing: Boolean(data.is_playing) })
} catch (err) {
console.error(err)
res.send({ error: err.message })
}
})

async function setLastPlayed(access_token, { item }) {
async function setLastPlayed(access_token, item) {
if (!Boolean(item)) {
const { data } = await axios.get(
`${spotifyBaseUrl}me/player/recently-played?market=US`,
Expand All @@ -169,9 +166,9 @@ async function setLastPlayed(access_token, { item }) {
}
}
)
postStoredTrack(data.items[0])
postStoredTrack(data.items[0].track)
} else {
postStoredTrack(item)
postStoredTrack(item.item)
}
}

Expand Down
18 changes: 3 additions & 15 deletions store/index.js
Expand Up @@ -35,28 +35,16 @@ export const actions = {
async nuxtServerInit({ commit }) {
try {
const redisUrl = `${clientUrl}/api/spotify/data/`
const promises = [
axios.get(`${redisUrl}is_connected`),
axios.get(`${redisUrl}last_played`)
]
const [
{
data: { is_connected }
},
{
data: { last_played }
}
] = await Promise.all(promises)
const {
data: { is_connected }
} = await axios.get(`${redisUrl}is_connected`)

commit('connectionChange', is_connected)
if (Boolean(last_played))
commit('recentlyPlayedChange', JSON.parse(last_played))

if (Boolean(is_connected)) {
const {
data: { item, is_playing }
} = await axios.get(`${clientUrl}/api/spotify/now-playing`)
console.log(is_playing)
commit('nowPlayingChange', item)
commit('isPlayingChange', is_playing)
}
Expand Down

0 comments on commit 412ebdb

Please sign in to comment.