2021-09-21 19:33:44 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { __ } from '@wordpress/i18n';
|
|
|
|
import { Button } from '@wordpress/components';
|
2022-01-14 13:39:25 +00:00
|
|
|
import { COUNTRIES_STORE_NAME } from '@woocommerce/data';
|
|
|
|
import { Fragment } from '@wordpress/element';
|
|
|
|
import { Form, Spinner } from '@woocommerce/components';
|
|
|
|
import { useSelect } from '@wordpress/data';
|
2021-09-21 19:33:44 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import {
|
|
|
|
StoreAddress,
|
2022-01-14 13:39:25 +00:00
|
|
|
getStoreAddressValidator,
|
2021-09-21 19:33:44 +00:00
|
|
|
} from '../../../dashboard/components/settings/general/store-address';
|
|
|
|
|
2022-01-14 13:39:25 +00:00
|
|
|
const StoreLocation = ( {
|
|
|
|
onComplete,
|
|
|
|
createNotice,
|
|
|
|
isSettingsError,
|
|
|
|
isSettingsRequesting,
|
|
|
|
updateAndPersistSettingsForGroup,
|
|
|
|
settings,
|
|
|
|
} ) => {
|
|
|
|
const { getLocale, hasFinishedResolution } = useSelect( ( select ) => {
|
2022-03-01 12:33:41 +00:00
|
|
|
const countryStore = select( COUNTRIES_STORE_NAME );
|
|
|
|
countryStore.getCountries();
|
2022-01-14 13:39:25 +00:00
|
|
|
return {
|
2022-03-01 12:33:41 +00:00
|
|
|
getLocale: countryStore.getLocale,
|
|
|
|
locales: countryStore.getLocales(),
|
|
|
|
hasFinishedResolution:
|
|
|
|
countryStore.hasFinishedResolution( 'getLocales' ) &&
|
|
|
|
countryStore.hasFinishedResolution( 'getCountries' ),
|
2022-01-14 13:39:25 +00:00
|
|
|
};
|
|
|
|
} );
|
|
|
|
const onSubmit = async ( values ) => {
|
2021-09-21 19:33:44 +00:00
|
|
|
await updateAndPersistSettingsForGroup( 'general', {
|
|
|
|
general: {
|
|
|
|
...settings,
|
|
|
|
woocommerce_store_address: values.addressLine1,
|
|
|
|
woocommerce_store_address_2: values.addressLine2,
|
|
|
|
woocommerce_default_country: values.countryState,
|
|
|
|
woocommerce_store_city: values.city,
|
|
|
|
woocommerce_store_postcode: values.postCode,
|
|
|
|
},
|
|
|
|
} );
|
|
|
|
|
|
|
|
if ( ! isSettingsError ) {
|
|
|
|
onComplete( values );
|
|
|
|
} else {
|
|
|
|
createNotice(
|
|
|
|
'error',
|
|
|
|
__(
|
|
|
|
'There was a problem saving your store location',
|
2022-03-30 09:00:04 +00:00
|
|
|
'woocommerce'
|
2021-09-21 19:33:44 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2022-01-14 13:39:25 +00:00
|
|
|
};
|
2021-09-21 19:33:44 +00:00
|
|
|
|
2022-01-14 13:39:25 +00:00
|
|
|
const getInitialValues = () => {
|
2021-09-21 19:33:44 +00:00
|
|
|
const {
|
|
|
|
woocommerce_store_address: storeAddress,
|
|
|
|
woocommerce_store_address_2: storeAddress2,
|
|
|
|
woocommerce_store_city: storeCity,
|
|
|
|
woocommerce_default_country: defaultCountry,
|
|
|
|
woocommerce_store_postcode: storePostcode,
|
|
|
|
} = settings;
|
|
|
|
|
|
|
|
return {
|
|
|
|
addressLine1: storeAddress || '',
|
|
|
|
addressLine2: storeAddress2 || '',
|
|
|
|
city: storeCity || '',
|
|
|
|
countryState: defaultCountry || '',
|
|
|
|
postCode: storePostcode || '',
|
|
|
|
};
|
2022-01-14 13:39:25 +00:00
|
|
|
};
|
2021-09-21 19:33:44 +00:00
|
|
|
|
2022-01-14 13:39:25 +00:00
|
|
|
const validate = ( values ) => {
|
|
|
|
const locale = getLocale( values.countryState );
|
|
|
|
const validator = getStoreAddressValidator( locale );
|
|
|
|
return validator( values );
|
|
|
|
};
|
2021-09-21 19:33:44 +00:00
|
|
|
|
2022-01-14 13:39:25 +00:00
|
|
|
if ( isSettingsRequesting || ! hasFinishedResolution ) {
|
|
|
|
return <Spinner />;
|
2021-09-21 19:33:44 +00:00
|
|
|
}
|
2022-01-14 13:39:25 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<Form
|
|
|
|
initialValues={ getInitialValues() }
|
|
|
|
onSubmit={ onSubmit }
|
|
|
|
validate={ validate }
|
|
|
|
>
|
|
|
|
{ ( { getInputProps, handleSubmit, setValue } ) => (
|
|
|
|
<Fragment>
|
|
|
|
<StoreAddress
|
|
|
|
getInputProps={ getInputProps }
|
|
|
|
setValue={ setValue }
|
|
|
|
/>
|
|
|
|
<Button isPrimary onClick={ handleSubmit }>
|
2022-03-30 09:00:04 +00:00
|
|
|
{ __( 'Continue', 'woocommerce' ) }
|
2022-01-14 13:39:25 +00:00
|
|
|
</Button>
|
|
|
|
</Fragment>
|
|
|
|
) }
|
|
|
|
</Form>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default StoreLocation;
|