File size: 1,352 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
import { I18N_LOCALE_SUGGESTIONS_REQUEST } from 'calypso/state/action-types';
import { registerHandlers } from 'calypso/state/data-layer/handler-registry';
import { http } from 'calypso/state/data-layer/wpcom-http/actions';
import { dispatchRequest } from 'calypso/state/data-layer/wpcom-http/utils';
import { receiveLocaleSuggestions } from 'calypso/state/i18n/locale-suggestions/actions';

const noop = () => {};

/**
 * @module state/data-layer/wpcom/locale-guess
 */

/**
 * Dispatches a request to /locale-guess to fetch locale suggestions
 * @param {Object} action Redux action
 * @returns {Object} WordPress.com API HTTP Request action object
 */
export const fetchLocaleSuggestions = ( action ) =>
	http(
		{
			apiVersion: '1.1',
			method: 'GET',
			path: '/locale-guess',
		},
		action
	);

/**
 * Dispatches returned locale suggestions data
 * @param {Object} action Redux action
 * @param {Array} data raw data from /locale-guess
 * @returns {Object} Redux action
 */
export const addLocaleSuggestions = ( action, data ) => receiveLocaleSuggestions( data );

export const dispatchPlansRequest = dispatchRequest( {
	fetch: fetchLocaleSuggestions,
	onSuccess: addLocaleSuggestions,
	onError: noop,
} );

registerHandlers( 'state/data-layer/wpcom/locale-guess/index.js', {
	[ I18N_LOCALE_SUGGESTIONS_REQUEST ]: [ dispatchPlansRequest ],
} );