2018-07-09 15:46:31 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2019-03-27 16:43:31 +00:00
|
|
|
import { __ } from '@wordpress/i18n';
|
2019-03-27 00:33:04 +00:00
|
|
|
import { Component, Fragment } from '@wordpress/element';
|
2019-04-01 21:34:17 +00:00
|
|
|
import { compose } from '@wordpress/compose';
|
2020-08-20 23:37:41 +00:00
|
|
|
import { withSelect } from '@wordpress/data';
|
2019-04-01 21:34:17 +00:00
|
|
|
import PropTypes from 'prop-types';
|
2020-11-18 20:52:24 +00:00
|
|
|
import CheckmarkIcon from 'gridicons/dist/checkmark';
|
2019-04-01 21:34:17 +00:00
|
|
|
import { EmptyContent, Section } from '@woocommerce/components';
|
2020-08-20 23:37:41 +00:00
|
|
|
import { QUERY_DEFAULTS, ITEMS_STORE_NAME } from '@woocommerce/data';
|
2018-07-09 15:46:31 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2019-04-15 07:35:22 +00:00
|
|
|
import { ActivityCard, ActivityCardPlaceholder } from '../../activity-card';
|
2019-03-27 16:20:40 +00:00
|
|
|
import ActivityHeader from '../../activity-header';
|
2019-03-27 16:43:31 +00:00
|
|
|
import ProductStockCard from './card';
|
2018-07-09 15:46:31 +00:00
|
|
|
|
|
|
|
class StockPanel extends Component {
|
2019-04-15 07:35:22 +00:00
|
|
|
renderEmptyCard() {
|
|
|
|
return (
|
|
|
|
<ActivityCard
|
2019-06-12 21:51:08 +00:00
|
|
|
className="woocommerce-empty-activity-card"
|
2020-02-14 02:23:21 +00:00
|
|
|
title={ __(
|
|
|
|
'Your stock is in good shape.',
|
|
|
|
'woocommerce-admin'
|
|
|
|
) }
|
2020-11-18 20:52:24 +00:00
|
|
|
icon={ <CheckmarkIcon size={ 48 } /> }
|
2019-04-15 07:35:22 +00:00
|
|
|
>
|
2020-02-14 02:23:21 +00:00
|
|
|
{ __(
|
|
|
|
'You currently have no products running low on stock.',
|
|
|
|
'woocommerce-admin'
|
|
|
|
) }
|
2019-04-15 07:35:22 +00:00
|
|
|
</ActivityCard>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
renderProducts() {
|
|
|
|
const { products } = this.props;
|
|
|
|
|
|
|
|
if ( products.length === 0 ) {
|
|
|
|
return this.renderEmptyCard();
|
|
|
|
}
|
|
|
|
|
2020-02-14 02:23:21 +00:00
|
|
|
return products.map( ( product ) => (
|
|
|
|
<ProductStockCard key={ product.id } product={ product } />
|
|
|
|
) );
|
2019-04-15 07:35:22 +00:00
|
|
|
}
|
|
|
|
|
2018-07-09 15:46:31 +00:00
|
|
|
render() {
|
2019-04-01 21:34:17 +00:00
|
|
|
const { isError, isRequesting, products } = this.props;
|
|
|
|
|
|
|
|
if ( isError ) {
|
|
|
|
const title = __(
|
|
|
|
'There was an error getting your low stock products. Please try again.',
|
|
|
|
'woocommerce-admin'
|
|
|
|
);
|
|
|
|
const actionLabel = __( 'Reload', 'woocommerce-admin' );
|
|
|
|
const actionCallback = () => {
|
|
|
|
// @todo Add tracking for how often an error is displayed, and the reload action is clicked.
|
|
|
|
window.location.reload();
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Fragment>
|
|
|
|
<EmptyContent
|
|
|
|
title={ title }
|
|
|
|
actionLabel={ actionLabel }
|
|
|
|
actionURL={ null }
|
|
|
|
actionCallback={ actionCallback }
|
|
|
|
/>
|
|
|
|
</Fragment>
|
|
|
|
);
|
|
|
|
}
|
2019-03-27 00:33:04 +00:00
|
|
|
|
2019-04-15 07:35:22 +00:00
|
|
|
const title =
|
|
|
|
isRequesting || products.length > 0
|
|
|
|
? __( 'Stock', 'woocommerce-admin' )
|
|
|
|
: __( 'No products with low stock', 'woocommerce-admin' );
|
2019-03-27 00:33:04 +00:00
|
|
|
return (
|
|
|
|
<Fragment>
|
2019-04-15 07:35:22 +00:00
|
|
|
<ActivityHeader title={ title } />
|
2019-03-27 00:33:04 +00:00
|
|
|
<Section>
|
2019-03-27 16:43:31 +00:00
|
|
|
{ isRequesting ? (
|
|
|
|
<ActivityCardPlaceholder
|
|
|
|
className="woocommerce-stock-activity-card"
|
|
|
|
hasAction
|
|
|
|
lines={ 1 }
|
|
|
|
/>
|
|
|
|
) : (
|
2019-04-15 07:35:22 +00:00
|
|
|
this.renderProducts()
|
2019-03-27 16:43:31 +00:00
|
|
|
) }
|
2019-03-27 00:33:04 +00:00
|
|
|
</Section>
|
|
|
|
</Fragment>
|
|
|
|
);
|
2018-07-09 15:46:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-01 21:34:17 +00:00
|
|
|
StockPanel.propTypes = {
|
|
|
|
products: PropTypes.array.isRequired,
|
|
|
|
isError: PropTypes.bool,
|
|
|
|
isRequesting: PropTypes.bool,
|
|
|
|
};
|
|
|
|
|
|
|
|
StockPanel.defaultProps = {
|
|
|
|
products: [],
|
|
|
|
isError: false,
|
|
|
|
isRequesting: false,
|
|
|
|
};
|
|
|
|
|
|
|
|
export default compose(
|
2020-02-14 02:23:21 +00:00
|
|
|
withSelect( ( select ) => {
|
2020-08-20 23:37:41 +00:00
|
|
|
const { getItems, getItemsError, isResolving } = select(
|
|
|
|
ITEMS_STORE_NAME
|
2020-02-14 02:23:21 +00:00
|
|
|
);
|
2019-04-01 21:34:17 +00:00
|
|
|
|
|
|
|
const productsQuery = {
|
|
|
|
page: 1,
|
|
|
|
per_page: QUERY_DEFAULTS.pageSize,
|
|
|
|
low_in_stock: true,
|
|
|
|
status: 'publish',
|
|
|
|
};
|
|
|
|
|
2020-02-14 02:23:21 +00:00
|
|
|
const products = Array.from(
|
|
|
|
getItems( 'products', productsQuery ).values()
|
|
|
|
);
|
2019-04-01 21:34:17 +00:00
|
|
|
const isError = Boolean( getItemsError( 'products', productsQuery ) );
|
2020-08-20 23:37:41 +00:00
|
|
|
const isRequesting = isResolving( 'getItems', [
|
|
|
|
'products',
|
|
|
|
productsQuery,
|
|
|
|
] );
|
2019-04-01 21:34:17 +00:00
|
|
|
|
|
|
|
return { products, isError, isRequesting };
|
|
|
|
} )
|
|
|
|
)( StockPanel );
|