Skip to content

Commit

Permalink
Merge pull request #66 from vakst/fetch.playback.state.on.inactive.de…
Browse files Browse the repository at this point in the history
…vice

fixes #65 E019: Failed to execute
  • Loading branch information
brandonsaldan authored Dec 19, 2024
2 parents 08a6228 + 12ab18c commit cbcb09a
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 85 deletions.
19 changes: 2 additions & 17 deletions src/pages/album/[albumId].jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { useEffect, useState, useRef, useCallback } from "react";
import LongPressLink from "../../components/LongPressLink";
import Image from "next/image";
import { getCurrentDevice } from "@/services/deviceService";
import { setPlaybackShuffleState } from "@/services/playerService";

export const runtime = "experimental-edge";

Expand Down Expand Up @@ -63,23 +64,7 @@ const AlbumPage = ({
}, [isShuffleEnabled]);

useEffect(() => {
const fetchPlaybackState = async () => {
try {
const response = await fetch("https://api.spotify.com/v1/me/player", {
headers: {
Authorization: `Bearer ${accessToken}`,
},
});
if (response.ok) {
const data = await response.json();
setIsShuffleEnabled(data.shuffle_state);
}
} catch (error) {
handleError("FETCH_PLAYBACK_STATE_ERROR", error.message);
}
};

fetchPlaybackState();
void setPlaybackShuffleState(accessToken, handleError, setIsShuffleEnabled);
}, [accessToken]);

const loadMoreTracks = async () => {
Expand Down
19 changes: 2 additions & 17 deletions src/pages/artist/[artistId].jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { useEffect, useState } from "react";
import LongPressLink from "../../components/LongPressLink";
import Image from "next/image";
import { getCurrentDevice } from "@/services/deviceService";
import {setPlaybackShuffleState} from "@/services/playerService";

export const runtime = "experimental-edge";

Expand Down Expand Up @@ -42,23 +43,7 @@ const ArtistPage = ({
}, [isShuffleEnabled]);

useEffect(() => {
const fetchPlaybackState = async () => {
try {
const response = await fetch("https://api.spotify.com/v1/me/player", {
headers: {
Authorization: `Bearer ${accessToken}`,
},
});
if (response.ok) {
const data = await response.json();
setIsShuffleEnabled(data.shuffle_state);
}
} catch (error) {
handleError("FETCH_PLAYBACK_STATE_ERROR", error.message);
}
};

fetchPlaybackState();
void setPlaybackShuffleState(accessToken, handleError, setIsShuffleEnabled);
}, [accessToken]);

const playArtistTopTracks = async () => {
Expand Down
19 changes: 2 additions & 17 deletions src/pages/collection/tracks.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import LongPressLink from "../../components/LongPressLink";
import Image from "next/image";
import SuccessAlert from "../../components/SuccessAlert";
import { getCurrentDevice } from "@/services/deviceService";
import {setPlaybackShuffleState} from "@/services/playerService";
export const runtime = "experimental-edge";

const LikedSongsPage = ({
Expand Down Expand Up @@ -97,23 +98,7 @@ const LikedSongsPage = ({
);

useEffect(() => {
const fetchPlaybackState = async () => {
try {
const response = await fetch("https://api.spotify.com/v1/me/player", {
headers: {
Authorization: `Bearer ${accessToken}`,
},
});
if (response.ok) {
const data = await response.json();
setIsShuffleEnabled(data.shuffle_state);
}
} catch (error) {
handleError("FETCH_PLAYBACK_STATE_ERROR", error.message);
}
};

fetchPlaybackState();
void setPlaybackShuffleState(accessToken, handleError, setIsShuffleEnabled);
}, [accessToken]);

useEffect(() => {
Expand Down
19 changes: 2 additions & 17 deletions src/pages/mix/[mixId].jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import Image from "next/image";
import SuccessAlert from "../../components/SuccessAlert";
import { fetchUserRadio } from "../../services";
import { getCurrentDevice } from "@/services/deviceService";
import {setPlaybackShuffleState} from "@/services/playerService";
export const runtime = "experimental-edge";

const MixPage = ({
Expand Down Expand Up @@ -98,23 +99,7 @@ const MixPage = ({
}, [mix]);

useEffect(() => {
const fetchPlaybackState = async () => {
try {
const response = await fetch("https://api.spotify.com/v1/me/player", {
headers: {
Authorization: `Bearer ${accessToken}`,
},
});
if (response.ok) {
const data = await response.json();
setIsShuffleEnabled(data.shuffle_state);
}
} catch (error) {
return;
}
};

fetchPlaybackState();
void setPlaybackShuffleState(accessToken, handleError, setIsShuffleEnabled);
}, [accessToken]);

const playMix = async () => {
Expand Down
19 changes: 2 additions & 17 deletions src/pages/playlist/[playlistId].jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import LongPressLink from "../../components/LongPressLink";
import Image from "next/image";
import SuccessAlert from "../../components/SuccessAlert";
import { getCurrentDevice } from "@/services/deviceService";
import {setPlaybackShuffleState} from "@/services/playerService";
export const runtime = "experimental-edge";

const PlaylistPage = ({
Expand Down Expand Up @@ -167,23 +168,7 @@ const PlaylistPage = ({
}, [playlist]);

useEffect(() => {
const fetchPlaybackState = async () => {
try {
const response = await fetch("https://api.spotify.com/v1/me/player", {
headers: {
Authorization: `Bearer ${accessToken}`,
},
});
if (response.ok) {
const data = await response.json();
setIsShuffleEnabled(data.shuffle_state);
}
} catch (error) {
return;
}
};

fetchPlaybackState();
void setPlaybackShuffleState(accessToken, handleError, setIsShuffleEnabled);
}, [accessToken]);

const loadMoreTracks = async () => {
Expand Down
27 changes: 27 additions & 0 deletions src/services/playerService.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
export const setPlaybackShuffleState = async (
accessToken,
handleError,
setIsShuffleEnabled
) => {
try {
const response = await fetch("https://api.spotify.com/v1/me/player", {
headers: {
Authorization: `Bearer ${accessToken}`,
},
});
if (response.ok) {
const data = await response.text();
if (data.length > 0) {
try {
const parsed = JSON.parse(JSON.stringify(data));
setIsShuffleEnabled(parsed.shuffle_state);
} catch (error) {
handleError("FETCH_PLAYBACK_STATE_ERROR", error.message);
}
}
}
} catch (error) {
handleError("FETCH_PLAYBACK_STATE_ERROR", error.message);
}

}

0 comments on commit cbcb09a

Please sign in to comment.