File size: 2,702 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
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[] => {
	// Sort domain categories by tier, then by title.
	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 );

	// We need to go through the `select` store to get the resolver action
	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;
};

// TODO: reconcile this function with "Pending" status?
// It doesn't seem to be used
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 ] );
};

/**
 * Do not use this selector. It is for internal use.
 * @param state Store state
 * @param queryObject Normalized object representing the query
 * @returns suggestions
 */
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;
};