-
Notifications
You must be signed in to change notification settings - Fork 6
/
favoritesController.jsx
57 lines (51 loc) · 1.93 KB
/
favoritesController.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
import React from "react";
import { Component } from "react";
import GetFavorites from "../UserDetails/getFavorites";
import FavoriteComponent from "../Components/favoriteComponent";
/**
* Class component that fetches and displays list of all Sonos favorites in selected household
*/
class FavoritesController extends Component {
/**
* @param props.museClientConfig {JSON} Contains Sonos API access token and configuration
* @param props.householdId {string} Used to target current household when fetching favorites
* @param props.groupId {string} Used to target current group when loading favorites
*/
constructor(props) {
super(props);
// fetchFlag = true causes favorites to be fetched on instantiation
this.state = {fetchFlag: true, favorites: []};
}
/**
* Handler function that updates the array of favorites in this.state
* @param favorites {Array} Array of JSON objects each containing the information of a favorite in current household
*/
favoritesHandler = (favorites) => {
// Fetch flag = false stops fetching of favorites from Sonos API
this.setState({fetchFlag: false, favorites:favorites});
}
render() {
return (
<div className="favorites_list">
{/* Upon instantiation, fetches favorites from Sonos API and sets fetchFlag to false */}
{this.state.fetchFlag && (
<GetFavorites
museClientConfig={this.props.museClientConfig}
householdId={this.props.householdId}
favoritesHandler={this.favoritesHandler}
/>)}
{/* Once favorites have been fetched, a button is created for each favorite */}
{!this.state.fetchFlag && (
this.state.favorites.map((item) => {
return (<FavoriteComponent
key={item.id}
state={item}
groupId={this.props.groupId}
/>)
})
)}
</div>
);
}
}
export default FavoritesController;