-
Notifications
You must be signed in to change notification settings - Fork 5
/
playerVolumeSubscribe.js
37 lines (31 loc) · 1.19 KB
/
playerVolumeSubscribe.js
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
import Helper from "../Utility/helper";
import { useEffect } from "react";
/**
* Functional component that subscribes to a player's volume change events
* Unsubscribes on unmounting of component
* @param props.playerId {string} Used to target specific player in Sonos API calls
*/
export default function PlayerVolumeSubscribe(props) {
// Used to make API calls
const helper = new Helper();
useEffect(() => {
// Player subscription URL
const endPoint = helper.getPlayersURL() + props.playerId + "/playerVolume/subscription";
// Contains access token and API response format specifier
const headers = helper.getHeaderBearer();
// Data sent to Sonos API (no data needed for subscriptions)
const data = {};
// Calls Sonos API to subscribe to player volume events for specified player
helper.apiCall(endPoint, headers, "POST", data)
.catch(function (error) {
console.error(error);
});
// When component is unmounted, it unsubscribes player volume events for the specified player
return () => {
helper.apiCall(endPoint, headers, "DELETE", data)
.catch(function (error) {
console.error(error);
});
}
}, []);
}