|
|
import { select } from '@wordpress/data'; |
|
|
import { STORE_KEY, DataStatus } from './constants'; |
|
|
import { stringifyDomainQueryObject, normalizeDomainSuggestionQuery } from './utils'; |
|
|
import type { State } from './reducer'; |
|
|
import type { |
|
|
DomainAvailability, |
|
|
DomainAvailabilities, |
|
|
DomainCategory, |
|
|
DomainSuggestion, |
|
|
DomainSuggestionQuery, |
|
|
DomainSuggestionSelectorOptions, |
|
|
} from './types'; |
|
|
|
|
|
export const getCategories = ( state: State ): DomainCategory[] => { |
|
|
|
|
|
return [ |
|
|
...state.categories |
|
|
.filter( ( { tier } ) => tier !== null ) |
|
|
.sort( ( a, b ) => ( a > b ? 1 : -1 ) ), |
|
|
...state.categories |
|
|
.filter( ( { tier } ) => tier === null ) |
|
|
.sort( ( a, b ) => a.title.localeCompare( b.title ) ), |
|
|
]; |
|
|
}; |
|
|
|
|
|
export const getDomainSuggestions = ( |
|
|
_state: State, |
|
|
search: string, |
|
|
options: DomainSuggestionSelectorOptions = {} |
|
|
): DomainSuggestion[] | undefined => { |
|
|
const normalizedQuery = normalizeDomainSuggestionQuery( search, options ); |
|
|
|
|
|
|
|
|
return ( |
|
|
select( STORE_KEY ) as { |
|
|
__internalGetDomainSuggestions: ( |
|
|
queryObject: DomainSuggestionQuery |
|
|
) => DomainSuggestion[] | undefined; |
|
|
} |
|
|
).__internalGetDomainSuggestions( normalizedQuery ); |
|
|
}; |
|
|
|
|
|
export const getDomainState = ( state: State ): DataStatus => { |
|
|
return state.domainSuggestions.state; |
|
|
}; |
|
|
|
|
|
export const getDomainErrorMessage = ( state: State ): string | null => { |
|
|
return state.domainSuggestions.errorMessage; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
export const isLoadingDomainSuggestions = ( |
|
|
_state: State, |
|
|
search: string, |
|
|
options: DomainSuggestionSelectorOptions = {} |
|
|
): boolean => { |
|
|
const normalizedQuery = normalizeDomainSuggestionQuery( search, options ); |
|
|
|
|
|
return ( |
|
|
select( 'core/data' ) as { |
|
|
isResolving: ( storeKey: string, resolverName: string, args: unknown[] ) => boolean; |
|
|
} |
|
|
).isResolving( STORE_KEY, '__internalGetDomainSuggestions', [ normalizedQuery ] ); |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export const __internalGetDomainSuggestions = ( |
|
|
state: State, |
|
|
queryObject: DomainSuggestionQuery |
|
|
): DomainSuggestion[] | undefined => { |
|
|
return state.domainSuggestions.data[ stringifyDomainQueryObject( queryObject ) ]; |
|
|
}; |
|
|
|
|
|
export const isAvailable = ( state: State, domainName: string ): DomainAvailability | undefined => { |
|
|
return state.availability[ domainName ]; |
|
|
}; |
|
|
|
|
|
export const getDomainAvailabilities = ( state: State ): DomainAvailabilities => { |
|
|
return state.availability; |
|
|
}; |
|
|
|