Commit e410e4e7 authored by Mattias J Duffy's avatar Mattias J Duffy
Browse files

Sort by open

parent 1254d190
...@@ -7,4 +7,4 @@ export const SET_CAMPUS_REGION = 'SET_CAMPUS_REGION'; ...@@ -7,4 +7,4 @@ export const SET_CAMPUS_REGION = 'SET_CAMPUS_REGION';
export const ADD_FAVORITE_FACILITY = 'ADD_FAVORITE_FACILITY'; export const ADD_FAVORITE_FACILITY = 'ADD_FAVORITE_FACILITY';
export const REMOVE_FAVORITE_FACILITY = 'REMOVE_FAVORITE_FACILITY'; export const REMOVE_FAVORITE_FACILITY = 'REMOVE_FAVORITE_FACILITY';
export const SET_ALL_FAVORITES = 'SET_ALL_FAVORITES'; export const SET_ALL_FAVORITES = 'SET_ALL_FAVORITES';
export const SORT_BY_FAVORITES = 'SORT_BY_FAVORITES'; export const SORT_FACILITY_CARDS = 'SORT_FACILITY_CARDS';
\ No newline at end of file \ No newline at end of file
import {GET_FACILITIES, SET_FACILITIES, SORT_BY_FAVORITES} from './action-types'; import {GET_FACILITIES, SET_FACILITIES, SORT_FACILITY_CARDS } from './action-types';
const API_GET_FACILITIES = 'https://api.srct.gmu.edu/whatsopen/v2/facilities/'; const API_GET_FACILITIES = 'https://api.srct.gmu.edu/whatsopen/v2/facilities/';
// SHOP MASON ONLY // SHOP MASON ONLY
...@@ -37,6 +37,7 @@ export const setFacilities = (facilities) => { ...@@ -37,6 +37,7 @@ export const setFacilities = (facilities) => {
}; };
}; };
export const sortByFavorites = () => ({ export const sortFacilityCards = () => ({
type: SORT_BY_FAVORITES type: SORT_FACILITY_CARDS
}); });
\ No newline at end of file
...@@ -37,3 +37,4 @@ export const setAllFavorites = (favorites) => ({ ...@@ -37,3 +37,4 @@ export const setAllFavorites = (favorites) => ({
type: SET_ALL_FAVORITES, type: SET_ALL_FAVORITES,
favorites favorites
}); });
...@@ -3,7 +3,7 @@ import {connect} from 'react-redux'; ...@@ -3,7 +3,7 @@ import {connect} from 'react-redux';
// import {setAllFavorites} from '../actions/ui'; // import {setAllFavorites} from '../actions/ui';
import AppBar from '../components/AppBar'; import AppBar from '../components/AppBar';
import Sidebar from '../components/Sidebar'; import Sidebar from '../components/Sidebar';
import {getFacilities, setFacilities, sortByFavorites} from '../actions/api'; import {getFacilities, setFacilities, sortFacilityCards} from '../actions/api';
import {setSidebar, setSelectedFacility, setAllFavorites} from '../actions/ui'; import {setSidebar, setSelectedFacility, setAllFavorites} from '../actions/ui';
import CardContainer from '../components/CardContainer'; import CardContainer from '../components/CardContainer';
...@@ -28,8 +28,7 @@ class Layout extends React.Component { ...@@ -28,8 +28,7 @@ class Layout extends React.Component {
const favorites = JSON.parse(localStorage.getItem('favorites')); const favorites = JSON.parse(localStorage.getItem('favorites'));
this.props.setAllFavorites(favorites); this.props.setAllFavorites(favorites);
} }
this.props.sortFacilityCards();
this.props.sortByFavorites();
} catch (e) { } catch (e) {
console.warn('you should enable cookies so we can remember what places you favorite'); console.warn('you should enable cookies so we can remember what places you favorite');
} }
...@@ -74,7 +73,7 @@ export default connect(mapStateToProps, { ...@@ -74,7 +73,7 @@ export default connect(mapStateToProps, {
getFacilities, getFacilities,
setFacilities, setFacilities,
setAllFavorites, setAllFavorites,
sortByFavorites, sortFacilityCards,
setSidebar, setSidebar,
setSelectedFacility setSelectedFacility,
})(Layout); })(Layout);
import {GET_FACILITIES, SET_FACILITIES, SORT_BY_FAVORITES} from '../actions/action-types'; import {GET_FACILITIES, SET_FACILITIES, SORT_FACILITY_CARDS } from '../actions/action-types';
import cloneDeep from 'lodash/cloneDeep'; import cloneDeep from 'lodash/cloneDeep';
import facilityUtils from '../utils/facilityUtils';
const defaultState = { const defaultState = {
isLoading: false, isLoading: false,
data: [] data: []
}; };
export const facilities = (state = defaultState, action, ui) => { export const facilities = (state = defaultState, action, ui) => {
const sortFunc = (a, b) => { const facilitySort = (a,b) =>{
const favoriteCheck = ui.favorites.includes(b.slug) - ui.favorites.includes(a.slug); const favoriteCheck = ui.favorites.includes(b.slug) - ui.favorites.includes(a.slug);
if (favoriteCheck !== 0) { if (favoriteCheck !== 0) {
return ui.favorites.includes(b.slug) - ui.favorites.includes(a.slug); return favoriteCheck;
}
const openCheck = facilityUtils.isFacilityOpen(b) - facilityUtils.isFacilityOpen(a);
if(openCheck !== 0) {
return openCheck;
} }
if (a.slug < b.slug) { if (a.slug < b.slug) {
...@@ -22,9 +27,11 @@ export const facilities = (state = defaultState, action, ui) => { ...@@ -22,9 +27,11 @@ export const facilities = (state = defaultState, action, ui) => {
return 1; return 1;
} }
return 0; return 0;
}; };
const newData = cloneDeep(state.data);
switch (action.type) { switch (action.type) {
case GET_FACILITIES: case GET_FACILITIES:
return Object.assign({}, state, { return Object.assign({}, state, {
...@@ -32,13 +39,12 @@ export const facilities = (state = defaultState, action, ui) => { ...@@ -32,13 +39,12 @@ export const facilities = (state = defaultState, action, ui) => {
}); });
case SET_FACILITIES: case SET_FACILITIES:
return Object.assign({}, state, { return Object.assign({}, state, {
data: action.facilities.sort(sortFunc), data: action.facilities.sort(facilitySort),
isLoading: false isLoading: false
}); });
case SORT_BY_FAVORITES: case SORT_FACILITY_CARDS:
const newData = cloneDeep(state.data);
return Object.assign({}, state, { return Object.assign({}, state, {
data: newData.sort(sortFunc) data: newData.sort(facilitySort)
}); });
default: default:
return state; return state;
......
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