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-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';
|
|
|
|
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,
|
|
|
|
};
|
|
|
|
|
|
|
|
this.beginEdit = this.beginEdit.bind( this );
|
|
|
|
this.cancelEdit = this.cancelEdit.bind( this );
|
|
|
|
this.onQuantityChange = this.onQuantityChange.bind( this );
|
2019-03-28 17:39:55 +00:00
|
|
|
this.updateStock = this.updateStock.bind( this );
|
2019-03-27 17:59:29 +00:00
|
|
|
}
|
|
|
|
|
2019-04-04 00:05:19 +00:00
|
|
|
componentDidUpdate() {
|
|
|
|
this.quantityInput && this.quantityInput.focus();
|
|
|
|
}
|
|
|
|
|
2019-03-27 17:59:29 +00:00
|
|
|
beginEdit() {
|
|
|
|
this.setState( { editing: true } );
|
|
|
|
}
|
|
|
|
|
|
|
|
cancelEdit() {
|
|
|
|
this.setState( {
|
|
|
|
editing: false,
|
|
|
|
quantity: this.props.product.stock_quantity,
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
|
|
|
onQuantityChange( quantity ) {
|
|
|
|
this.setState( { quantity } );
|
|
|
|
}
|
|
|
|
|
2019-03-28 17:39:55 +00:00
|
|
|
updateStock() {
|
|
|
|
const { product, updateItem } = this.props;
|
|
|
|
|
|
|
|
this.setState( { editing: false }, () => {
|
2019-03-28 19:10:01 +00:00
|
|
|
const data = {
|
|
|
|
stock_quantity: this.state.quantity,
|
2019-04-01 22:16:51 +00:00
|
|
|
type: product.type,
|
|
|
|
parent_id: product.parent_id,
|
2019-03-28 19:10:01 +00:00
|
|
|
};
|
|
|
|
|
2019-04-01 22:16:51 +00:00
|
|
|
updateItem( 'products', product.id, data );
|
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-03-28 17:39:55 +00:00
|
|
|
<Button onClick={ this.updateStock } isPrimary>
|
|
|
|
{ __( 'Save', 'woocommerce-admin' ) }
|
|
|
|
</Button>,
|
2019-03-27 17:59:29 +00:00
|
|
|
<Button onClick={ this.cancelEdit }>{ __( 'Cancel', 'woocommerce-admin' ) }</Button>,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
return [
|
|
|
|
<Button isDefault onClick={ this.beginEdit }>
|
|
|
|
{ __( 'Update stock', 'woocommerce-admin' ) }
|
|
|
|
</Button>,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
getBody() {
|
|
|
|
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 }
|
|
|
|
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">
|
|
|
|
{ sprintf( __( '%d in stock', 'woocommerce-admin' ), quantity ) }
|
|
|
|
</span>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-03-27 16:43:31 +00:00
|
|
|
render() {
|
|
|
|
const { product } = this.props;
|
|
|
|
const title = (
|
2019-04-04 17:11:48 +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
|
|
|
|
|
|
|
return (
|
|
|
|
<ActivityCard
|
|
|
|
className="woocommerce-stock-activity-card"
|
|
|
|
title={ title }
|
2019-04-02 17:37:50 +00:00
|
|
|
subtitle={ subtitle }
|
2019-03-27 16:43:31 +00:00
|
|
|
icon={ <ProductImage product={ product } /> }
|
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-03-28 17:39:55 +00:00
|
|
|
export default compose(
|
|
|
|
withDispatch( dispatch => {
|
|
|
|
const { updateItem } = dispatch( 'wc-api' );
|
|
|
|
return {
|
|
|
|
updateItem,
|
|
|
|
};
|
|
|
|
} )
|
|
|
|
)( ProductStockCard );
|