2019-09-25 14:22:36 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { Component } from '@wordpress/element';
|
|
|
|
import { createHigherOrderComponent } from '@wordpress/compose';
|
|
|
|
import isShallowEqual from '@wordpress/is-shallow-equal';
|
2020-09-02 08:21:46 +00:00
|
|
|
import { getProductVariations } from '@woocommerce/editor-components/utils';
|
2024-02-26 13:26:21 +00:00
|
|
|
import { ErrorObject } from '@woocommerce/editor-components/error-placeholder';
|
|
|
|
import {
|
|
|
|
ProductResponseItem,
|
|
|
|
ProductResponseVariationsItem,
|
|
|
|
} from '@woocommerce/types';
|
2019-09-25 14:22:36 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2023-08-30 06:56:28 +00:00
|
|
|
import { formatError } from '../base/utils/errors';
|
2019-09-25 14:22:36 +00:00
|
|
|
|
2024-02-26 13:26:21 +00:00
|
|
|
interface WithProductVariationsProps {
|
|
|
|
selected: number[];
|
|
|
|
showVariations: boolean;
|
|
|
|
products: ProductResponseItem[];
|
|
|
|
isLoading?: boolean;
|
|
|
|
error?: ErrorObject;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface State {
|
|
|
|
error: ErrorObject | null;
|
|
|
|
loading: boolean;
|
|
|
|
variations: { [ key: string ]: ProductResponseVariationsItem[] | null };
|
|
|
|
}
|
|
|
|
|
2019-12-10 17:17:46 +00:00
|
|
|
/**
|
|
|
|
* HOC that queries variations for a component.
|
|
|
|
*
|
2024-02-26 13:26:21 +00:00
|
|
|
* @param OriginalComponent Component being wrapped.
|
2019-12-10 17:17:46 +00:00
|
|
|
*/
|
2019-09-25 14:22:36 +00:00
|
|
|
const withProductVariations = createHigherOrderComponent(
|
2024-02-26 13:26:21 +00:00
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
|
|
// @ts-ignore ignoring this line because @wordpress/compose does not expose the correct type for createHigherOrderComponent
|
2019-09-25 14:22:36 +00:00
|
|
|
( OriginalComponent ) => {
|
2024-02-26 13:26:21 +00:00
|
|
|
class WrappedComponent extends Component<
|
|
|
|
WithProductVariationsProps,
|
|
|
|
State
|
|
|
|
> {
|
|
|
|
state: State = {
|
2019-09-25 14:22:36 +00:00
|
|
|
error: null,
|
|
|
|
loading: false,
|
|
|
|
variations: {},
|
|
|
|
};
|
|
|
|
|
2024-02-26 13:26:21 +00:00
|
|
|
private prevSelectedItem?: number;
|
|
|
|
|
2019-09-25 14:22:36 +00:00
|
|
|
componentDidMount() {
|
|
|
|
const { selected, showVariations } = this.props;
|
|
|
|
|
|
|
|
if ( selected && showVariations ) {
|
|
|
|
this.loadVariations();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-26 13:26:21 +00:00
|
|
|
componentDidUpdate( prevProps: WithProductVariationsProps ) {
|
2019-09-25 14:22:36 +00:00
|
|
|
const { isLoading, selected, showVariations } = this.props;
|
|
|
|
|
|
|
|
if (
|
|
|
|
showVariations &&
|
|
|
|
( ! isShallowEqual( prevProps.selected, selected ) ||
|
|
|
|
( prevProps.isLoading && ! isLoading ) )
|
|
|
|
) {
|
|
|
|
this.loadVariations();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
loadVariations = () => {
|
|
|
|
const { products } = this.props;
|
|
|
|
const { loading, variations } = this.state;
|
|
|
|
|
|
|
|
if ( loading ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const expandedProduct = this.getExpandedProduct();
|
|
|
|
|
|
|
|
if ( ! expandedProduct || variations[ expandedProduct ] ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const productDetails = products.find(
|
|
|
|
( findProduct ) => findProduct.id === expandedProduct
|
|
|
|
);
|
|
|
|
|
|
|
|
if (
|
2024-02-26 13:26:21 +00:00
|
|
|
! productDetails?.variations ||
|
2019-09-25 14:22:36 +00:00
|
|
|
productDetails.variations.length === 0
|
|
|
|
) {
|
|
|
|
this.setState( {
|
|
|
|
variations: {
|
|
|
|
...this.state.variations,
|
|
|
|
[ expandedProduct ]: null,
|
|
|
|
},
|
|
|
|
loading: false,
|
|
|
|
error: null,
|
|
|
|
} );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.setState( { loading: true } );
|
|
|
|
|
2024-02-26 13:26:21 +00:00
|
|
|
(
|
|
|
|
getProductVariations( expandedProduct ) as Promise<
|
|
|
|
ProductResponseVariationsItem[]
|
|
|
|
>
|
|
|
|
)
|
2019-09-25 14:22:36 +00:00
|
|
|
.then( ( expandedProductVariations ) => {
|
|
|
|
const newVariations = expandedProductVariations.map(
|
|
|
|
( variation ) => ( {
|
|
|
|
...variation,
|
|
|
|
parent: expandedProduct,
|
|
|
|
} )
|
|
|
|
);
|
|
|
|
this.setState( {
|
|
|
|
variations: {
|
|
|
|
...this.state.variations,
|
|
|
|
[ expandedProduct ]: newVariations,
|
|
|
|
},
|
|
|
|
loading: false,
|
|
|
|
error: null,
|
|
|
|
} );
|
|
|
|
} )
|
|
|
|
.catch( async ( e ) => {
|
|
|
|
const error = await formatError( e );
|
|
|
|
|
|
|
|
this.setState( {
|
|
|
|
variations: {
|
|
|
|
...this.state.variations,
|
|
|
|
[ expandedProduct ]: null,
|
|
|
|
},
|
|
|
|
loading: false,
|
|
|
|
error,
|
|
|
|
} );
|
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
2024-02-26 13:26:21 +00:00
|
|
|
isProductId( itemId: number ) {
|
2019-09-25 14:22:36 +00:00
|
|
|
const { products } = this.props;
|
|
|
|
return products.some( ( p ) => p.id === itemId );
|
|
|
|
}
|
|
|
|
|
2024-02-26 13:26:21 +00:00
|
|
|
findParentProduct( variationId: number ) {
|
2019-09-25 14:22:36 +00:00
|
|
|
const { products } = this.props;
|
|
|
|
const parentProduct = products.filter(
|
|
|
|
( p ) =>
|
2020-06-19 14:51:09 +00:00
|
|
|
p.variations &&
|
|
|
|
p.variations.find( ( { id } ) => id === variationId )
|
2019-09-25 14:22:36 +00:00
|
|
|
);
|
2022-07-05 07:24:28 +00:00
|
|
|
return parentProduct[ 0 ]?.id;
|
2019-09-25 14:22:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
getExpandedProduct() {
|
|
|
|
const { isLoading, selected, showVariations } = this.props;
|
|
|
|
|
|
|
|
if ( ! showVariations ) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
let selectedItem =
|
|
|
|
selected && selected.length ? selected[ 0 ] : null;
|
|
|
|
|
|
|
|
// If there is no selected item, check if there was one in the past, so we
|
|
|
|
// can keep the same product expanded.
|
|
|
|
if ( selectedItem ) {
|
|
|
|
this.prevSelectedItem = selectedItem;
|
2024-02-26 13:26:21 +00:00
|
|
|
} else if (
|
|
|
|
this.prevSelectedItem &&
|
|
|
|
! isLoading &&
|
|
|
|
! this.isProductId( this.prevSelectedItem )
|
|
|
|
) {
|
2019-09-25 14:22:36 +00:00
|
|
|
// If previous selected item was a variation
|
2024-02-26 13:26:21 +00:00
|
|
|
selectedItem = this.prevSelectedItem;
|
2019-09-25 14:22:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! isLoading && selectedItem ) {
|
|
|
|
return this.isProductId( selectedItem )
|
|
|
|
? selectedItem
|
|
|
|
: this.findParentProduct( selectedItem );
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
const { error: propsError, isLoading } = this.props;
|
|
|
|
const { error, loading, variations } = this.state;
|
|
|
|
|
|
|
|
return (
|
2024-02-26 13:26:21 +00:00
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
|
|
// @ts-ignore ignoring this line because @wordpress/compose does not expose the correct type for createHigherOrderComponent
|
2019-09-25 14:22:36 +00:00
|
|
|
<OriginalComponent
|
|
|
|
{ ...this.props }
|
|
|
|
error={ error || propsError }
|
|
|
|
expandedProduct={ this.getExpandedProduct() }
|
|
|
|
isLoading={ isLoading }
|
|
|
|
variations={ variations }
|
|
|
|
variationsLoading={ loading }
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2024-02-26 13:26:21 +00:00
|
|
|
|
2019-09-25 14:22:36 +00:00
|
|
|
return WrappedComponent;
|
|
|
|
},
|
|
|
|
'withProductVariations'
|
|
|
|
);
|
|
|
|
|
|
|
|
export default withProductVariations;
|