@automattic/data-stores 3.1.2 → 3.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (181) hide show
  1. package/CHANGELOG.md +14 -1
  2. package/dist/cjs/contextual-help/admin-sections.js +1 -1
  3. package/dist/cjs/contextual-help/admin-sections.js.map +1 -1
  4. package/dist/cjs/help-center/actions.js +79 -49
  5. package/dist/cjs/help-center/actions.js.map +1 -1
  6. package/dist/cjs/help-center/reducer.js +19 -10
  7. package/dist/cjs/help-center/reducer.js.map +1 -1
  8. package/dist/cjs/help-center/resolvers.js +2 -1
  9. package/dist/cjs/help-center/resolvers.js.map +1 -1
  10. package/dist/cjs/help-center/selectors.js +7 -5
  11. package/dist/cjs/help-center/selectors.js.map +1 -1
  12. package/dist/cjs/index.js +1 -4
  13. package/dist/cjs/index.js.map +1 -1
  14. package/dist/cjs/onboard/actions.js +11 -1
  15. package/dist/cjs/onboard/actions.js.map +1 -1
  16. package/dist/cjs/onboard/reducer.js +23 -1
  17. package/dist/cjs/onboard/reducer.js.map +1 -1
  18. package/dist/cjs/onboard/selectors.js +5 -1
  19. package/dist/cjs/onboard/selectors.js.map +1 -1
  20. package/dist/cjs/plans/queries/use-plans.js +20 -1
  21. package/dist/cjs/plans/queries/use-plans.js.map +1 -1
  22. package/dist/cjs/purchases/lib/assembler.js +6 -16
  23. package/dist/cjs/purchases/lib/assembler.js.map +1 -1
  24. package/dist/cjs/site/types.js.map +1 -1
  25. package/dist/esm/contextual-help/admin-sections.js +1 -1
  26. package/dist/esm/contextual-help/admin-sections.js.map +1 -1
  27. package/dist/esm/help-center/actions.js +74 -46
  28. package/dist/esm/help-center/actions.js.map +1 -1
  29. package/dist/esm/help-center/reducer.js +19 -10
  30. package/dist/esm/help-center/reducer.js.map +1 -1
  31. package/dist/esm/help-center/resolvers.js +3 -2
  32. package/dist/esm/help-center/resolvers.js.map +1 -1
  33. package/dist/esm/help-center/selectors.js +3 -2
  34. package/dist/esm/help-center/selectors.js.map +1 -1
  35. package/dist/esm/index.js +1 -3
  36. package/dist/esm/index.js.map +1 -1
  37. package/dist/esm/onboard/actions.js +8 -0
  38. package/dist/esm/onboard/actions.js.map +1 -1
  39. package/dist/esm/onboard/reducer.js +20 -0
  40. package/dist/esm/onboard/reducer.js.map +1 -1
  41. package/dist/esm/onboard/selectors.js +2 -0
  42. package/dist/esm/onboard/selectors.js.map +1 -1
  43. package/dist/esm/plans/queries/use-plans.js +20 -1
  44. package/dist/esm/plans/queries/use-plans.js.map +1 -1
  45. package/dist/esm/purchases/lib/assembler.js +6 -16
  46. package/dist/esm/purchases/lib/assembler.js.map +1 -1
  47. package/dist/esm/site/types.js.map +1 -1
  48. package/dist/tsconfig-cjs.tsbuildinfo +1 -1
  49. package/dist/tsconfig.tsbuildinfo +1 -1
  50. package/dist/types/help-center/actions.d.ts +30 -14
  51. package/dist/types/help-center/actions.d.ts.map +1 -1
  52. package/dist/types/help-center/reducer.d.ts +6 -5
  53. package/dist/types/help-center/reducer.d.ts.map +1 -1
  54. package/dist/types/help-center/resolvers.d.ts +16 -8
  55. package/dist/types/help-center/resolvers.d.ts.map +1 -1
  56. package/dist/types/help-center/selectors.d.ts +3 -2
  57. package/dist/types/help-center/selectors.d.ts.map +1 -1
  58. package/dist/types/help-center/types.d.ts +1 -0
  59. package/dist/types/help-center/types.d.ts.map +1 -1
  60. package/dist/types/index.d.ts +473 -43
  61. package/dist/types/index.d.ts.map +1 -1
  62. package/dist/types/onboard/actions.d.ts +14 -2
  63. package/dist/types/onboard/actions.d.ts.map +1 -1
  64. package/dist/types/onboard/reducer.d.ts +7 -1
  65. package/dist/types/onboard/reducer.d.ts.map +1 -1
  66. package/dist/types/onboard/selectors.d.ts +6 -2
  67. package/dist/types/onboard/selectors.d.ts.map +1 -1
  68. package/dist/types/onboard/types.d.ts +1 -1
  69. package/dist/types/onboard/types.d.ts.map +1 -1
  70. package/dist/types/plans/queries/use-plans.d.ts.map +1 -1
  71. package/dist/types/purchases/lib/assembler.d.ts +3 -3
  72. package/dist/types/purchases/lib/assembler.d.ts.map +1 -1
  73. package/dist/types/purchases/types.d.ts +3 -109
  74. package/dist/types/purchases/types.d.ts.map +1 -1
  75. package/dist/types/queries/use-all-domains-query.d.ts +1 -1
  76. package/dist/types/queries/use-all-domains-query.d.ts.map +1 -1
  77. package/dist/types/queries/use-site-domains-query.d.ts +1 -1
  78. package/dist/types/queries/use-site-domains-query.d.ts.map +1 -1
  79. package/dist/types/queries/use-site-query.d.ts +1 -1
  80. package/dist/types/queries/use-site-query.d.ts.map +1 -1
  81. package/dist/types/reader/index.d.ts +472 -40
  82. package/dist/types/reader/index.d.ts.map +1 -1
  83. package/dist/types/reader/queries/use-pending-post-subscriptions-query.d.ts +75 -7
  84. package/dist/types/reader/queries/use-pending-post-subscriptions-query.d.ts.map +1 -1
  85. package/dist/types/reader/queries/use-pending-site-subscriptions-query.d.ts +75 -7
  86. package/dist/types/reader/queries/use-pending-site-subscriptions-query.d.ts.map +1 -1
  87. package/dist/types/reader/queries/use-post-subscriptions-query.d.ts +143 -13
  88. package/dist/types/reader/queries/use-post-subscriptions-query.d.ts.map +1 -1
  89. package/dist/types/reader/queries/use-site-subscriptions-query.d.ts +103 -13
  90. package/dist/types/reader/queries/use-site-subscriptions-query.d.ts.map +1 -1
  91. package/dist/types/site/types.d.ts +2 -0
  92. package/dist/types/site/types.d.ts.map +1 -1
  93. package/dist/types/user/actions.d.ts +1 -1
  94. package/dist/types/user/reducer.d.ts +1 -1
  95. package/dist/types/user/resolvers.d.ts +2 -2
  96. package/dist/types/user/selectors.d.ts +2 -2
  97. package/dist/types/user/selectors.d.ts.map +1 -1
  98. package/dist/types/user/types.d.ts +2 -33
  99. package/dist/types/user/types.d.ts.map +1 -1
  100. package/package.json +4 -8
  101. package/src/contextual-help/admin-sections.ts +1 -1
  102. package/src/help-center/actions.ts +85 -50
  103. package/src/help-center/reducer.ts +27 -15
  104. package/src/help-center/resolvers.ts +15 -8
  105. package/src/help-center/selectors.ts +4 -2
  106. package/src/help-center/types.ts +1 -0
  107. package/src/index.ts +0 -3
  108. package/src/onboard/actions.ts +13 -1
  109. package/src/onboard/reducer.ts +26 -1
  110. package/src/onboard/selectors.ts +2 -0
  111. package/src/onboard/types.ts +1 -1
  112. package/src/plans/queries/use-plans.ts +29 -1
  113. package/src/purchases/lib/assembler.ts +10 -25
  114. package/src/purchases/types.ts +4 -116
  115. package/src/site/types.ts +2 -0
  116. package/src/user/types.ts +2 -38
  117. package/dist/cjs/domain-suggestions/actions.js +0 -33
  118. package/dist/cjs/domain-suggestions/actions.js.map +0 -1
  119. package/dist/cjs/domain-suggestions/constants.js +0 -12
  120. package/dist/cjs/domain-suggestions/constants.js.map +0 -1
  121. package/dist/cjs/domain-suggestions/index.js +0 -25
  122. package/dist/cjs/domain-suggestions/index.js.map +0 -1
  123. package/dist/cjs/domain-suggestions/queries.js +0 -78
  124. package/dist/cjs/domain-suggestions/queries.js.map +0 -1
  125. package/dist/cjs/domain-suggestions/reducer.js +0 -65
  126. package/dist/cjs/domain-suggestions/reducer.js.map +0 -1
  127. package/dist/cjs/domain-suggestions/resolvers.js +0 -91
  128. package/dist/cjs/domain-suggestions/resolvers.js.map +0 -1
  129. package/dist/cjs/domain-suggestions/selectors.js +0 -58
  130. package/dist/cjs/domain-suggestions/selectors.js.map +0 -1
  131. package/dist/cjs/domain-suggestions/types.js +0 -3
  132. package/dist/cjs/domain-suggestions/types.js.map +0 -1
  133. package/dist/cjs/domain-suggestions/utils.js +0 -47
  134. package/dist/cjs/domain-suggestions/utils.js.map +0 -1
  135. package/dist/esm/domain-suggestions/actions.js +0 -25
  136. package/dist/esm/domain-suggestions/actions.js.map +0 -1
  137. package/dist/esm/domain-suggestions/constants.js +0 -9
  138. package/dist/esm/domain-suggestions/constants.js.map +0 -1
  139. package/dist/esm/domain-suggestions/index.js +0 -20
  140. package/dist/esm/domain-suggestions/index.js.map +0 -1
  141. package/dist/esm/domain-suggestions/queries.js +0 -72
  142. package/dist/esm/domain-suggestions/queries.js.map +0 -1
  143. package/dist/esm/domain-suggestions/reducer.js +0 -61
  144. package/dist/esm/domain-suggestions/reducer.js.map +0 -1
  145. package/dist/esm/domain-suggestions/resolvers.js +0 -84
  146. package/dist/esm/domain-suggestions/resolvers.js.map +0 -1
  147. package/dist/esm/domain-suggestions/selectors.js +0 -47
  148. package/dist/esm/domain-suggestions/selectors.js.map +0 -1
  149. package/dist/esm/domain-suggestions/types.js +0 -2
  150. package/dist/esm/domain-suggestions/types.js.map +0 -1
  151. package/dist/esm/domain-suggestions/utils.js +0 -41
  152. package/dist/esm/domain-suggestions/utils.js.map +0 -1
  153. package/dist/types/domain-suggestions/actions.d.ts +0 -27
  154. package/dist/types/domain-suggestions/actions.d.ts.map +0 -1
  155. package/dist/types/domain-suggestions/constants.d.ts +0 -8
  156. package/dist/types/domain-suggestions/constants.d.ts.map +0 -1
  157. package/dist/types/domain-suggestions/index.d.ts +0 -10
  158. package/dist/types/domain-suggestions/index.d.ts.map +0 -1
  159. package/dist/types/domain-suggestions/queries.d.ts +0 -323
  160. package/dist/types/domain-suggestions/queries.d.ts.map +0 -1
  161. package/dist/types/domain-suggestions/reducer.d.ts +0 -16
  162. package/dist/types/domain-suggestions/reducer.d.ts.map +0 -1
  163. package/dist/types/domain-suggestions/resolvers.d.ts +0 -39
  164. package/dist/types/domain-suggestions/resolvers.d.ts.map +0 -1
  165. package/dist/types/domain-suggestions/selectors.d.ts +0 -18
  166. package/dist/types/domain-suggestions/selectors.d.ts.map +0 -1
  167. package/dist/types/domain-suggestions/types.d.ts +0 -206
  168. package/dist/types/domain-suggestions/types.d.ts.map +0 -1
  169. package/dist/types/domain-suggestions/utils.d.ts +0 -23
  170. package/dist/types/domain-suggestions/utils.d.ts.map +0 -1
  171. package/src/domain-suggestions/actions.ts +0 -51
  172. package/src/domain-suggestions/constants.ts +0 -8
  173. package/src/domain-suggestions/index.ts +0 -22
  174. package/src/domain-suggestions/queries.ts +0 -85
  175. package/src/domain-suggestions/reducer.ts +0 -77
  176. package/src/domain-suggestions/resolvers.ts +0 -110
  177. package/src/domain-suggestions/selectors.ts +0 -86
  178. package/src/domain-suggestions/test/reducer.ts +0 -105
  179. package/src/domain-suggestions/test/selectors.ts +0 -127
  180. package/src/domain-suggestions/types.ts +0 -254
  181. package/src/domain-suggestions/utils.ts +0 -50
