2019-03-27 16:43:31 +00:00
|
|
|
/** @format */
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
|
|
|
/**
|
|
|
|
* WooCommerce dependencies
|
|
|
|
*/
|
|
|
|
import { Link, ProductImage } from '@woocommerce/components';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import { ActivityCard } from '../../activity-card';
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
},
|
|
|
|
() => {
|
|
|
|
this.quantityInput && this.quantityInput.focus();
|
|
|
|
}
|
|
|
|
);
|
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
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
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 );
|
2019-03-28 17:39:55 +00:00
|
|
|
}
|
|
|
|
|
2019-03-27 17:59:29 +00:00
|
|
|
getActions() {
|
|
|
|
const { editing } = this.state;
|
|
|
|
|
|
|
|
if ( editing ) {
|
|
|
|
return [
|
2019-06-18 15:58:10 +00:00
|
|
|
<Button type="submit" isPrimary>
|
2019-03-28 17:39:55 +00:00
|
|
|
{ __( 'Save', 'woocommerce-admin' ) }
|
|
|
|
</Button>,
|
2019-06-18 15:58:10 +00:00
|
|
|
<Button type="reset">{ __( 'Cancel', 'woocommerce-admin' ) }</Button>,
|
2019-03-27 17:59:29 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
return [
|
|
|
|
<Button isDefault onClick={ this.beginEdit }>
|
|
|
|
{ __( '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 }
|
|
|
|
ref={ input => {
|
|
|
|
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">
|
2019-06-27 09:21:43 +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;
|
|
|
|
const { notifyLowStockAmount } = wcSettings;
|
|
|
|
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
|
|
|
|
href={ 'post.php?action=edit&post=' + ( product.parent_id || product.id ) }
|
|
|
|
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;
|
|
|
|
|
|
|
|
if ( 'variation' === product.type ) {
|
|
|
|
subtitle = Object.values( product.attributes )
|
|
|
|
.map( attr => attr.option )
|
|
|
|
.join( ', ' );
|
|
|
|
}
|
2019-03-27 16:43:31 +00:00
|
|
|
|
2019-06-12 21:51:08 +00:00
|
|
|
const productImage = get( product, [ 'images', 0 ] ) || get( product, [ 'image' ] );
|
|
|
|
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>
|
|
|
|
);
|
2019-06-27 09:21:43 +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(
|
|
|
|
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 );
|