@woocommerce/data
Version:
WooCommerce Admin data store and utilities
55 lines (54 loc) • 1.63 kB
JavaScript
/**
* External dependencies
*/
import { apiFetch, select } from '@wordpress/data-controls';
import { controls } from '@wordpress/data';
import { getLocalesSuccess, getLocalesError, getCountriesSuccess, getCountriesError, } from './actions';
import { NAMESPACE } from '../constants';
import { STORE_NAME } from './constants';
const resolveSelect = controls && controls.resolveSelect ? controls.resolveSelect : select;
export function* getLocale() {
yield resolveSelect(STORE_NAME, 'getLocales');
}
export function* getLocales() {
try {
const url = NAMESPACE + '/data/countries/locales';
const results = yield apiFetch({
path: url,
method: 'GET',
});
return getLocalesSuccess(results);
}
catch (error) {
return getLocalesError(error);
}
}
export function* getCountry() {
yield resolveSelect(STORE_NAME, 'getCountries');
}
export function* getCountries() {
try {
const url = NAMESPACE + '/data/countries';
const results = yield apiFetch({
path: url,
method: 'GET',
});
return getCountriesSuccess(results);
}
catch (error) {
return getCountriesError(error);
}
}
export const geolocate = () => async ({ dispatch }) => {
try {
const url = `https://public-api.wordpress.com/geo/?v=${new Date().getTime()}`;
const response = await fetch(url, {
method: 'GET',
});
const result = await response.json();
dispatch.geolocationSuccess(result);
}
catch (error) {
dispatch.geolocationError(error);
}
};