@@ -1,51 +0,0 @@
1
- import type {
2
- DomainSuggestion,
3
- DomainSuggestionQuery,
4
- DomainCategory,
5
- DomainAvailability,
6
- } from './types';
7
-
8
- export const receiveCategories = ( categories: DomainCategory[] ) =>
9
- ( {
10
- type: 'RECEIVE_CATEGORIES',
11
- categories,
12
- } ) as const;
13
-
14
- export const fetchDomainSuggestions = () =>
15
- ( {
16
- type: 'FETCH_DOMAIN_SUGGESTIONS',
17
- timeStamp: Date.now(),
18
- } ) as const;
19
-
20
- export const receiveDomainAvailability = ( domainName: string, availability: DomainAvailability ) =>
21
- ( {
22
- type: 'RECEIVE_DOMAIN_AVAILABILITY',
23
- domainName,
24
- availability,
25
- } ) as const;
26
-
27
- export const receiveDomainSuggestionsSuccess = (
28
- queryObject: DomainSuggestionQuery,
29
- suggestions: DomainSuggestion[] | undefined
30
- ) =>
31
- ( {
32
- type: 'RECEIVE_DOMAIN_SUGGESTIONS_SUCCESS',
33
- queryObject,
34
- suggestions,
35
- timeStamp: Date.now(),
36
- } ) as const;
37
-
38
- export const receiveDomainSuggestionsError = ( errorMessage: string ) =>
39
- ( {
40
- type: 'RECEIVE_DOMAIN_SUGGESTIONS_ERROR',
41
- errorMessage,
42
- timeStamp: Date.now(),
43
- } ) as const;
44
-
45
- export type Action = ReturnType<
46
- | typeof receiveCategories
47
- | typeof fetchDomainSuggestions
48
- | typeof receiveDomainSuggestionsSuccess
49
- | typeof receiveDomainSuggestionsError
50
- | typeof receiveDomainAvailability
51
- >;
@@ -1,8 +0,0 @@
1
- export const STORE_KEY = 'automattic/domains/suggestions';
2
-
3
- export enum DataStatus {
4
- Failure = 'failure',
5
- Pending = 'pending',
6
- Success = 'success',
7
- Uninitialized = 'uninitialized',
8
- }
@@ -1,22 +0,0 @@
1
- import { register, createReduxStore } from '@wordpress/data';
2
- import { controls } from '../wpcom-request-controls';
3
- import * as actions from './actions';
4
- import { STORE_KEY } from './constants';
5
- import reducer, { State } from './reducer';
6
- import * as resolvers from './resolvers';
7
- import * as selectors from './selectors';
8
-
9
- export * from './types';
10
- export * from './constants';
11
- export * from './queries';
12
- export { getFormattedPrice } from './utils';
13
- export type { State };
14
-
15
- export const store = createReduxStore( STORE_KEY, {
16
- actions,
17
- controls,
18
- reducer,
19
- resolvers,
20
- selectors,
21
- } );
22
- register( store );
@@ -1,85 +0,0 @@
1
- import { useQuery, useQueryClient } from '@tanstack/react-query';
2
- import { stringify } from 'qs';
3
- import wpcomProxyRequest from 'wpcom-proxy-request';
4
- import { getFormattedPrice, normalizeDomainSuggestionQuery } from './utils';
5
- import type { DomainSuggestion, DomainSuggestionSelectorOptions } from './types';
6
-
7
- const STALE_TIME = 1000 * 60 * 5; // 5 minutes
8
-
9
- export function useGetDomainSuggestions(
10
- search?: string | null,
11
- searchOptions: DomainSuggestionSelectorOptions = {},
12
- queryOptions = {}
13
- ) {
14
- const queryClient = useQueryClient();
15
- const result = useQuery( {
16
- queryKey: [ 'domain-suggestions', search, searchOptions ],
17
- queryFn: async () => {
18
- if ( ! search ) {
19
- return [];
20
- }
21
- const queryObject = normalizeDomainSuggestionQuery( search, searchOptions );
22
-
23
- const suggestions: DomainSuggestion[] = await wpcomProxyRequest( {
24
- apiVersion: '1.1',
25
- path: '/domains/suggestions',
26
- query: stringify( queryObject ),
27
- } );
28
- if ( ! Array.isArray( suggestions ) ) {
29
- throw new Error( 'Invalid response from the server' );
30
- }
31
-
32
- const processedSuggestions = suggestions.map( ( suggestion: DomainSuggestion ) => {
33
- if ( suggestion.unavailable ) {
34
- return suggestion;
35
- }
36
- return {
37
- ...suggestion,
38
- ...( suggestion.raw_price &&
39
- suggestion.currency_code && {
40
- cost: getFormattedPrice( suggestion.raw_price, suggestion.currency_code ),
41
- } ),
42
- };
43
- } );
44
-
45
- return processedSuggestions;
46
- },
47
- enabled: !! search,
48
- staleTime: STALE_TIME,
49
- ...queryOptions,
50
- } );
51
-
52
- return {
53
- ...result,
54
- invalidateCache: () =>
55
- queryClient.invalidateQueries( {
56
- queryKey: [ 'domain-suggestions', search, searchOptions ],
57
- } ),
58
- };
59
- }
60
-
61
- /**
62
- * Returns the expected *.wordpress.com for a given domain name
63
- */
64
- export function useGetWordPressSubdomain( paidDomainName?: string | null ) {
65
- return useGetDomainSuggestions( paidDomainName, {
66
- quantity: 1,
67
- include_wordpressdotcom: true,
68
- include_dotblogsubdomain: false,
69
- only_wordpressdotcom: false,
70
- vendor: 'dot',
71
- } );
72
- }
73
-
74
- /**
75
- * Returns a custom .com domain suggestion for a given query
76
- */
77
- export function useGetSingleCustomDotComDomainSuggestion( query?: string | null, locale?: string ) {
78
- return useGetDomainSuggestions( query, {
79
- quantity: 1,
80
- include_wordpressdotcom: false,
81
- include_dotblogsubdomain: false,
82
- locale,
83
- tlds: [ 'com' ],
84
- } );
85
- }
@@ -1,77 +0,0 @@
1
- import { combineReducers } from '@wordpress/data';
2
- import { DataStatus } from './constants';
3
- import { stringifyDomainQueryObject } from './utils';
4
- import type { Action } from './actions';
5
- import type { DomainCategory, DomainSuggestionState, DomainAvailabilities } from './types';
6
- import type { Reducer } from 'redux';
7
-
8
- const initialDomainSuggestionState: DomainSuggestionState = {
9
- state: DataStatus.Uninitialized,
10
- data: {},
11
- errorMessage: null,
12
- lastUpdated: -Infinity,
13
- pendingSince: undefined,
14
- };
15
-
16
- export const domainSuggestions: Reducer< DomainSuggestionState, Action > = (
17
- state = initialDomainSuggestionState,
18
- action
19
- ) => {
20
- if ( action.type === 'FETCH_DOMAIN_SUGGESTIONS' ) {
21
- return {
22
- ...state,
23
- state: DataStatus.Pending,
24
- errorMessage: null,
25
- pendingSince: action.timeStamp,
26
- };
27
- }
28
-
29
- if ( action.type === 'RECEIVE_DOMAIN_SUGGESTIONS_SUCCESS' ) {
30
- return {
31
- ...state,
32
- state: DataStatus.Success,
33
- data: {
34
- ...state.data,
35
- [ stringifyDomainQueryObject( action.queryObject ) ]: action.suggestions,
36
- },
37
- errorMessage: null,
38
- lastUpdated: action.timeStamp,
39
- pendingSince: undefined,
40
- };
41
- }
42
-
43
- if ( action.type === 'RECEIVE_DOMAIN_SUGGESTIONS_ERROR' ) {
44
- return {
45
- ...state,
46
- state: DataStatus.Failure,
47
- errorMessage: action.errorMessage,
48
- lastUpdated: action.timeStamp,
49
- pendingSince: undefined,
50
- };
51
- }
52
-
53
- return state;
54
- };
55
-
56
- const categories: Reducer< DomainCategory[], Action > = ( state = [], action ) => {
57
- if ( action.type === 'RECEIVE_CATEGORIES' ) {
58
- return action.categories;
59
- }
60
- return state;
61
- };
62
-
63
- const availability: Reducer< DomainAvailabilities, Action > = ( state = {}, action ) => {
64
- if ( action.type === 'RECEIVE_DOMAIN_AVAILABILITY' ) {
65
- return {
66
- ...state,
67
- [ action.domainName ]: action.availability,
68
- };
69
- }
70
- return state;
71
- };
72
-
73
- const reducer = combineReducers( { categories, domainSuggestions, availability } );
74
-
75
- export type State = ReturnType< typeof reducer >;
76
-
77
- export default reducer;
@@ -1,110 +0,0 @@
1
- import { translate } from 'i18n-calypso';
2
- import { stringify } from 'qs';
3
- import validator from 'validator';
4
- import { fetchAndParse, wpcomRequest } from '../wpcom-request-controls';
5
- import {
6
- receiveCategories,
7
- receiveDomainSuggestionsSuccess,
8
- receiveDomainSuggestionsError,
9
- fetchDomainSuggestions,
10
- receiveDomainAvailability,
11
- } from './actions';
12
- import { getFormattedPrice } from './utils';
13
- import type { DomainSuggestion, DomainSuggestionQuery } from './types';
14
-
15
- function getAvailabilityURL( domainName: string ) {
16
- return `https://public-api.wordpress.com/rest/v1.3/domains/${ encodeURIComponent(
17
- domainName
18
- ) }/is-available?is_cart_pre_check=true`;
19
- }
20
-
21
- function suggestionsLackThisFQDN( suggestions: DomainSuggestion[], domainName: string ) {
22
- return (
23
- validator.isFQDN( domainName ) &&
24
- ! suggestions.some( ( s ) => s.domain_name.toLowerCase() === domainName )
25
- );
26
- }
27
-
28
- export const isAvailable = function* isAvailable( domainName: string ) {
29
- const url = getAvailabilityURL( domainName );
30
-
31
- try {
32
- const { body } = yield fetchAndParse( url );
33
- return receiveDomainAvailability( domainName, body );
34
- } catch {
35
- // the API returns a status of 'unknown' if it can not accurately determine
36
- // availability, we will return the same status if the API request fails.
37
- return receiveDomainAvailability( domainName, {
38
- domain_name: domainName,
39
- mappable: 'unknown',
40
- status: 'unknown',
41
- supports_privacy: false,
42
- } );
43
- }
44
- };
45
-
46
- export function* getCategories() {
47
- const { body } = yield fetchAndParse(
48
- 'https://public-api.wordpress.com/wpcom/v2/onboarding/domains/categories'
49
- );
50
- return receiveCategories( body );
51
- }
52
-
53
- export function* __internalGetDomainSuggestions( queryObject: DomainSuggestionQuery ) {
54
- // If normalized search string (`query`) contains no alphanumerics, endpoint 404s
55
- if ( ! queryObject.query ) {
56
- return receiveDomainSuggestionsError( 'Empty query' );
57
- }
58
-
59
- yield fetchDomainSuggestions();
60
-
61
- try {
62
- const suggestions: DomainSuggestion[] = yield wpcomRequest( {
63
- apiVersion: '1.1',
64
- path: '/domains/suggestions',
65
- query: stringify( queryObject ),
66
- } );
67
-
68
- if ( ! Array.isArray( suggestions ) ) {
69
- // Other internal server errors
70
- return receiveDomainSuggestionsError(
71
- translate( 'Invalid response from the server' ) as string
72
- );
73
- }
74
-
75
- // if the query is a FQDN and the results don't have it,
76
- // this implies that the user is searching for an unavailable domain name
77
- // TODO: query the availability endpoint to find the exact reason why it's unavailable
78
- // all the possible responses can be found here https://github.com/Automattic/wp-calypso/blob/trunk/client/lib/domains/registration/availability-messages.js#L40-L390
79
- if ( suggestionsLackThisFQDN( suggestions, queryObject.query ) ) {
80
- const unavailableSuggestion: DomainSuggestion = {
81
- domain_name: queryObject.query,
82
- unavailable: true,
83
- cost: '',
84
- raw_price: 0,
85
- currency_code: '',
86
- };
87
- suggestions.unshift( unavailableSuggestion );
88
- }
89
-
90
- const processedSuggestions = suggestions.map( ( suggestion: DomainSuggestion ) => {
91
- if ( suggestion.unavailable ) {
92
- return suggestion;
93
- }
94
- return {
95
- ...suggestion,
96
- ...( suggestion.raw_price &&
97
- suggestion.currency_code && {
98
- cost: getFormattedPrice( suggestion.raw_price, suggestion.currency_code ),
99
- } ),
100
- };
101
- } );
102
-
103
- return receiveDomainSuggestionsSuccess( queryObject, processedSuggestions );
104
- } catch ( e ) {
105
- // e.g. no connection, or JSON parsing error
106
- return receiveDomainSuggestionsError(
107
- ( e as Error ).message || ( translate( 'Error while fetching server response' ) as string )
108
- );
109
- }
110
- }
@@ -1,86 +0,0 @@
1
- import { select } from '@wordpress/data';
2
- import { STORE_KEY, DataStatus } from './constants';
3
- import { stringifyDomainQueryObject, normalizeDomainSuggestionQuery } from './utils';
4
- import type { State } from './reducer';
5
- import type {
6
- DomainAvailability,
7
- DomainAvailabilities,
8
- DomainCategory,
9
- DomainSuggestion,
10
- DomainSuggestionQuery,
11
- DomainSuggestionSelectorOptions,
12
- } from './types';
13
-
14
- export const getCategories = ( state: State ): DomainCategory[] => {
15
- // Sort domain categories by tier, then by title.
16
- return [
17
- ...state.categories
18
- .filter( ( { tier } ) => tier !== null )
19
- .sort( ( a, b ) => ( a > b ? 1 : -1 ) ),
20
- ...state.categories
21
- .filter( ( { tier } ) => tier === null )
22
- .sort( ( a, b ) => a.title.localeCompare( b.title ) ),
23
- ];
24
- };
25
-
26
- export const getDomainSuggestions = (
27
- _state: State,
28
- search: string,
29
- options: DomainSuggestionSelectorOptions = {}
30
- ): DomainSuggestion[] | undefined => {
31
- const normalizedQuery = normalizeDomainSuggestionQuery( search, options );
32
-
33
- // We need to go through the `select` store to get the resolver action
34
- return (
35
- select( STORE_KEY ) as {
36
- __internalGetDomainSuggestions: (
37
- queryObject: DomainSuggestionQuery
38
- ) => DomainSuggestion[] | undefined;
39
- }
40
- ).__internalGetDomainSuggestions( normalizedQuery );
41
- };
42
-
43
- export const getDomainState = ( state: State ): DataStatus => {
44
- return state.domainSuggestions.state;
45
- };
46
-
47
- export const getDomainErrorMessage = ( state: State ): string | null => {
48
- return state.domainSuggestions.errorMessage;
49
- };
50
-
51
- // TODO: reconcile this function with "Pending" status?
52
- // It doesn't seem to be used
53
- export const isLoadingDomainSuggestions = (
54
- _state: State,
55
- search: string,
56
- options: DomainSuggestionSelectorOptions = {}
57
- ): boolean => {
58
- const normalizedQuery = normalizeDomainSuggestionQuery( search, options );
59
-
60
- return (
61
- select( 'core/data' ) as {
62
- isResolving: ( storeKey: string, resolverName: string, args: unknown[] ) => boolean;
63
- }
64
- ).isResolving( STORE_KEY, '__internalGetDomainSuggestions', [ normalizedQuery ] );
65
- };
66
-
67
- /**
68
- * Do not use this selector. It is for internal use.
69
- * @param state Store state
70
- * @param queryObject Normalized object representing the query
71
- * @returns suggestions
72
- */
73
- export const __internalGetDomainSuggestions = (
74
- state: State,
75
- queryObject: DomainSuggestionQuery
76
- ): DomainSuggestion[] | undefined => {
77
- return state.domainSuggestions.data[ stringifyDomainQueryObject( queryObject ) ];
78
- };
79
-
80
- export const isAvailable = ( state: State, domainName: string ): DomainAvailability | undefined => {
81
- return state.availability[ domainName ];
82
- };
83
-
84
- export const getDomainAvailabilities = ( state: State ): DomainAvailabilities => {
85
- return state.availability;
86
- };
@@ -1,105 +0,0 @@
1
- import { DataStatus } from '../constants';
2
- import { domainSuggestions } from '../reducer';
3
- import type { DomainSuggestionState, DomainSuggestion } from '../types';
4
-
5
- describe( 'domainSuggestions', () => {
6
- const initialTimeStamp = Date.now();
7
- const initialState: DomainSuggestionState = {
8
- state: DataStatus.Uninitialized,
9
- data: undefined,
10
- errorMessage: null,
11
- lastUpdated: initialTimeStamp,
12
- pendingSince: undefined,
13
- };
14
-
15
- it( 'goes into a state of pending when fetching results', () => {
16
- const now = Date.now();
17
- const state = domainSuggestions( initialState, {
18
- type: 'FETCH_DOMAIN_SUGGESTIONS',
19
- timeStamp: now,
20
- } );
21
-
22
- const expectedState: DomainSuggestionState = {
23
- state: DataStatus.Pending,
24
- data: undefined,
25
- errorMessage: null,
26
- lastUpdated: initialTimeStamp,
27
- pendingSince: now,
28
- };
29
- expect( state ).toEqual( expectedState );
30
- } );
31
-
32
- it( 'goes into a state of errored when fetching results have failed', () => {
33
- const now = Date.now();
34
- const errorMessage = 'An unexpected error has occured';
35
- const state = domainSuggestions( initialState, {
36
- type: 'RECEIVE_DOMAIN_SUGGESTIONS_ERROR',
37
- timeStamp: now,
38
- errorMessage,
39
- } );
40
-
41
- const expectedState: DomainSuggestionState = {
42
- state: DataStatus.Failure,
43
- data: undefined,
44
- errorMessage: errorMessage,
45
- lastUpdated: now,
46
- pendingSince: undefined,
47
- };
48
- expect( state ).toEqual( expectedState );
49
- } );
50
-
51
- it( 'goes into a state of success when fetching results have returned results', () => {
52
- const queryObject = {
53
- include_dotblogsubdomain: false,
54
- include_wordpressdotcom: true,
55
- locale: 'en',
56
- only_wordpressdotcom: false,
57
- quantity: 11,
58
- query: 'test site',
59
- vendor: 'variation4_front',
60
- };
61
- const suggestions: DomainSuggestion[] = [
62
- {
63
- domain_name: 'test.site',
64
- relevance: 1,
65
- supports_privacy: true,
66
- vendor: 'donuts',
67
- match_reasons: [ 'tld-common', 'tld-exact' ],
68
- product_id: 78,
69
- product_slug: 'dotsite_domain',
70
- cost: '$25.00',
71
- raw_price: 25,
72
- currency_code: 'USD',
73
- },
74
- {
75
- domain_name: 'hot-test-site.com',
76
- relevance: 1,
77
- supports_privacy: true,
78
- vendor: 'donuts',
79
- match_reasons: [ 'tld-common' ],
80
- product_id: 6,
81
- product_slug: 'domain_reg',
82
- cost: '$18.00',
83
- raw_price: 18,
84
- currency_code: 'USD',
85
- },
86
- ];
87
- const now = Date.now();
88
- const state = domainSuggestions( initialState, {
89
- type: 'RECEIVE_DOMAIN_SUGGESTIONS_SUCCESS',
90
- queryObject,
91
- suggestions,
92
- timeStamp: now,
93
- } );
94
-
95
- const expectedState: DomainSuggestionState = {
96
- state: DataStatus.Success,
97
- data: { [ JSON.stringify( queryObject ) ]: suggestions },
98
- errorMessage: null,
99
- lastUpdated: now,
100
- pendingSince: undefined,
101
- };
102
- expect( state ).toEqual( expectedState );
103
- expect( state.data[ JSON.stringify( queryObject ) ].length ).toEqual( suggestions.length );
104
- } );
105
- } );
@@ -1,127 +0,0 @@
1
- import { select, subscribe } from '@wordpress/data';
2
- import wpcomRequest from 'wpcom-proxy-request';
3
- import { store } from '..';
4
- import { DataStatus } from '../constants';
5
-
6
- jest.mock( 'wpcom-proxy-request', () => ( {
7
- __esModule: true,
8
- default: jest.fn(),
9
- requestAllBlogsAccess: jest.fn( () => Promise.resolve() ),
10
- } ) );
11
-
12
- const options = {
13
- category_slug: undefined,
14
- include_dotblogsubdomain: false,
15
- include_wordpressdotcom: true,
16
- locale: 'en',
17
- quantity: 11,
18
- };
19
- const apiResponse = [
20
- {
21
- domain_name: 'test.site',
22
- relevance: 1,
23
- supports_privacy: true,
24
- vendor: 'donuts',
25
- match_reasons: [ 'tld-common', 'tld-exact' ],
26
- product_id: 78,
27
- product_slug: 'dotsite_domain',
28
- cost: '$25',
29
- raw_price: 25,
30
- currency_code: 'USD',
31
- unavailable: false,
32
- },
33
- {
34
- domain_name: 'hot-test-site.com',
35
- relevance: 1,
36
- supports_privacy: true,
37
- vendor: 'donuts',
38
- match_reasons: [ 'tld-common' ],
39
- product_id: 6,
40
- product_slug: 'domain_reg',
41
- cost: '$18',
42
- raw_price: 18,
43
- currency_code: 'USD',
44
- unavailable: false,
45
- },
46
- ];
47
-
48
- beforeEach( () => {
49
- ( wpcomRequest as jest.Mock ).mockReset();
50
- } );
51
-
52
- describe( 'getDomainSuggestions', () => {
53
- it( 'resolves the state via an API call and caches the resolver on success', async () => {
54
- ( wpcomRequest as jest.Mock ).mockResolvedValue( apiResponse );
55
-
56
- const query = 'test query one';
57
- const listenForStateUpdate = () => {
58
- return new Promise< void >( ( resolve ) => {
59
- const unsubscribe = subscribe( () => {
60
- unsubscribe();
61
- resolve();
62
- } );
63
- } );
64
- };
65
-
66
- // Status should be uninitialized before first call
67
- expect( select( store ).getDomainState() ).toEqual( DataStatus.Uninitialized );
68
-
69
- // First call returns undefined
70
- expect( select( store ).getDomainSuggestions( query, options ) ).toEqual( undefined );
71
-
72
- await listenForStateUpdate();
73
-
74
- // Status should be pending while we wait for the response
75
- expect( select( store ).getDomainState() ).toEqual( DataStatus.Pending );
76
-
77
- await listenForStateUpdate();
78
-
79
- // Status should be successful once we get the response
80
- expect( select( store ).getDomainState() ).toEqual( DataStatus.Success );
81
-
82
- // By the second call, the resolver will have resolved
83
- expect( select( store ).getDomainSuggestions( query, options ) ).toEqual( apiResponse );
84
-
85
- await listenForStateUpdate();
86
-
87
- // The resolver should now be cached with an `isStarting` value of false
88
-
89
- expect(
90
- select( 'core/data' ).isResolving( store, 'getDomainSuggestions', [ query, options ] )
91
- ).toEqual( false );
92
- } );
93
- } );
94
-
95
- describe( 'getDomainErrorMessage', () => {
96
- it( 'should return null if no error message', () => {
97
- expect( select( store ).getDomainErrorMessage() ).toEqual( null );
98
- } );
99
-
100
- it( 'should return correct error message if no suggestions are returned', async () => {
101
- ( wpcomRequest as jest.Mock ).mockResolvedValue( null );
102
-
103
- const query = 'test query two';
104
- const listenForStateUpdate = () => {
105
- return new Promise< void >( ( resolve ) => {
106
- const unsubscribe = subscribe( () => {
107
- unsubscribe();
108
- resolve();
109
- } );
110
- } );
111
- };
112
-
113
- // The error message should start off as a null value
114
- expect( select( store ).getDomainErrorMessage() ).toBeFalsy();
115
-
116
- // First call returns undefined
117
- select( store ).getDomainSuggestions( query, options );
118
- await listenForStateUpdate();
119
-
120
- // By the second call, the resolver will have resolved
121
- select( store ).getDomainSuggestions( query, options );
122
- await listenForStateUpdate();
123
-
124
- // The promise resolves null suggestions so we should now have an error message
125
- expect( select( store ).getDomainErrorMessage() ).toBeTruthy();
126
- } );
127
- } );