rebase on oct-10-2023

This commit is contained in:
Rachit Bhargava
2023-10-10 17:23:21 -04:00
parent d37566ffb6
commit d096058d7d
4789 changed files with 254611 additions and 307223 deletions

View File

@@ -1,7 +1,11 @@
/**
* External dependencies
*/
import { getSetting, STORE_PAGES } from '@woocommerce/settings';
import {
getSetting,
STORE_PAGES,
LocaleSpecificAddressField,
} from '@woocommerce/settings';
export type WordCountType =
| 'words'
@@ -41,22 +45,69 @@ export const CART_URL = STORE_PAGES.cart.permalink;
export const LOGIN_URL = STORE_PAGES.myaccount.permalink
? STORE_PAGES.myaccount.permalink
: getSetting( 'wpLoginUrl', '/wp-login.php' );
export const SHIPPING_COUNTRIES = getSetting< Record< string, string > >(
'shippingCountries',
{}
);
export const ALLOWED_COUNTRIES = getSetting< Record< string, string > >(
'allowedCountries',
{}
);
export const SHIPPING_STATES = getSetting<
Record< string, Record< string, string > >
>( 'shippingStates', {} );
export const ALLOWED_STATES = getSetting< Record< string, string > >(
'allowedStates',
{}
);
export const LOCAL_PICKUP_ENABLED = getSetting< boolean >(
'localPickupEnabled',
false
);
type CountryData = {
allowBilling: boolean;
allowShipping: boolean;
states: Record< string, string >;
locale: Record< string, LocaleSpecificAddressField >;
};
// Contains country names.
const countries = getSetting< Record< string, string > >( 'countries', {} );
// Contains country settings.
const countryData = getSetting< Record< string, CountryData > >(
'countryData',
{}
);
export const ALLOWED_COUNTRIES = Object.fromEntries(
Object.keys( countryData )
.filter( ( countryCode ) => {
return countryData[ countryCode ].allowBilling === true;
} )
.map( ( countryCode ) => {
return [ countryCode, countries[ countryCode ] || '' ];
} )
);
export const ALLOWED_STATES = Object.fromEntries(
Object.keys( countryData )
.filter( ( countryCode ) => {
return countryData[ countryCode ].allowBilling === true;
} )
.map( ( countryCode ) => {
return [ countryCode, countryData[ countryCode ].states || [] ];
} )
);
export const SHIPPING_COUNTRIES = Object.fromEntries(
Object.keys( countryData )
.filter( ( countryCode ) => {
return countryData[ countryCode ].allowShipping === true;
} )
.map( ( countryCode ) => {
return [ countryCode, countries[ countryCode ] || '' ];
} )
);
export const SHIPPING_STATES = Object.fromEntries(
Object.keys( countryData )
.filter( ( countryCode ) => {
return countryData[ countryCode ].allowShipping === true;
} )
.map( ( countryCode ) => {
return [ countryCode, countryData[ countryCode ].states || [] ];
} )
);
export const COUNTRY_LOCALE = Object.fromEntries(
Object.keys( countryData ).map( ( countryCode ) => {
return [ countryCode, countryData[ countryCode ].locale || [] ];
} )
);

View File

@@ -20,8 +20,8 @@ export interface AddressField {
index: number;
}
export interface LocaleSpecificAddressField extends AddressField {
priority: number;
export interface LocaleSpecificAddressField extends Partial< AddressField > {
priority?: number | undefined;
}
export interface AddressFields {

View File

@@ -10,7 +10,7 @@ export const ADMIN_URL = allSettings.adminUrl;
export const COUNTRIES = allSettings.countries;
export const CURRENCY = allSettings.currency;
export const CURRENT_USER_IS_ADMIN = allSettings.currentUserIsAdmin as boolean;
export const HOME_URL = allSettings.homeUrl;
export const HOME_URL = allSettings.homeUrl as string | undefined;
export const LOCALE = allSettings.locale;
export const ORDER_STATUSES = allSettings.orderStatuses;
export const PLACEHOLDER_IMG_SRC = allSettings.placeholderImgSrc as string;

View File

@@ -21,7 +21,25 @@ export const getSetting = < T >(
filter = ( val: unknown, fb: unknown ) =>
typeof val !== 'undefined' ? val : fb
): T => {
const value = name in allSettings ? allSettings[ name ] : fallback;
let value = fallback;
if ( name in allSettings ) {
value = allSettings[ name ];
} else if ( name.includes( '_data' ) ) {
// This handles back compat with payment data _data properties after the move to camelCase and the dedicated
// paymentMethodData setting.
const nameWithoutData = name.replace( '_data', '' );
const paymentMethodData = getSetting(
'paymentMethodData',
{}
) as Record< string, unknown >;
value =
nameWithoutData in paymentMethodData
? paymentMethodData[ nameWithoutData ]
: fallback;
}
return filter( value, fallback ) as T;
};
@@ -106,3 +124,17 @@ export const isWcVersion = (
*/
export const getAdminLink = ( path: string ): string =>
getSetting( 'adminUrl' ) + path;
/**
* Get payment method data from the paymentMethodData setting.
*/
export const getPaymentMethodData = (
paymentMethodId: string,
defaultValue: null | unknown = null
) => {
const paymentMethodData = getSetting( 'paymentMethodData', {} ) as Record<
string,
unknown
>;
return paymentMethodData[ paymentMethodId ] ?? defaultValue;
};