2020-11-25 18:51:15 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { Component } from '@wordpress/element';
|
|
|
|
import { compose } from '@wordpress/compose';
|
|
|
|
import { withDispatch, withSelect } from '@wordpress/data';
|
|
|
|
import PropTypes from 'prop-types';
|
2024-06-17 05:05:11 +00:00
|
|
|
import { Section } from '@woocommerce/components';
|
2020-11-25 18:51:15 +00:00
|
|
|
import { ITEMS_STORE_NAME } from '@woocommerce/data';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2021-12-14 16:56:42 +00:00
|
|
|
import { ActivityCardPlaceholder } from '~/activity-panel/activity-card';
|
2020-11-25 18:51:15 +00:00
|
|
|
import { ProductStockCard } from './card';
|
2020-12-03 20:20:13 +00:00
|
|
|
import { getLowStockCountQuery } from '../orders/utils';
|
2020-11-25 18:51:15 +00:00
|
|
|
|
|
|
|
const productsQuery = {
|
|
|
|
page: 1,
|
|
|
|
per_page: 5,
|
|
|
|
status: 'publish',
|
|
|
|
_fields: [
|
|
|
|
'attributes',
|
|
|
|
'id',
|
|
|
|
'images',
|
|
|
|
'last_order_date',
|
|
|
|
'low_stock_amount',
|
|
|
|
'name',
|
|
|
|
'parent_id',
|
|
|
|
'stock_quantity',
|
|
|
|
'type',
|
|
|
|
],
|
|
|
|
};
|
|
|
|
export class StockPanel extends Component {
|
|
|
|
constructor( props ) {
|
|
|
|
super( props );
|
|
|
|
|
|
|
|
this.updateStock = this.updateStock.bind( this );
|
|
|
|
}
|
|
|
|
|
|
|
|
async updateStock( product, quantity ) {
|
2023-11-09 04:49:50 +00:00
|
|
|
const { invalidateResolution, updateProductStock } = this.props;
|
2020-11-30 15:12:08 +00:00
|
|
|
const success = await updateProductStock( product, quantity );
|
2020-11-25 18:51:15 +00:00
|
|
|
|
2020-11-30 15:12:08 +00:00
|
|
|
if ( success ) {
|
2020-11-25 18:51:15 +00:00
|
|
|
// Request more low stock products.
|
2021-07-20 23:47:22 +00:00
|
|
|
invalidateResolution( 'getItems', [
|
|
|
|
'products/low-in-stock',
|
|
|
|
productsQuery,
|
|
|
|
] );
|
2023-11-09 04:49:50 +00:00
|
|
|
invalidateResolution( 'getItemsTotalCount', [
|
|
|
|
'products/count-low-in-stock',
|
|
|
|
getLowStockCountQuery,
|
|
|
|
null,
|
|
|
|
] );
|
2020-11-25 18:51:15 +00:00
|
|
|
}
|
|
|
|
|
2020-11-30 15:12:08 +00:00
|
|
|
return success;
|
2020-11-25 18:51:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
renderProducts() {
|
|
|
|
const { products, createNotice } = this.props;
|
|
|
|
|
|
|
|
return products.map( ( product ) => (
|
|
|
|
<ProductStockCard
|
|
|
|
key={ product.id }
|
|
|
|
product={ product }
|
|
|
|
updateProductStock={ this.updateStock }
|
|
|
|
createNotice={ createNotice }
|
|
|
|
/>
|
|
|
|
) );
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2022-06-21 08:37:34 +00:00
|
|
|
const { lowStockProductsCount, isError, isRequesting, products } =
|
|
|
|
this.props;
|
2020-11-25 18:51:15 +00:00
|
|
|
|
|
|
|
if ( isError ) {
|
2024-06-17 05:05:11 +00:00
|
|
|
throw new Error(
|
|
|
|
'Failed to load low stock products, Raise error to trigger ErrorBoundary'
|
2020-11-25 18:51:15 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Show placeholders only for the first products fetch.
|
2021-06-09 13:56:45 +00:00
|
|
|
if ( isRequesting || ! products.length ) {
|
2022-01-19 16:45:17 +00:00
|
|
|
const numPlaceholders = Math.min( 5, lowStockProductsCount ?? 1 );
|
2022-06-21 08:37:34 +00:00
|
|
|
const placeholders = Array.from( new Array( numPlaceholders ) ).map(
|
|
|
|
( v, idx ) => (
|
|
|
|
<ActivityCardPlaceholder
|
|
|
|
key={ idx }
|
|
|
|
className="woocommerce-stock-activity-card"
|
|
|
|
hasAction
|
|
|
|
lines={ 1 }
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
);
|
2020-11-25 18:51:15 +00:00
|
|
|
|
|
|
|
return <Section>{ placeholders }</Section>;
|
|
|
|
}
|
|
|
|
|
|
|
|
return <Section>{ this.renderProducts() }</Section>;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
StockPanel.propTypes = {
|
2022-01-19 16:45:17 +00:00
|
|
|
lowStockProductsCount: PropTypes.number,
|
2020-11-25 18:51:15 +00:00
|
|
|
products: PropTypes.array.isRequired,
|
|
|
|
isError: PropTypes.bool,
|
|
|
|
isRequesting: PropTypes.bool,
|
|
|
|
};
|
|
|
|
|
|
|
|
StockPanel.defaultProps = {
|
|
|
|
products: [],
|
|
|
|
isError: false,
|
|
|
|
isRequesting: false,
|
|
|
|
};
|
|
|
|
|
|
|
|
export default compose(
|
|
|
|
withSelect( ( select ) => {
|
2022-06-21 08:37:34 +00:00
|
|
|
const { getItems, getItemsError, isResolving } =
|
|
|
|
select( ITEMS_STORE_NAME );
|
2020-11-25 18:51:15 +00:00
|
|
|
|
|
|
|
const products = Array.from(
|
2021-07-20 23:47:22 +00:00
|
|
|
getItems( 'products/low-in-stock', productsQuery ).values()
|
|
|
|
);
|
|
|
|
const isError = Boolean(
|
|
|
|
getItemsError( 'products/low-in-stock', productsQuery )
|
2020-11-25 18:51:15 +00:00
|
|
|
);
|
|
|
|
const isRequesting = isResolving( 'getItems', [
|
2021-07-20 23:47:22 +00:00
|
|
|
'products/low-in-stock',
|
2020-11-25 18:51:15 +00:00
|
|
|
productsQuery,
|
|
|
|
] );
|
|
|
|
|
|
|
|
return { products, isError, isRequesting };
|
|
|
|
} ),
|
|
|
|
withDispatch( ( dispatch ) => {
|
2022-06-21 08:37:34 +00:00
|
|
|
const { invalidateResolution, updateProductStock } =
|
|
|
|
dispatch( ITEMS_STORE_NAME );
|
2020-11-25 18:51:15 +00:00
|
|
|
const { createNotice } = dispatch( 'core/notices' );
|
|
|
|
|
|
|
|
return {
|
|
|
|
createNotice,
|
|
|
|
invalidateResolution,
|
|
|
|
updateProductStock,
|
|
|
|
};
|
|
|
|
} )
|
|
|
|
)( StockPanel );
|