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';
|
2023-03-31 10:43:16 +00:00
|
|
|
import { Form, FormContextType, Spinner } from '@woocommerce/components';
|
2022-01-14 13:39:25 +00:00
|
|
|
import { useSelect } from '@wordpress/data';
|
2023-03-30 19:07:58 +00:00
|
|
|
import { Status, Options } from 'wordpress__notices';
|
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';
|
|
|
|
|
2023-05-09 09:01:37 +00:00
|
|
|
export type FormValues = {
|
2023-03-30 19:07:58 +00:00
|
|
|
addressLine1: string;
|
|
|
|
addressLine2: string;
|
|
|
|
countryState: string;
|
|
|
|
city: string;
|
|
|
|
postCode: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
type StoreLocationProps = {
|
|
|
|
onComplete: ( values: FormValues ) => void;
|
|
|
|
createNotice: (
|
|
|
|
status: Status | undefined,
|
|
|
|
content: string,
|
|
|
|
options?: Partial< Options >
|
|
|
|
) => void;
|
|
|
|
isSettingsError: boolean;
|
|
|
|
isSettingsRequesting: boolean;
|
|
|
|
buttonText?: string;
|
|
|
|
updateAndPersistSettingsForGroup: (
|
|
|
|
group: string,
|
|
|
|
data: {
|
|
|
|
[ key: string ]: unknown;
|
|
|
|
} & {
|
|
|
|
general?: {
|
|
|
|
[ key: string ]: string;
|
|
|
|
};
|
|
|
|
tax?: {
|
|
|
|
[ key: string ]: string;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
) => void;
|
|
|
|
settings?: {
|
|
|
|
[ key: string ]: string;
|
|
|
|
};
|
2023-05-09 09:01:37 +00:00
|
|
|
validate?: ( values: FormValues ) => { [ key: string ]: string };
|
|
|
|
};
|
|
|
|
|
|
|
|
export const defaultValidate = ( values: FormValues ) => {
|
|
|
|
const validator = getStoreAddressValidator();
|
|
|
|
return validator( values );
|
2023-03-30 19:07:58 +00:00
|
|
|
};
|
|
|
|
|
2022-01-14 13:39:25 +00:00
|
|
|
const StoreLocation = ( {
|
|
|
|
onComplete,
|
|
|
|
createNotice,
|
|
|
|
isSettingsError,
|
|
|
|
isSettingsRequesting,
|
|
|
|
updateAndPersistSettingsForGroup,
|
|
|
|
settings,
|
2022-06-29 07:40:42 +00:00
|
|
|
buttonText = __( 'Continue', 'woocommerce' ),
|
2023-05-09 09:01:37 +00:00
|
|
|
validate = defaultValidate,
|
2023-03-30 19:07:58 +00:00
|
|
|
}: StoreLocationProps ) => {
|
|
|
|
const { 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
|
|
|
};
|
|
|
|
} );
|
2023-03-30 19:07:58 +00:00
|
|
|
const onSubmit = async ( values: FormValues ) => {
|
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
|
|
|
return {
|
2023-03-30 19:07:58 +00:00
|
|
|
addressLine1: settings?.woocommerce_store_address || '',
|
|
|
|
addressLine2: settings?.woocommerce_store_address_2 || '',
|
|
|
|
city: settings?.woocommerce_store_city || '',
|
|
|
|
countryState: settings?.woocommerce_default_country || '',
|
|
|
|
postCode: settings?.woocommerce_store_postcode || '',
|
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
|
|
|
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 }
|
|
|
|
>
|
2023-03-30 19:07:58 +00:00
|
|
|
{ ( {
|
|
|
|
getInputProps,
|
|
|
|
handleSubmit,
|
|
|
|
setValue,
|
2023-03-31 10:43:16 +00:00
|
|
|
}: FormContextType< FormValues > ) => (
|
2022-01-14 13:39:25 +00:00
|
|
|
<Fragment>
|
|
|
|
<StoreAddress
|
2023-03-30 19:07:58 +00:00
|
|
|
// @ts-expect-error return type doesn't match, but they do work. We should revisit and refactor them in a follow up issue.
|
2022-01-14 13:39:25 +00:00
|
|
|
getInputProps={ getInputProps }
|
|
|
|
setValue={ setValue }
|
|
|
|
/>
|
|
|
|
<Button isPrimary onClick={ handleSubmit }>
|
2022-06-29 07:40:42 +00:00
|
|
|
{ buttonText }
|
2022-01-14 13:39:25 +00:00
|
|
|
</Button>
|
|
|
|
</Fragment>
|
|
|
|
) }
|
|
|
|
</Form>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default StoreLocation;
|