-
Notifications
You must be signed in to change notification settings - Fork 6
/
fetchHouseholdsController.jsx
57 lines (51 loc) · 1.83 KB
/
fetchHouseholdsController.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 { Configuration } from "../museClient/configuration";
import GetHouseholds from "../UserDetails/getHouseholds";
import ListHouseholdsComponent from "../Components/listHouseholdsComponent";
/**
* Class component that fetches and displays list of all households associated with user
*/
class FetchHouseholds extends Component {
// Default value for householdFlag causes households to be fetched on instantiation
state = {
householdFlag: true,
households: null,
};
/**
* Handler function to update state of FetchHouseholds. Passed through props to GetHouseholds
* @param householdsResponse {Array} List of households fetched from Sonos API
*/
hh_handler = (householdsResponse) => {
this.setState({
householdFlag: false,
households: householdsResponse,
});
};
render() {
// Contains access token needed for Sonos API calls
const museClientConfig = new Configuration({
accessToken: JSON.parse(window.localStorage.accessToken).token,
});
// First calls GetHouseholds, which updates this.state to contain a list of households associated with user and sets state.householdFlag to false
// GetHouseholds then is unmounted and ListHouseholdsComponent is instantiated, which displays a button for each household
return (
<div>
<div className="getHouseholdID">
{this.state.householdFlag && (
<GetHouseholds
hh_handler={this.hh_handler}
museClientConfig={museClientConfig}
/>
)}
</div>
<div>
{!this.state.householdFlag && (
<ListHouseholdsComponent households={this.state.households} museClientConfig={museClientConfig}/>
)}
</div>
</div>
);
}
}
export default FetchHouseholds;