2021-04-20 17:17:19 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { applyFilters } from '@wordpress/hooks';
|
|
|
|
import QueryString, { parse } from 'qs';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import { PaymentRecommendations } from '../payments';
|
2021-08-11 22:09:32 +00:00
|
|
|
import { ShippingRecommendations } from '../shipping';
|
2021-04-20 17:17:19 +00:00
|
|
|
import { EmbeddedBodyProps } from './embedded-body-props';
|
|
|
|
import './style.scss';
|
|
|
|
|
|
|
|
type QueryParams = EmbeddedBodyProps;
|
|
|
|
|
|
|
|
function isWPPage(
|
|
|
|
params: QueryParams | QueryString.ParsedQs
|
|
|
|
): params is QueryParams {
|
|
|
|
return ( params as QueryParams ).page !== undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
const EMBEDDED_BODY_COMPONENT_LIST: React.ElementType[] = [
|
|
|
|
PaymentRecommendations,
|
2021-08-11 22:09:32 +00:00
|
|
|
ShippingRecommendations,
|
2021-04-20 17:17:19 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This component is appended to the bottom of the WooCommerce non-react pages (like settings).
|
|
|
|
* You can add a component by writing a Fill component from slot-fill with the `embedded-body-layout` name.
|
|
|
|
*
|
|
|
|
* Each Fill component receives QueryParams, consisting of a page, tab, and section string.
|
|
|
|
*/
|
|
|
|
export const EmbeddedBodyLayout = () => {
|
|
|
|
const query = parse( location.search.substring( 1 ) );
|
|
|
|
let queryParams: QueryParams = { page: '', tab: '' };
|
|
|
|
if ( isWPPage( query ) ) {
|
|
|
|
queryParams = query;
|
|
|
|
}
|
2022-02-11 14:38:38 +00:00
|
|
|
/**
|
|
|
|
* Filter an array of body components for WooCommerce non-react pages.
|
|
|
|
*
|
|
|
|
* @filter woocommerce_admin_embedded_layout_components
|
|
|
|
* @param {Array.<Node>} embeddedBodyComponentList Array of body components.
|
2022-03-18 11:45:14 +00:00
|
|
|
* @param {Object} query url query parameters.
|
2022-02-11 14:38:38 +00:00
|
|
|
*/
|
2021-04-20 17:17:19 +00:00
|
|
|
const componentList = applyFilters(
|
|
|
|
'woocommerce_admin_embedded_layout_components',
|
|
|
|
EMBEDDED_BODY_COMPONENT_LIST,
|
|
|
|
queryParams
|
|
|
|
) as React.ElementType< EmbeddedBodyProps >[];
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div
|
|
|
|
className="woocommerce-embedded-layout__primary"
|
|
|
|
id="woocommerce-embedded-layout__primary"
|
|
|
|
>
|
|
|
|
{ componentList.map( ( Comp, index ) => {
|
|
|
|
return <Comp key={ index } { ...queryParams } />;
|
|
|
|
} ) }
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|