-
-
Save KTruong008/4c71201882b663c16d6fc0279eb75d59 to your computer and use it in GitHub Desktop.
reselect example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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