Commit 7b10b127 authored by Andrew Hrdy's avatar Andrew Hrdy
Browse files

Removed unnecessary API calls because isOpen logic is being handled on the client side.

parent 9a2d2d10
......@@ -18,53 +18,20 @@ export const getFacilities = () => dispatch => {
type: GET_FACILITIES
});
//A request for all the open facilities
const requestOpen = new Request('https://api.srct.gmu.edu/whatsopen/v2/facilities/?open_now=True&format=json', {
const request = new Request('https://api.srct.gmu.edu/whatsopen/v2/facilities/', {
method: 'GET'
});
//A request for all the closed facilities
const requestClosed = new Request('https://api.srct.gmu.edu/whatsopen/v2/facilities/?closed_now=True&format=json', {
method: 'GET'
});
/**
* Merges the two promises (returned from the fetch operations) in order to dispatch only when both the
* open and closed requests are completed.
*/
return Promise.all([
fetch(requestOpen)
.then(res => {
if (res.status < 200 || res.status >= 300) {
throw new Error(res.statusText);
}
return res.json();
}),
fetch(requestClosed)
.then(res => {
if (res.status < 200 || res.status >= 300) {
throw new Error(res.statusText);
}
return res.json();
})])
.then(facilitiesByStatus => { //facilitiesByStatus is in the format: [[openFacilities], [closedFacilities]]
return fetch(request)
.then(res => {
if (res.status < 200 || res.status >= 300) {
throw new Error(res.statusText);
}
/**
* Iterates over the open and closed facility arrays and adds the isOpen property which drives styling
* in the view.
*/
facilitiesByStatus[0].forEach(openFacility => {
openFacility.isOpen = true;
});
facilitiesByStatus[1].forEach(closedFacility => {
closedFacility.isOpen = false;
});
//Merges the two facility status arrays and sorts by alphabetical order.
const allFacilities = facilitiesByStatus[0].concat(facilitiesByStatus[1])
.sort((a, b) => a.facility_name > b.facility_name ? 1 : -1);
dispatch(setFacilities(JSON.stringify(allFacilities)));
return res.json();
})
.then(json => {
dispatch(setFacilities(JSON.stringify(json)));
});
};
......
......@@ -56,6 +56,11 @@ class Layout extends React.Component {
}
componentWillMount() {
/*
This is done in order to immediately load the page (retrieving from local storage is faster
than an API call). After retrieving from local storage, then call the API to see if there
are any updates.
*/
if(localStorage.getItem('facilities')){
const facilities = localStorage.getItem('facilities');
this.props.setFacilities(facilities)
......@@ -77,7 +82,7 @@ class Layout extends React.Component {
<div className={classes.mainContent}>
<SearchBar styles={styleSheet.searchBar} suggestions={suggestions}/>
<div className={classes.cardContainer}>
<CardContainer styles={styleSheet.cardContainer}searchTerm={this.props.searchTerm} facilities={this.props.facilities}/>
<CardContainer styles={styleSheet.cardContainer} searchTerm={this.props.searchTerm} facilities={this.props.facilities}/>
</div>
</div>
<div className={classes.sidebarToggleContainer}>
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment