Skip to content

Instantly share code, notes, and snippets.

@KTruong008
Created November 14, 2017 21:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KTruong008/4c71201882b663c16d6fc0279eb75d59 to your computer and use it in GitHub Desktop.
Save KTruong008/4c71201882b663c16d6fc0279eb75d59 to your computer and use it in GitHub Desktop.
reselect example
import { path, find, propEq } from 'ramda';
import { createSelector } from 'reselect';
import {
AppStateT,
UserProfileT,
BusinessNameSearchesT,
BusinessNameSearchesSearchesT,
UserDetailsT,
PaymentOrderT,
BUSINESS_REGISTRATION_STATUS,
} from '../types';
import { selectLocationSearchTerm } from '../location/location.selectors';
export const selectUserProfile = (state: AppStateT) => state.userProfile || {};
export const selectUserId = createSelector(
selectUserProfile,
(userProfile: UserProfileT) => userProfile.userId || ''
);
export const selectUserDetails = createSelector(
selectUserProfile,
(userProfile: UserProfileT) => userProfile.userDetails || {}
);
export const selectUserType = createSelector(
selectUserDetails,
(userDetails: UserDetailsT) => userDetails.userType
);
export const selectUserConsent = createSelector(
selectUserProfile,
(userProfile: UserProfileT) => userProfile.userConsent || ''
);
export const selectBusinessNameSearches = createSelector(
selectUserProfile,
(userProfile: UserProfileT) => userProfile.businessNameSearches || ''
);
export const selectSearches = createSelector(
selectBusinessNameSearches,
(businessNameSearchState: BusinessNameSearchesT) => businessNameSearchState.searches || []
);
export const selectUserHasSearched = createSelector(
selectSearches,
(searches) => searches && searches.length > 0
);
export const selectAMatchingServiceOrderIdOfSearchTerm = createSelector(
selectSearches,
selectLocationSearchTerm,
(searches: Array<BusinessNameSearchesSearchesT>, searchTerm: string) => {
return path(['searchResult', 'ServiceOrderId'], find(propEq('searchTerm', searchTerm), searches));
}
);
export const selectBusinessRegistration = createSelector(
selectUserProfile,
(userProfile: UserProfileT) => userProfile.businessRegistration || {}
);
export const selectUserIndustry = createSelector(
selectBusinessRegistration,
(businessRegistration) => businessRegistration.industry || ''
);
export const selectHasRegisteredBusiness = createSelector(
selectBusinessRegistration,
(businessRegistration) => businessRegistration && (businessRegistration.status === BUSINESS_REGISTRATION_STATUS.COMPLETE || businessRegistration.status === BUSINESS_REGISTRATION_STATUS.PENDING)
);
export const selectUserOrders = createSelector(
selectUserDetails,
(details: UserDetailsT) => details.orders,
);
export const selectRebateRequested = createSelector(
selectUserDetails,
(userDetails: UserDetailsT): boolean => userDetails.rebateRequested || false
);
export const selectUserHasPaid = createSelector(
selectUserOrders,
(orders: PaymentOrderT[]) => orders.length > 0,
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment