Back

0% Statements 0/4
0% Branches 0/2
0% Functions 0/2
0% Lines 0/4

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73                                                                                                                                                 
/**
 * External dependencies
 */
import {
	useWidgetRootContext,
	WidgetRoot,
	type ReportParamsFieldAttributes,
} from '@automattic/jetpack-premium-analytics-sdk';
import { __ } from '@wordpress/i18n';
/**
 * Internal dependencies
 */
import { ReportExecutionCard } from '../../src/components/report-execution-card';
import { useExecutedReport } from '../../src/reports/hooks/use-executed-report';
import type { ReportExecution } from '../../src/reports/fetch-report';
import type { WidgetRenderProps } from '@wordpress/widget-primitives';
 
/**
 * @property execution - The provider to force. Defaults to `auto`.
 */
export type PrototypeStockLocalAttributes = {
	execution?: ReportExecution;
};
 
type PrototypeStockLocalRenderAttributes = PrototypeStockLocalAttributes &
	Partial< ReportParamsFieldAttributes >;
 
/**
 * `stock.summary` with the `execution` attribute, read under the widget root for its report params.
 *
 * @param {object}          props             - The component props.
 * @param {ReportExecution} [props.execution] - The provider to force.
 * @return {JSX.Element} The card.
 */
function PrototypeStockLocal( { execution = 'auto' }: { execution?: ReportExecution } ) {
	const { reportParams } = useWidgetRootContext();
	const report = useExecutedReport( reportParams, {
		route: 'stock.summary',
		execution,
		send: [],
		fields: [ 'products', 'instock', 'lowstock', 'outofstock', 'onbackorder' ],
	} );
 
	return (
		<ReportExecutionCard
			report={ report }
			metrics={ [
				{ field: 'products', label: __( 'Products', 'jetpack-woocommerce-stats-pkg' ) },
				{ field: 'instock', label: __( 'In stock', 'jetpack-woocommerce-stats-pkg' ) },
				{ field: 'lowstock', label: __( 'Low stock', 'jetpack-woocommerce-stats-pkg' ) },
				{ field: 'outofstock', label: __( 'Out of stock', 'jetpack-woocommerce-stats-pkg' ) },
				{ field: 'onbackorder', label: __( 'On backorder', 'jetpack-woocommerce-stats-pkg' ) },
			] }
		/>
	);
}
 
/**
 * The Stock (local only) prototype widget.
 *
 * @param {WidgetRenderProps< PrototypeStockLocalRenderAttributes >} props - The props the host passes.
 * @return {JSX.Element} The widget.
 */
export default function PrototypeStockLocalRender( {
	attributes = {},
}: WidgetRenderProps< PrototypeStockLocalRenderAttributes > ) {
	return (
		<WidgetRoot attributes={ attributes }>
			<PrototypeStockLocal execution={ attributes.execution } />
		</WidgetRoot>
	);
}