2019-03-27 16:43:31 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { __, sprintf } from '@wordpress/i18n';
|
2019-04-04 00:05:19 +00:00
|
|
|
import { BaseControl, Button } from '@wordpress/components';
|
2019-06-12 21:51:08 +00:00
|
|
|
import classnames from 'classnames';
|
2019-03-27 17:59:29 +00:00
|
|
|
import { Component, Fragment } from '@wordpress/element';
|
2019-03-28 17:39:55 +00:00
|
|
|
import { compose } from '@wordpress/compose';
|
2019-06-18 15:58:10 +00:00
|
|
|
import { ESCAPE } from '@wordpress/keycodes';
|
2019-06-12 21:51:08 +00:00
|
|
|
import { get } from 'lodash';
|
2019-03-28 17:39:55 +00:00
|
|
|
import { withDispatch } from '@wordpress/data';
|
2019-03-27 16:43:31 +00:00
|
|
|
import { Link, ProductImage } from '@woocommerce/components';
|
2019-10-07 11:51:25 +00:00
|
|
|
import { getSetting } from '@woocommerce/wc-admin-settings';
|
2019-03-27 16:43:31 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import { ActivityCard } from '../../activity-card';
|
2020-08-13 02:05:22 +00:00
|
|
|
import { recordEvent } from '../../../../lib/tracks';
|
2019-03-27 16:43:31 +00:00
|
|
|
|
|
|
|
class ProductStockCard extends Component {
|
2019-03-27 17:59:29 +00:00
|
|
|
constructor( props ) {
|
|
|
|
super( props );
|
|
|
|
this.state = {
|
|
|
|
quantity: props.product.stock_quantity,
|
|
|
|
editing: false,
|
2019-06-27 09:21:43 +00:00
|
|
|
edited: false,
|
2019-03-27 17:59:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
this.beginEdit = this.beginEdit.bind( this );
|
|
|
|
this.cancelEdit = this.cancelEdit.bind( this );
|
|
|
|
this.onQuantityChange = this.onQuantityChange.bind( this );
|
2019-06-18 15:58:10 +00:00
|
|
|
this.handleKeyDown = this.handleKeyDown.bind( this );
|
2019-06-27 09:21:43 +00:00
|
|
|
this.onSubmit = this.onSubmit.bind( this );
|
2019-04-04 00:05:19 +00:00
|
|
|
}
|
|
|
|
|
2020-08-03 16:38:57 +00:00
|
|
|
recordStockEvent( eventName, eventProps = {} ) {
|
|
|
|
recordEvent( `activity_panel_stock_${ eventName }`, eventProps );
|
|
|
|
}
|
|
|
|
|
2019-03-27 17:59:29 +00:00
|
|
|
beginEdit() {
|
2019-06-27 09:21:43 +00:00
|
|
|
const { product } = this.props;
|
|
|
|
|
|
|
|
this.setState(
|
|
|
|
{
|
|
|
|
editing: true,
|
|
|
|
quantity: product.stock_quantity,
|
|
|
|
},
|
|
|
|
() => {
|
2020-02-14 02:23:21 +00:00
|
|
|
if ( this.quantityInput ) {
|
|
|
|
this.quantityInput.focus();
|
|
|
|
}
|
2019-06-27 09:21:43 +00:00
|
|
|
}
|
|
|
|
);
|
2020-08-03 16:38:57 +00:00
|
|
|
this.recordStockEvent( 'update_stock' );
|
2019-03-27 17:59:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cancelEdit() {
|
2019-06-27 09:21:43 +00:00
|
|
|
const { product } = this.props;
|
|
|
|
|
2019-03-27 17:59:29 +00:00
|
|
|
this.setState( {
|
|
|
|
editing: false,
|
2019-06-27 09:21:43 +00:00
|
|
|
quantity: product.stock_quantity,
|
2019-03-27 17:59:29 +00:00
|
|
|
} );
|
2020-08-03 16:38:57 +00:00
|
|
|
this.recordStockEvent( 'cancel' );
|
2019-03-27 17:59:29 +00:00
|
|
|
}
|
|
|
|
|
2019-06-18 15:58:10 +00:00
|
|
|
handleKeyDown( event ) {
|
|
|
|
if ( event.keyCode === ESCAPE ) {
|
|
|
|
this.cancelEdit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-05 20:31:20 +00:00
|
|
|
onQuantityChange( event ) {
|
|
|
|
this.setState( { quantity: event.target.value } );
|
2019-03-27 17:59:29 +00:00
|
|
|
}
|
|
|
|
|
2019-06-27 09:21:43 +00:00
|
|
|
onSubmit() {
|
|
|
|
const { product, updateProductStock } = this.props;
|
|
|
|
const { quantity } = this.state;
|
2019-03-28 17:39:55 +00:00
|
|
|
|
2019-06-27 09:21:43 +00:00
|
|
|
this.setState( { editing: false, edited: true } );
|
2019-03-28 19:10:01 +00:00
|
|
|
|
2019-06-27 09:21:43 +00:00
|
|
|
updateProductStock( product, quantity );
|
2020-08-03 16:38:57 +00:00
|
|
|
this.recordStockEvent( 'save', {
|
|
|
|
quantity,
|
|
|
|
} );
|
2019-03-28 17:39:55 +00:00
|
|
|
}
|
|
|
|
|
2019-03-27 17:59:29 +00:00
|
|
|
getActions() {
|
|
|
|
const { editing } = this.state;
|
|
|
|
|
|
|
|
if ( editing ) {
|
|
|
|
return [
|
2020-02-14 02:23:21 +00:00
|
|
|
<Button key="save" type="submit" isPrimary>
|
2019-03-28 17:39:55 +00:00
|
|
|
{ __( 'Save', 'woocommerce-admin' ) }
|
|
|
|
</Button>,
|
2020-02-14 02:23:21 +00:00
|
|
|
<Button key="cancel" type="reset">
|
|
|
|
{ __( 'Cancel', 'woocommerce-admin' ) }
|
|
|
|
</Button>,
|
2019-03-27 17:59:29 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
return [
|
2020-06-11 19:22:20 +00:00
|
|
|
<Button key="update" isSecondary onClick={ this.beginEdit }>
|
2019-03-27 17:59:29 +00:00
|
|
|
{ __( 'Update stock', 'woocommerce-admin' ) }
|
|
|
|
</Button>,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
getBody() {
|
2019-06-27 09:21:43 +00:00
|
|
|
const { product } = this.props;
|
2019-03-27 17:59:29 +00:00
|
|
|
const { editing, quantity } = this.state;
|
|
|
|
|
|
|
|
if ( editing ) {
|
|
|
|
return (
|
|
|
|
<Fragment>
|
2019-04-04 00:05:19 +00:00
|
|
|
<BaseControl className="woocommerce-stock-activity-card__edit-quantity">
|
|
|
|
<input
|
|
|
|
className="components-text-control__input"
|
|
|
|
type="number"
|
|
|
|
value={ quantity }
|
2019-06-18 15:58:10 +00:00
|
|
|
onKeyDown={ this.handleKeyDown }
|
2019-04-04 00:05:19 +00:00
|
|
|
onChange={ this.onQuantityChange }
|
2020-02-14 02:23:21 +00:00
|
|
|
ref={ ( input ) => {
|
2019-04-04 00:05:19 +00:00
|
|
|
this.quantityInput = input;
|
|
|
|
} }
|
|
|
|
/>
|
|
|
|
</BaseControl>
|
2019-03-27 17:59:29 +00:00
|
|
|
<span>{ __( 'in stock', 'woocommerce-admin' ) }</span>
|
|
|
|
</Fragment>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<span className="woocommerce-stock-activity-card__stock-quantity">
|
2020-02-14 02:23:21 +00:00
|
|
|
{ sprintf(
|
|
|
|
__( '%d in stock', 'woocommerce-admin' ),
|
|
|
|
product.stock_quantity
|
|
|
|
) }
|
2019-03-27 17:59:29 +00:00
|
|
|
</span>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-03-27 16:43:31 +00:00
|
|
|
render() {
|
|
|
|
const { product } = this.props;
|
2019-06-27 09:21:43 +00:00
|
|
|
const { edited, editing } = this.state;
|
2019-10-07 11:51:25 +00:00
|
|
|
const notifyLowStockAmount = getSetting( 'notifyLowStockAmount', 0 );
|
2019-06-27 09:21:43 +00:00
|
|
|
const lowStockAmount = Number.isFinite( product.low_stock_amount )
|
|
|
|
? product.low_stock_amount
|
|
|
|
: notifyLowStockAmount;
|
|
|
|
const isLowStock = product.stock_quantity <= lowStockAmount;
|
|
|
|
|
|
|
|
// Hide cards that are not in low stock and have not been edited.
|
|
|
|
// This allows clearing cards which are no longer in low stock after
|
|
|
|
// closing & opening the panel without having to make another request.
|
|
|
|
if ( ! isLowStock && ! edited ) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-03-27 16:43:31 +00:00
|
|
|
const title = (
|
2019-04-30 09:43:55 +00:00
|
|
|
<Link
|
2020-02-14 02:23:21 +00:00
|
|
|
href={
|
|
|
|
'post.php?action=edit&post=' +
|
|
|
|
( product.parent_id || product.id )
|
|
|
|
}
|
2020-08-03 16:38:57 +00:00
|
|
|
onClick={ () => this.recordStockEvent( 'product_name' ) }
|
2019-04-30 09:43:55 +00:00
|
|
|
type="wp-admin"
|
|
|
|
>
|
2019-03-27 16:43:31 +00:00
|
|
|
{ product.name }
|
|
|
|
</Link>
|
|
|
|
);
|
2019-04-02 17:37:50 +00:00
|
|
|
let subtitle = null;
|
|
|
|
|
2020-02-14 02:23:21 +00:00
|
|
|
if ( product.type === 'variation' ) {
|
2019-04-02 17:37:50 +00:00
|
|
|
subtitle = Object.values( product.attributes )
|
2020-02-14 02:23:21 +00:00
|
|
|
.map( ( attr ) => attr.option )
|
2019-04-02 17:37:50 +00:00
|
|
|
.join( ', ' );
|
|
|
|
}
|
2019-03-27 16:43:31 +00:00
|
|
|
|
2020-02-14 02:23:21 +00:00
|
|
|
const productImage =
|
|
|
|
get( product, [ 'images', 0 ] ) || get( product, [ 'image' ] );
|
2019-06-12 21:51:08 +00:00
|
|
|
const productImageClasses = classnames(
|
|
|
|
'woocommerce-stock-activity-card__image-overlay__product',
|
|
|
|
{
|
|
|
|
'is-placeholder': ! productImage || ! productImage.src,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
const icon = (
|
|
|
|
<div className="woocommerce-stock-activity-card__image-overlay">
|
|
|
|
<div className={ productImageClasses }>
|
|
|
|
<ProductImage product={ product } />
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
2020-02-14 02:23:21 +00:00
|
|
|
const activityCardClasses = classnames(
|
|
|
|
'woocommerce-stock-activity-card',
|
|
|
|
{
|
|
|
|
'is-dimmed': ! editing && ! isLowStock,
|
|
|
|
}
|
|
|
|
);
|
2019-06-12 21:51:08 +00:00
|
|
|
|
2019-06-18 15:58:10 +00:00
|
|
|
const activityCard = (
|
2019-03-27 16:43:31 +00:00
|
|
|
<ActivityCard
|
2019-06-27 09:21:43 +00:00
|
|
|
className={ activityCardClasses }
|
2019-03-27 16:43:31 +00:00
|
|
|
title={ title }
|
2019-04-02 17:37:50 +00:00
|
|
|
subtitle={ subtitle }
|
2019-06-12 21:51:08 +00:00
|
|
|
icon={ icon }
|
2019-03-27 17:59:29 +00:00
|
|
|
actions={ this.getActions() }
|
2019-03-27 16:43:31 +00:00
|
|
|
>
|
2019-03-27 17:59:29 +00:00
|
|
|
{ this.getBody() }
|
2019-03-27 16:43:31 +00:00
|
|
|
</ActivityCard>
|
|
|
|
);
|
2019-06-18 15:58:10 +00:00
|
|
|
|
|
|
|
if ( editing ) {
|
|
|
|
return (
|
2019-06-27 09:21:43 +00:00
|
|
|
<form onReset={ this.cancelEdit } onSubmit={ this.onSubmit }>
|
2019-06-18 15:58:10 +00:00
|
|
|
{ activityCard }
|
|
|
|
</form>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return activityCard;
|
2019-03-27 16:43:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-28 17:39:55 +00:00
|
|
|
export default compose(
|
2020-02-14 02:23:21 +00:00
|
|
|
withDispatch( ( dispatch ) => {
|
2019-06-27 09:21:43 +00:00
|
|
|
const { updateProductStock } = dispatch( 'wc-api' );
|
|
|
|
|
2019-03-28 17:39:55 +00:00
|
|
|
return {
|
2019-06-27 09:21:43 +00:00
|
|
|
updateProductStock,
|
2019-03-28 17:39:55 +00:00
|
|
|
};
|
|
|
|
} )
|
|
|
|
)( ProductStockCard );
|