Skip to content

Instantly share code, notes, and snippets.

@sht5
Last active May 16, 2018 04:00
Show Gist options
  • Save sht5/d496d1c988e77e88daa8e453038b7eed to your computer and use it in GitHub Desktop.
Save sht5/d496d1c988e77e88daa8e453038b7eed to your computer and use it in GitHub Desktop.
reselect usage
/**
* Created by shahartaite on 23/11/2016.
*/
import consts from 'js/consts';
export const allLeftNavSelectedFiltersSelector = (state) => state[consts.TOP_LEVEL_STATE_REDUCERS.FILTERING_THING_CAPABILITIES_REDUCER].get(consts.STATE_INNER_OBJECT_NAMES.LEFT_NAV_SELECTED_FILTER_PATHS).toJS();
export const selectedSearchBarTermSelector = (state) => {
return state[consts.TOP_LEVEL_STATE_REDUCERS.FILTERING_THING_CAPABILITIES_REDUCER].get(consts.STATE_INNER_OBJECT_NAMES.SEARCH_BAR_TERM).toJS();
}
export const graphStartTimeSelector = (state) => state[consts.TOP_LEVEL_STATE_REDUCERS.FILTERING_THING_CAPABILITIES_REDUCER].get(consts.STATE_INNER_OBJECT_NAMES.SELECTED_START_TIME_FOR_GRAPHS);
export const graphEndTimeSelector = (state) => state[consts.TOP_LEVEL_STATE_REDUCERS.FILTERING_THING_CAPABILITIES_REDUCER].get(consts.STATE_INNER_OBJECT_NAMES.SELECTED_END_TIME_FOR_GRAPHS);
export const isSearchBarTermValid = (state) => state[consts.TOP_LEVEL_STATE_REDUCERS.FILTERING_THING_CAPABILITIES_REDUCER].get(consts.STATE_INNER_OBJECT_NAMES.IS_SEARCH_BAR_TERM_VALID);
export const wasIncorrectTermSearchedByEnterOrSearchClickSelector = (state) => state[consts.TOP_LEVEL_STATE_REDUCERS.FILTERING_THING_CAPABILITIES_REDUCER].get(consts.STATE_INNER_OBJECT_NAMES.WAS_INCORRECT_TERM_SEARCHED_BY_ENTER_OR_SEARCH_CLICK);
export const isShowLeftDrawerSelector = (state) => state[consts.TOP_LEVEL_STATE_REDUCERS.FILTERING_THING_CAPABILITIES_REDUCER].get(consts.STATE_INNER_OBJECT_NAMES.IS_LEFT_DRAWER_OPEN);
export const previousZoomStartAndEndTimesSelector = (state) => state[consts.TOP_LEVEL_STATE_REDUCERS.FILTERING_THING_CAPABILITIES_REDUCER].get(consts.STATE_INNER_OBJECT_NAMES.PREVIOUS_ZOOM_START_AND_END_TIMES).toJS();
/**
* Created by shahartaite on 16/11/2016.
*/
import React, {Component} from 'react';
import { connect } from 'react-redux';
import PageWithFilterPanel from 'js/components/page_with_filter_panel/page_with_filter_panel';
import {requestToGetViewerThingHierarchy, getUserSpecificConfiguration} from 'js/state2/common_actions/data_state_actions';
import {FilteredThingIDsSelector} from 'js/state/common/things/things_selector';
import {connectWebSocket} from 'js/socket_handler';
import {userEmailSelector, userSelectedOrDefaultTimezoneSelector} from 'js/state/common/user/user_selector';
import {isShowLeftDrawerSelector} from 'js/state/filtering_thing_capabilities/filtering_thing_capabilities_selector'
import {userDefaultLanguageSelector, userViewingConfigSelector, userSelectedTimeZoneSelector} from 'js/state/common/user/user_selector';
import {setLocale, loadTranslations} from 'react-redux-i18n';
import {translationsObject} from 'js/store/configureStore'
import {userLoggedIn, requestForUserSettings} from 'js/state/common/user/user_actions';
import clientAuthentication from 'js/authentication/client_authentication';
function mapStateToProps(state) {
return {
thingIDs : FilteredThingIDsSelector(state),
userEmail : userEmailSelector(state),
isShowLeftDrawer : isShowLeftDrawerSelector(state),
userDefaultLanguage : userDefaultLanguageSelector(state),
viewingConfigSettings : userViewingConfigSelector(state),
userSelectedTimeZone : userSelectedTimeZoneSelector(state),
userSelectedOrDefaultTimezone : userSelectedOrDefaultTimezoneSelector(state)
};
}
function mapDispatchToProps(dispatch) {
return {
initializeData : () => {
dispatch(userLoggedIn(clientAuthentication.getEmail()));
dispatch(requestToGetViewerThingHierarchy());
dispatch(getUserSpecificConfiguration());
dispatch(requestForUserSettings());
connectWebSocket();
},
setLocale : (lang) => {
dispatch(loadTranslations(translationsObject));
dispatch(setLocale(lang));
}
};
}
export default connect(
mapStateToProps,
mapDispatchToProps,
)(PageWithFilterPanel);
/**
* Created by shahartaite on 07/11/2016.
*/
import consts from 'js/consts';
import utils from 'js/utils';
import {
selectedSearchBarTermSelector,
allLeftNavSelectedFiltersSelector,
isSearchBarTermValid
} from 'js/state/filtering_thing_capabilities/filtering_thing_capabilities_selector';
import {userViewingConfigSelector} from 'js/state/common/user/user_selector';
export const thingsAndTheirStatusSelector = (state) => state[consts.TOP_LEVEL_STATE_REDUCERS.THINGS_REDUCER].get(consts.STATE_INNER_OBJECT_NAMES.THINGS_CURRENT_STATUS).toJS();
export const thingHierarchySelector = (state) => state[consts.TOP_LEVEL_STATE_REDUCERS.THINGS_REDUCER].get(consts.STATE_INNER_OBJECT_NAMES.THINGS_HIERARCHY).toJS();
const createDeepEqualSelector = utils.createDeepEqualSelector;
export const filteredThingHierarchySelector = createDeepEqualSelector(
[thingHierarchySelector, selectedSearchBarTermSelector, allLeftNavSelectedFiltersSelector, isSearchBarTermValid],
(hierarchy, searchBarTerm, leftNavBarFilters, isSearchTermValid) => {
if(Object.keys(hierarchy).length === 0){
return {};
}
const oldHierarchy = JSON.parse(JSON.stringify(hierarchy));
let combinedFilters = [...leftNavBarFilters];
if(isSearchTermValid && searchBarTerm.length > 0){
combinedFilters.push(searchBarTerm);
}
utils.markAndSweepHierarchyByFilteringWithSelectedPropPaths(combinedFilters, hierarchy);
if (hierarchy.hasOwnProperty('markToBeDeleted')) {
return oldHierarchy;
}
if(utils.getAllThingIDsFromHierarchy(hierarchy, true).length === 0){
return oldHierarchy;
}
console.log('filteredThingHierarchySelector')
return hierarchy;
}
);
export const FilteredThingIDsSelector = createDeepEqualSelector(
[filteredThingHierarchySelector],
(hierarchy) => {
console.log('FilteredThingIDsSelector')
return utils.getAllThingIDsFromHierarchy(hierarchy, true);
}
);
const createDeepEqualSelector = createSelectorCreator(
defaultMemoize,
loadash.isEqual
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment