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

Sort by open

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