2022-11-11 16:17:49 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { cleanForSlug } from '@wordpress/url';
|
2023-01-11 13:57:17 +00:00
|
|
|
import { __ } from '@wordpress/i18n';
|
2023-03-14 13:49:31 +00:00
|
|
|
import { isObject } from '@woocommerce/types';
|
|
|
|
import { getSetting } from '@woocommerce/settings';
|
2022-11-11 16:17:49 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import type {
|
|
|
|
PickupLocation,
|
|
|
|
SortablePickupLocation,
|
|
|
|
ShippingMethodSettings,
|
|
|
|
} from './types';
|
|
|
|
|
|
|
|
export const indexLocationsById = (
|
|
|
|
locations: PickupLocation[]
|
|
|
|
): SortablePickupLocation[] => {
|
|
|
|
return locations.map( ( value, index ) => {
|
|
|
|
return {
|
|
|
|
...value,
|
|
|
|
id: cleanForSlug( value.name ) + '-' + index,
|
|
|
|
};
|
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
|
|
|
export const defaultSettings = {
|
2022-12-23 12:23:06 +00:00
|
|
|
enabled: false,
|
2024-04-30 12:59:03 +00:00
|
|
|
title: __( 'Pickup', 'woocommerce' ),
|
2022-11-11 16:17:49 +00:00
|
|
|
tax_status: 'taxable',
|
|
|
|
cost: '',
|
|
|
|
};
|
|
|
|
|
2022-12-07 15:52:02 +00:00
|
|
|
export const defaultReadyOnlySettings = {
|
|
|
|
hasLegacyPickup: false,
|
2022-12-23 12:26:06 +00:00
|
|
|
storeCountry: '',
|
2023-03-14 13:49:31 +00:00
|
|
|
storeState: '',
|
2022-12-07 15:52:02 +00:00
|
|
|
};
|
|
|
|
declare global {
|
|
|
|
const hydratedScreenSettings: {
|
|
|
|
pickupLocationSettings: {
|
2024-04-09 13:31:04 +00:00
|
|
|
enabled: boolean;
|
2022-12-07 15:52:02 +00:00
|
|
|
title: string;
|
|
|
|
tax_status: string;
|
|
|
|
cost: string;
|
|
|
|
};
|
|
|
|
pickupLocations: PickupLocation[];
|
|
|
|
readonlySettings: typeof defaultReadyOnlySettings;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-11-11 16:17:49 +00:00
|
|
|
export const getInitialSettings = (): ShippingMethodSettings => {
|
2022-12-07 15:52:02 +00:00
|
|
|
const settings = hydratedScreenSettings.pickupLocationSettings;
|
2022-11-11 16:17:49 +00:00
|
|
|
|
|
|
|
return {
|
2024-04-09 13:31:04 +00:00
|
|
|
enabled:
|
|
|
|
typeof settings?.enabled === 'boolean'
|
|
|
|
? settings.enabled
|
|
|
|
: defaultSettings.enabled,
|
2022-11-11 16:17:49 +00:00
|
|
|
title: settings?.title || defaultSettings.title,
|
|
|
|
tax_status: settings?.tax_status || defaultSettings.tax_status,
|
|
|
|
cost: settings?.cost || defaultSettings.cost,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
export const getInitialPickupLocations = (): SortablePickupLocation[] =>
|
2022-12-07 15:52:02 +00:00
|
|
|
indexLocationsById( hydratedScreenSettings.pickupLocations || [] );
|
|
|
|
|
|
|
|
export const readOnlySettings =
|
|
|
|
hydratedScreenSettings.readonlySettings || defaultReadyOnlySettings;
|
2023-03-14 13:49:31 +00:00
|
|
|
|
|
|
|
export const countries = getSetting< Record< string, string > >(
|
|
|
|
'countries',
|
|
|
|
[]
|
|
|
|
);
|
|
|
|
export const states = getSetting< Record< string, Record< string, string > > >(
|
|
|
|
'countryStates',
|
|
|
|
[]
|
|
|
|
);
|
|
|
|
export const getUserFriendlyAddress = ( address: unknown ) => {
|
|
|
|
const updatedAddress = isObject( address ) && {
|
|
|
|
...address,
|
|
|
|
country:
|
|
|
|
typeof address.country === 'string' && countries[ address.country ],
|
|
|
|
state:
|
|
|
|
typeof address.country === 'string' &&
|
|
|
|
typeof address.state === 'string' &&
|
|
|
|
states[ address.country ]?.[ address.state ]
|
|
|
|
? states[ address.country ][ address.state ]
|
|
|
|
: address.state,
|
|
|
|
};
|
|
|
|
|
|
|
|
return Object.values( updatedAddress )
|
|
|
|
.filter( ( value ) => value !== '' )
|
|
|
|
.join( ', ' );
|
|
|
|
};
|
|
|
|
|
|
|
|
// Outputs the list of countries and states in a single dropdown select.
|
|
|
|
const countryStateDropdownOptions = () => {
|
|
|
|
const countryStateOptions = Object.keys( countries ).map( ( country ) => {
|
|
|
|
const countryStates = states[ country ] || {};
|
|
|
|
|
|
|
|
if ( Object.keys( countryStates ).length === 0 ) {
|
|
|
|
return {
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
value: country,
|
|
|
|
label: countries[ country ],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
const stateOptions = Object.keys( countryStates ).map( ( state ) => ( {
|
|
|
|
value: `${ country }:${ state }`,
|
|
|
|
label: `${ countries[ country ] } — ${ countryStates[ state ] }`,
|
|
|
|
} ) );
|
|
|
|
return {
|
|
|
|
label: countries[ country ],
|
|
|
|
options: [ ...stateOptions ],
|
|
|
|
};
|
|
|
|
} );
|
|
|
|
return {
|
|
|
|
options: countryStateOptions,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
export const countryStateOptions = countryStateDropdownOptions();
|